Using an image, how do I find the coordinates of pixels of a certain colors?
24 views (last 30 days)
Show older comments
Hello,
From the image pasted below, I want to make 2 different calculations.
- Caclulate the center of mass of the white pebble. To do this, I want to find all the coordinates of the white pebble and the black dot. Once I get the total number of rows and columns, I want to average this to find center of mass. I am trying to write a script that tells matlab to store all the values that are not red pixels.
- Caclulate the center of mass of only the black dot. I find all the coordinates of the black dot, and then average the total number of rows and columns to find the center of mass.
I have also pasted the code I have so far. The loop I made is not working and when i tried other variations, I had difficulty writing the code so that it only read the pixels of the colors I want.
image = imread('red_backpebble1.png');
redChannel = image(:,:,1);
greenChannel = image(:,:,2);
blueChannel = image(:,:,3);
% black pixels are where red channel and green channel and blue channel in an rgb image are 0;
blackpixelsmask = redChannel == 0 & greenChannel == 0 & blueChannel == 0;
[rows, columns, number] = size(image);
[x, y] = meshgrid(1: columns, 1:rows);
while k == blackpixelsmask
fprintf('Pixel #%d is at (x, y) = (%d, %d)\n',...
k, x(k), y(k));
end
com_x = rows/2;
com_y = columns/2;
disp("The Center of Mass of the black dot is ("+com_x+","+com_y+")")
0 Comments
Answers (1)
Mahesh Taparia
on 15 Dec 2020
Hi
You can use regionprops function to find the pixel list and centroid of the white and black part. For more information, you can refer this documentation. For illustration, consider the below code:
I=imread('image.png');
whitePart=(I(:,:,1)==255)&(I(:,:,2)==255)&(I(:,:,3)==255);
blackPart=(I(:,:,1)==0)&(I(:,:,2)==0)&(I(:,:,3)==0);
figure;imshow(whitePart)
figure;imshow(blackPart)
blachRegion=regionprops(blackPart,'all');
whiteRegion=regionprops(whitePart,'all');
Hope it will help!
0 Comments
See Also
Categories
Find more on Image Filtering and Enhancement 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!