How to make image from raw data in MATLAB
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello, I hope you are doing well. I have the following two dataset. I want to make a binary image
the data has shape of 1x1000. How can i do it in MATLAB. I have used the following code but it does not work. Can any body help me in that
[numImages, lenImage] = size(data1);
imSz = 1000; % assuming images are 1000x1000
imbg = false(imSz); % background "color"
imfg = ~imbg(1,1); % forground "color"
imSizeOut=[1000 1000];
for imNum = 1:numImages
imData =data1(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 = flipud(BW);
end
2 comentarios
DGM
el 8 de Oct. de 2022
Editada: DGM
el 8 de Oct. de 2022
What is data1? It looks like it's a numeric array consisting of concatenated row vectors, each of length 1000. It looks like you're trying to extract each row, expand it to 1000x1000 and do some work on it.
If that is correct, I think we need to then know what the class and range of data1 is. Some of the casting and comparison operations may be causing problems due to that.
If data1 is a cell array, then the addressing would be incorrect, but we'd still need to know what the cells contain.
Respuestas (1)
Tejas
el 24 de Dic. de 2024
Hello Stephen,
One method to create a binary image from a 1-D array is by using the array elements as indices for the image pixels.
Here are the steps to generate a binary image:
- Within the 'for' loop, retrieve the data for each shape and scale it so that the array elements can serve as indices for the pixels.
imData = round(data(imNum, :) * imSz);
BW = false(imSz);
- Iterate over the data for a shape and set the pixel values to 'true' for valid indices.
for idx = 1:length(imData)
if imData(idx) > 0 && imData(idx) <= imSz
BW(imData(idx), idx) = true;
end
end
- Convert the binary image elements to 'uint8' and apply dilation.
im = im2uint8(BW);
SE = strel('disk', 2);
BW = imdilate(im, SE);
im = flipud(BW);
Below is a screenshot showing how the image appears for the first shape:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1821868/image.png)
0 comentarios
Ver también
Categorías
Más información sobre Image Preview and Device Configuration en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!