How to Convert data into Image using Matlab

57 visualizaciones (últimos 30 días)
Stephen john
Stephen john el 8 de Sept. de 2022
Comentada: Walter Roberson el 3 de Oct. de 2022
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)

Rik
Rik el 8 de Sept. de 2022
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 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.
Stephen john
Stephen john el 3 de Oct. de 2022
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 It will not work on whole dataset
Walter Roberson
Walter Roberson el 3 de Oct. de 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)

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by