How to convert a figure in matrix of pixel?

8 views (last 30 days)
I have a picture and I want to convert this into a numeric matrix. I want to create a matrix of 1's and 0's where the 0's represent the orange pixels and the 1's represent the transparent pixels.
Can anybody help me?
  4 Comments
SAC CSA
SAC CSA on 14 Dec 2021
Hi @Rik and @Benjamin, thank you for the comment. The image has an extension .png. The image represents a bifurcation of tubulation. Then, is a real image. The idea is obtain a matrix of 128x128.
SAC CSA
SAC CSA on 14 Dec 2021
@Rik and @Benjamin. The Mathematica code for this procedure is relatively simple, but I would like to get a version of the code in matlab.
image = Import["https://i.stack.imgur.com/CQkvZ.png"]
data = ImageData[ChanVeseBinarize[image]]

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 14 Dec 2021
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/834235/image.png';
img = imread(filename);
whos
Name Size Bytes Class Attributes filename 1x79 158 char img 373x363x3 406197 uint8
bw = imbinarize( rgb2gray(img) );
whos bw
Name Size Bytes Class Attributes bw 373x363 135399 logical
imshow(bw)
Notice that the result is logical datatype, with 1 for the white pixels and 0 for the black pixels. Depending exactly what you are doing, you might need to convert that to double precision -- for example if you are wanting to use it as Alpha data, you will need to convert to double precision.
  4 Comments
SAC CSA
SAC CSA on 17 Dec 2021
Hi @Walter Roberson, thank you very much for working on the code above. It worked perfectly for a 2D figure.
Now, I tried to apply an similar protocol to a 3D figure, like the image 2 attached.
However, although the image depicts a 3D plot, there are only 2D (448x416) pixels. I'm trying to get a 3D matrix (128x128x128) of 1's and 0's where the 0's represent the branches' pixels and the transparent part is 1's inside of the box.
In this case, do you think it would be necessary to transform the image into a picture in 3D pixels to then get the matrix?
Could you help me to obtain the matrix, please?
Thank you so much in advance!
Walter Roberson
Walter Roberson on 17 Dec 2021
Depth cannot be calculated from a single 2d picture (unless there are cues such as color)

Sign in to comment.

More Answers (0)

Categories

Find more on Images 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!