- White [1 1 1]
- Blue: [0 0 1]
- Yellow [1 1 0]
- Green [0 1 0]
- Red [1 0 0]
How to find the colour of an image at specific pixel values?
조회 수: 29 (최근 30일)
이전 댓글 표시
I have an image with 16 region props. I have found the centroids of my desired region.
I am now required to find the colour of the image at each specific region. How would I go ahead doing this?
My centoids are (y1,x1;y2,x2;,...,y16,x16)
I am trying to identify the colour that the pixel is for each centroid. available colours are : white,blue,yellow,green and red.
Thank you
댓글 수: 0
채택된 답변
Adam Danz
2022년 3월 14일
편집: Adam Danz
2022년 3월 15일
The solution depends on the image type you're using but this demo should get you started. See in-line comments for more information.
The demo shows the normalized RGB values but if you want to classify colors by color names you'll need to add an additional step.
Pure color definitions in RGB space are
But unless you're using exactly those colors you'll need a more rigorous color classification that classifies floating point RGB values into one of the color categories.
% Read and display image
I = imread('peppers.png');
imshow(I)
axis on
% Identify regions of interest
% coordinates should be the [x,y] areas of the image and in this example
% coordinates is also the image indicies. If you image specifies x and
% y values such that the indicies do not equal the x and y data, you'll
% need an additional step to get convert the (x,y) coordinates to indicies.
coordinates = [320 150; 100 230; 250 335];
hold on
plot(coordinates(:,1), coordinates(:,2), 'wo', 'LineWidth', 2)
% Get color values at specified indicies
cval = I(coordinates(:,2), coordinates(:,1), :);
RGB = [diag(cval(:,:,1)), diag(cval(:,:,2)), diag(cval(:,:,3))];
% Convert to normalized RGB values - you may not need this step if your
% image already uses normalized RGB values.
rgbNorm = double(RGB)./ double(intmax('like',RGB));
rgbStr = compose('[%.2f, %.2f, %.2f]',rgbNorm);
% Display color values next to selected areas - for demo purposes only.
text(coordinates(:,1)+10, coordinates(:,2)-10, rgbStr, ...
'Color', 'W', ...
'VerticalAlignment','baseline', ...
'BackgroundColor', 'k')
% Show the three colors
figure()
hold on
x = 1:size(rgbNorm,1);
scatter(x, 0.5, 1500, rgbNorm, 'filled')
xlim([-3,5])
axis off
댓글 수: 0
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!