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