How can I convert data from a line figure to binary?
5 views (last 30 days)
Show older comments
Paschalis Garouniatis
on 27 May 2016
Edited: Paschalis Garouniatis
on 4 Jun 2016
Hello again. I have the following line plot. Instead of that I want to create an image which will be consisted of pixel data. To be more specific I want the data to be binary and value '1' should match each pair (x,y) for each line (there are 25 lines plotted in the graph below)and value '0' everything else . I have extracted all data from the graph and they are in cell form (xdata cell 25x1 and ydata cell 25x1). Is it possible to be done? Thanks in advance.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/165330/image.jpeg)
0 Comments
Accepted Answer
Image Analyst
on 28 May 2016
Just use a double for loop to set pixels. Assign the x to a column and the y to a row in the binary image.
binaryImage = false(4500, 200);
for k = 1 : length(x)
row = round(y);
col = round(x+100);
binaryImage(row, col) = true;
end
9 Comments
Image Analyst
on 4 Jun 2016
Try this:
clc; % Clear the command window.
clear all;
close all;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontsize = 24;
sx = load('x-data.mat')
sy = load('y-data.mat')
cellArrayX = sx.x
cellArrayY = sy.y
allX = [];
allY = [];
subplot(2, 1, 1);
for k = 1 : length(cellArrayX)
thisCellX = cellArrayX{k};
thisCellY = cellArrayY{k};
plot(thisCellX, thisCellY, 'b-');
grid on;
hold on;
% Append
allX = [allX, thisCellX];
allY = [allY, thisCellY];
end
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Get min and max
minX = min(allX);
maxX = max(allX);
minY = min(allY);
maxY = max(allY);
fprintf('min x = %f\nmax x = %f\nmin y = %f\nmax y = %f\n',...
minX, maxX, minY, maxY);
% Shift to start at 1 on the x and y axes
allX = allX - minX + 1;
allY = allY - minX + 1;
% Get updated min and max
minX2 = min(allX);
maxX2 = max(allX);
minY2 = min(allY);
maxY2 = max(allY);
fprintf('min x = %f\nmax x = %f\nmin y = %f\nmax y = %f\n',...
minX2, maxX2, minY2, maxY2);
% Declare a binary image of about 600 rows by 800 columns.
rows = 600;
columns = 800;
binaryImage = false(rows, columns);
% Set pixels in the binary image.
for k = 1 : length(cellArrayX)
thisCellX = cellArrayX{k} - minX;
thisCellY = cellArrayY{k} - minY;
x = round(columns * thisCellX / maxX) + 1;
y = round(rows * thisCellY / maxY) + 1;
for k2 = 1 : length(x)
binaryImage(y(k2), x(k2)) = true;
end
end
subplot(2, 1, 2);
imshow(binaryImage);
title('Binary Image', 'FontSize', fontsize);
Or, if you want just what you see from plot(), then save the axes with export_fig.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!