How to Convert data into Image using Matlab

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 comentarios

Rik
Rik el 8 de Sept. de 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
Stephen john el 8 de Sept. de 2022
@Rik I want to create a binary Image thats why i am doing this , is there is any other method Which is use to convert data into image? The wrong is when i round decimal values the shape is change

Iniciar sesión para comentar.

Respuestas (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 comentarios

Stephen john
Stephen john el 13 de Sept. de 2022
Editada: Rik el 13 de Sept. de 2022
When the values are fixed there is no change it gives the following error
load Dataset2
dataset=ans;
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');
Error using matlab.internal.math.interp1
Sample points must be unique.

Error in interp1 (line 188)
VqLite = matlab.internal.math.interp1(X,V,method,method,Xqcol);
%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))
Rik
Rik el 13 de Sept. de 2022
Editada: Rik el 13 de Sept. de 2022
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
Stephen john el 30 de Sept. de 2022
@Rik I have attached the dataset, Have you check on this for the error?
Rik
Rik el 30 de Sept. de 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
Stephen john el 30 de Sept. de 2022
The above code you share does not work on the data which i have attached below, When i run the code the error comes
Rik
Rik el 30 de Sept. de 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
Stephen john el 3 de Oct. de 2022
yeah it has only one values means we have a straight white line in the image.
The same code i used which you have pasted but it gives the above error as i show.
I need a code which works on this data as well.
the following error
Error using matlab.internal.math.interp1
Sample points must be unique.
Error in interp1 (line 188)
VqLite = matlab.internal.math.interp1(X,V,method,method,Xqcol);

Iniciar sesión para comentar.

Walter Roberson
Walter Roberson el 3 de Oct. de 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 comentarios

Stephen john
Stephen john el 3 de Oct. de 2022
@Walter Roberson Anyother solution to solve this problem?
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
Stephen john el 3 de Oct. de 2022
@Walter Roberson Is there is anyother code to draw this lines and shape into binary image?
Stephen john
Stephen john el 3 de Oct. de 2022
@Walter Roberson Can you provide any solution for this?
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
Stephen john el 3 de Oct. de 2022
@Walter Roberson It will not work on whole dataset
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)

Iniciar sesión para comentar.

Categorías

Productos

Versión

R2021b

Preguntada:

el 8 de Sept. de 2022

Comentada:

el 3 de Oct. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by