How to Convert data into Image using Matlab
Show older comments
Hello Everyone, I Hope you are doing well. I have the following data, I have written a code to convert the data into Image. But when i round the values the Image does not same as the plot, I am doing round because each value represent a pixel value.
The Dataplot.jpg shows the original data, which is also attached in dataScan.mat. The output image after the code is imagefromdataset.jpg.
Is there is any way to get the same shape as original data. Due to rounding the value the shape changes.
How can i modified the code to get the same shape in image.
%% create grayscale shapes that resemble the data
[numImages, lenImage] = size(dataset);
imSz = 1000; % assuming images are 1000x1000
imbg = false(imSz); % background "color"
imfg = ~imbg(1,1); % forground "color"
imSizeOut=[1000 1000]; % ImageSize
for imNum = 1:numImages
imData = round(dataset(imNum,:)); % get pattern
[~,Y] = meshgrid(1:imSz); % make a grid
% black and white image
BW = imbg;
BW(Y==imData)=imfg;
% resize (from 1000x1000)
BW=imbinarize(imresize(uint8(BW),imSizeOut));
% convert to uint8 (0 255)
im = im2uint8(BW);
SE=strel('disk',2);
BW=imdilate(im,SE);
%im = BW;
im = flipud(BW);
end
2 Comments
Rik
on 8 Sep 2022
Why are you doing it like this, instead of capturing the image from the plot?
I don't see what is going wrong. You picked a low resolution and a large range. What exactly is different between this result and your expectations?
Stephen john
on 8 Sep 2022
Answers (2)
The shape did not actually change. You just expanded the y from [-2 18] to [0 1000].
S=load(websave('tmp.mat','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1119810/dataScan.mat'));
dataset=S.dataset;
plot(dataset,'o'),ylim([0 1000])
If you want to reproduce this data yourself, one way to that is to create the coordinate grid explicitly:
x=1:numel(dataset);%this is what you did implictly
y=dataset;
resolution=1000;
X=linspace(min(x), max(x) ,resolution);
Y=linspace(min(y), max(y) ,resolution);
% determine index in X of each x, likewise for y (note that the coordinates
% are flipped for the y-direction in images).
ind_x=interp1(X, 1:numel(X) ,x,'nearest');
ind_y=interp1(Y,numel(Y):-1:1,y,'nearest');
%compute linear index
ind=sub2ind([resolution resolution],ind_y,ind_x);
% create the image
IM=zeros(resolution,resolution);
IM(ind)=1;
% Since you apparently want a dilation, apply it before displaying the
% image.
SE=strel('disk',2);
imshow(imdilate(IM,SE))
8 Comments
Next time, please attach the mat file to the same comment that has the code and use the green triangle to run the code. I will now edit this thread.
You also don't need to @-mention me every comment.
What is the range you want the y-values to have on your image?
My code assumes you want the full data range, but if you only have a single y value, then that doesn't make much sense, as you would simply get a white image.
What do you want to happen in that case?
Stephen john
on 30 Sep 2022
Rik
on 30 Sep 2022
I asked you several question (which you ignored) and told you that you don't need to @-mention me every comment (which you ignored as well).
Why exactly do you expect me to respond?
Stephen john
on 30 Sep 2022
Rik
on 30 Sep 2022
Of course the code doesn't work. You only have 1 y-value. Do you want a completely white image with a height of 1 pixel?
You also misread the part that you now did: you attached the mat file to a comment that does not contain your code. Whenever you post code or mention an error, you should make sure we can see that error. That means you have to put the code and the data in a single comment and use the green triangle to run your code.
In this case that is not needed, because the cause is not your data or my code. The cause is that there is an edge case that you didn't explain. It is not clear what should happen. That is something you need to explain.
Stephen john
on 3 Oct 2022
Stephen john
on 3 Oct 2022
Walter Roberson
on 3 Oct 2022
yeah it has only one values means we have a straight white line in the image.
Y=linspace(min(y), max(y) ,resolution);
With your y values all being the same, min(y) and max(y) are going to be equal, so you would be generating a constant vector with resolution elements in it.
ind_y=interp1(Y,numel(Y):-1:1,y,'nearest');
Your Y values (the independent variable for interpolation purposes) are all the same, but interp1() requries that the values of the independent variables are all different.
I need a code which works on this data as well.
Well you cannot use interp1() in that case.
Your situation is asking interp1([5 5 5], [3 2 1], [5 5 5 5], 'nearest') . Yes, 5 (in the query values) appear in the independent vector [5 5 5], but do you say that they should be considered to be come from location 3, or location 2, or location 1 ?
You will need to detect this case and treat it differently.
8 Comments
Stephen john
on 3 Oct 2022
Walter Roberson
on 3 Oct 2022
if Y(1) == Y(end)
do something else
else
proceed with the interp1()
end
but in the "do something else" section you need to figure out what a reasonable output would be.
I am not clear at the moment as to why you are not using Computer Vision insertShape() to "draw" the line into a matrix without any of that interp1() stuff.
Stephen john
on 3 Oct 2022
Walter Roberson
on 3 Oct 2022
Computer Vision insertShape() makes it easy.
But if you do not have the Computer Vision Toolbox, then use Bresenham's Line Algorithm https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
see
Stephen john
on 3 Oct 2022
Walter Roberson
on 3 Oct 2022
Scale your coordinates to the size of the output image, x to fit OUTPUT_COLUMNS and y to fit OUTPUT_ROWS
Coords = [Scaled_x(:), Scaled_y(:)];
M = zeros(OUTPUT_ROWS, OUTPUT_COLUMNS);
M = insertShape(M, 'line', Coords, 'Color', 'b'); %outputs RGB
M = M(:,:,1) ~= 0; %convert RGB to binary
Stephen john
on 3 Oct 2022
What error message are you getting?
OUTPUT_ROWS = 384; OUTPUT_COLUMNS = 384;
Scaled_x = [1:200, 200, 200:-1:150, 150, 150:175];
Scaled_y = [50*ones(1,200), 150, 151:151+51-1, 83, 84:-1:59];
Coords = [Scaled_x(:), Scaled_y(:)];
M = zeros(OUTPUT_ROWS, OUTPUT_COLUMNS);
M = insertShape(M, 'line', Coords, 'Color', 'white'); %outputs RGB
M = M(:,:,1) ~= 0; %convert RGB to binary
imshow(M)
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

