Read ImageJ XY coordinates file into matlab

108 Views Asked by At

I am trying to read an ImageJ XY coordinates file (attached) using the code below but I get a strange output. Any idea what I did wrong?

clc;
clear;
close all;
imgMatrix = readmatrix("E:/fd1.txt"); 
imgMatrix(:,3) = []
x=imgMatrix(:,1);y=imgMatrix(:,2);
BW = poly2mask(x,y,ceil(max(y)) ,ceil(max(x)));
imshow(BW);

Original image:

enter image description here

Output image:

enter image description here

fd1.txt file: https://mega.nz/file/vE0SxDgb#rQuWCsH8H2fYvuzGTSMgjyMMdyZGfQYMhLJ3zdXcJB4

Thanks a lot

2

There are 2 best solutions below

6
Herbie On

I must admit that I have no experience with MATLAB but let me explain a bit the format ImageJ uses, if you export values per "Analyze >> Tools >> Save XY Coordinates...".

If, like in the case in question, the background of an 8bit gray-value image is black (zero), I would first invert the image (important!) and set the background value in the "Save XY Coordinates"-dialog to zero, as well as check "Invert y coordinates off".

In the following I shall provide an ImageJ-macro that indirectly explains the structure of the data by reconstructing the image from the data that has to be saved with suffix ".tsv" not ".txt". Open this data file in ImageJ first and then run the macro:

requires("1.54g");
x=Table.getColumn("C1");
y=Table.getColumn("C2");
v=Table.getColumn("C3");
Array.getStatistics(x,mi,xMax);
Array.getStatistics(y,mi,yMax);
newImage("Reconstruction","8-bit black",xMax+1,yMax+1,1);
for (i=0;i<x.length;i++)
      setPixel(x[i],y[i],v[i]);
run("Invert");
exit();

HTH

0
Herbie On

Below please find an ImageJ-macro that saves the outline coordinates of a binary-valued image:

requires("1.54g");
run("Input/Output...", "jpeg=80 gif=-1 file=.txt");
run("Create Selection");
getSelectionCoordinates(xpoints, ypoints);
Array.show(xpoints, ypoints);
saveAs("text");
exit();