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

4 views (last 30 days)
Med Future on 2 Nov 2022
Commented: Image Analyst on 9 Nov 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

DGM on 2 Nov 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.
DGM on 9 Nov 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) Image Analyst on 2 Nov 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); Image Analyst on 9 Nov 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?

Walter Roberson on 2 Nov 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 CommentsShow 1 older commentHide 1 older comment
Walter Roberson on 2 Nov 2022
(CVT means Computer Vision Toolbox in this context, and IPT means Image Processing Toolbox)

### Categories

Find more on Image Processing and Computer Vision in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!