# How to create a Binary image from two columns of raw data

11 visualizaciones (últimos 30 días)
Med Future el 2 de Nov. de 2022
Comentada: Image Analyst el 9 de Nov. de 2022
Hello, I have the following dataset, which consists of two columns. I have also attached a scatter plot of the dataset, where first column is on x axis and 2nd column is on y axis.
I want to create a Binary image for the dataset.
How can i do it in MATLAB
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Respuestas (3)

DGM el 2 de Nov. de 2022
This is similar to the prior answer, but in this case, we need to deal with scaling both x and y data.
outsize = [500 500];
% rescale data to fit width, generate indices
dlen = size(pdw,1);
x0 = pdw(:,1);
y0 = pdw(:,2);
x0 = (outsize(2)-1)*normalize(x0,'range') + 1;
xidx = 1:outsize(2);
yidx = interp1(x0,y0,xidx);
yidx = outsize(1) - (outsize(1)-1)*normalize(yidx,'range');
% display a dummy image to fix geometry
imshow(false(outsize))
% create ROI object and configure
ROI = images.roi.Polyline(gca);
ROI.Position = [xidx(:) yidx(:)];
imshow(outpict)
Note that strokes are drawn across any periods where there are no samples being taken.
The same configuration of xidx,yidx would work with the rudimentary non-aa polyline example given in that same thread.
##### 12 comentariosMostrar 10 comentarios más antiguosOcultar 10 comentarios más antiguos
Med Future el 8 de Nov. de 2022
@DGM Did we able to find values from image as on original dataset?
DGM el 9 de Nov. de 2022
If you knew what the original range of y had been, you might recover the y-data with a resolution determined by the height of the image. All the actual x-data will be lost. It's merely one dot per sample. Any jumps in x are not represented by the image.
Of course, I don't know why you would be wanting to recover the data from the image. Even if the issue with the nonuniform x-data weren't the case, you'd still be losing information in converting it to a raster image.
% you have an output image
% and a limited-precision stored reference to the original data range
y0range = [1.41351e-05 0.00327854];
% you can recover the y-position of each dot
[yidx,~] = find(inpict);
% you can rescale to data units
yrec = y0range(2) - rescale(yidx,0,range(y0range));
% compare to the original data
y0 = pdw(:,2);
% compare original and recovered data
xl = [1830 3196]; % look at a closeup
yl = [0.0061 0.2693]*1E-3;
subplot(2,1,1)
plot(y0); xlim(xl); ylim(yl)
subplot(2,1,2)
plot(yrec); xlim(xl); ylim(yl)
% plot the error
plot(y0-yrec)

Iniciar sesión para comentar.

Image Analyst el 2 de Nov. de 2022
What do you want the size of this image to be in pixels? How many rows and columns?
xy = s.pdw;
x = xy(:, 1);
y = xy(:, 2);
subplot(2, 1, 1);
plot(x, y, 'b.', 'MarkerSize', 10);
grid on;
% Define the size of the image you want
rows = 512;
columns = 1500;
% Rescale data
x = rescale(x, 1, columns);
y = rescale(y, 1, rows);
binaryImage = false(rows, columns);
for k = 1 : length(x)
row = rows - round(y(k)) + 1;
col = round(x(k));
binaryImage(row, col) = true;
end
subplot(2, 1, 2);
imshow(binaryImage);
##### 6 comentariosMostrar 4 comentarios más antiguosOcultar 4 comentarios más antiguos
DGM el 3 de Nov. de 2022
The comment I posted on my answer above generates 14 images, each 1000px wide, rendering 1px per datapoint (i.e. not a polyline)
Image Analyst el 9 de Nov. de 2022
Let's step back and ask WHY you want 1000 binary images, or even one binary image from your data? I see no need for it, and you haven't given any reason - you just said that you want that but with no justification. What do the original coordinates represent in reality?

Iniciar sesión para comentar.

Walter Roberson el 2 de Nov. de 2022
filename = 'matlab.mat';
pdw = datastruct.pdw;
targetsize = [930 1860]; %rows, columns
margin = 5;
scaled_x = rescale(pdw(:,1), margin+1, targetsize(2)-margin);
scaled_y = rescale(pdw(:,2), margin+1, targetsize(1)-margin);
canvas = zeros(targetsize(1), targetsize(2), 3, 'uint8');
r = 3;
xyr = [scaled_x, scaled_y, r * ones(size(scaled_x))];
canvas = insertShape(canvas, 'circle', xyr, 'Color','white');
binary = canvas(:,:,1) > 0;
%verify
imshow(flipud(binary))
Except for the imshow() at the end, none of this requires the graphics system or creating any files.
##### 2 comentariosMostrar NingunoOcultar Ninguno
DGM el 2 de Nov. de 2022
Editada: DGM el 2 de Nov. de 2022
FWIW, it does require CVT.
Not that I'm in any position to object, since I got lazy and used IPT ROI tools. Still, the linked comment has a polyline implementation that does not. I suppose even imshow() is an IPT dependency depending on version.
I suppose I could've simplified the rescaling though.
Walter Roberson el 2 de Nov. de 2022
(CVT means Computer Vision Toolbox in this context, and IPT means Image Processing Toolbox)

Iniciar sesión para comentar.

### Categorías

Más información sobre Convert Image Type en Help Center y File Exchange.

R2022b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by