Convert pixel into Coordinate

7 views (last 30 days)
Danilo Sanfilippo
Danilo Sanfilippo on 17 Jul 2020
Answered: Kiran Felix Robert on 22 Jul 2020
Hello All,
I have an image of a material made by 3 different components. For each component I assigned a colour. I have attached the 2 photos.
I would like to convert the pixels into coordinate assigning the type and defining also the scale of axis.
As the final result I would like to obtain these informations:
x and y lenght
N# pixel x y type
1 0.01 0.01 1
2 0.02 0.01 1
3 0.03 0.01 2
4
...
I have already did something similar for a binary images but not with and rgb
Many thanks

Answers (1)

Kiran Felix Robert
Kiran Felix Robert on 22 Jul 2020
Hi Danilo Sanfilippo,
I understand that you have a RGB image with three colours each corresponding to a different material. You attempt to determine which pixel correspond to which type, viz. 1,2,3 and to get the coordinate by using an appropriate scaling value from the pixel locations.
The image segmentation using k-means function can be used to map each pixel value to the type of material (Assuming each colour as a cluster, here you can use it to be 3 clusters as you need 3 different materials). (imsegkmeans)
pixelsType = imsegkmeans(image,3,'NumAttempts',4);
The output of imsegkmeans function, say pixelsType, is a 2D matrix which is same as the size of the image, with the type number of each pixel corresponding to the Matrix Element. The pixelsType matrix can be directly used to get the coordinate value using the appropriate scaling factor as shown below,
nr = size(pixelType,1); % Number of Rows
nc = size(pixelType,2); % Number of Columns
N = 1:nr*nc; % Pixels Number Vector
kx = 0.5; %x - scaling Factor;
ky = 0.5; %y - scaling Factor;
x = zeros(nc*nr,1); %pre-allocating the x-coordinates
y = zeros(nc*nr,1); %pre-allocating the y-coordinates
type = zeros(nc*nr,1); %pre-allocating the type
i = 1;
for r = 1:nr
for c = 1:nc
x(i) = r*kx;
y(i) = c*ky;
type(i) = pixelsType(r,c);
i = i + 1;
end
end
A = [N' x y type]; % matrix A contains the required values
csvwrite('Req.csv',A) %Write the matrix A into a csv file
Kiran Felix Robert

Categories

Find more on Image Processing Toolbox 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!