Recognition of different colors in one picture

조회 수: 1 (최근 30일)
John Mulcahey
John Mulcahey 2019년 3월 4일
답변: Vidhi Agarwal 2024년 9월 19일
Hello,
I need a help with one algorithm, or at least a suggestion how to start (because I'm Matlab beginner). I need to recognize the colors from the picture. I need my algorithm to take both halves of the picture and recognize beige on one half and blue on the other one. That's the basic. All I can do for now is to load a picture... I also need it to recognize if there is something wrong with it (like big black spot on picture). I think It could be doable with some kind of area where I would calculate mean color or something like that, but I don't how to do it (or maybe there is some better way in Matlab to do it).
Thank you very much for all your help and suggestions!

답변 (1개)

Vidhi Agarwal
Vidhi Agarwal 2024년 9월 19일
I understand you have a query regarding an algorithm to recognizes colours in different halves of an image and identifies any large black spots. You can achieve the same by following the below steps:
  • Start by loading the image into MATLAB.
imagePath = 'your_image.jpg'; % Replace with your image file path
img = imread(imagePath);
  • Split the image into two halves.
[rows, cols, ~] = size(img);
% Split the image into two halves
leftHalf = img(:, 1:floor(cols/2), :);
rightHalf = img(:, floor(cols/2)+1:end, :);
  • Analyse each half to identify the predominant colours.
leftHSV = rgb2hsv(leftHalf);
rightHSV = rgb2hsv(rightHalf);
% Define thresholds for beige and blue in HSV
beigeThreshold = [0.1 0.1 0.5; 0.2 0.3 1]; % Adjust these values based on your image
blueThreshold = [0.55 0.4 0.2; 0.75 1 1];
% Detect beige in the left half
beigeMaskLeft = (leftHSV(:,:,1) >= beigeThreshold(1,1) & leftHSV(:,:,1) <= beigeThreshold(2,1)) & ...
(leftHSV(:,:,2) >= beigeThreshold(1,2) & leftHSV(:,:,2) <= beigeThreshold(2,2)) & ...
(leftHSV(:,:,3) >= beigeThreshold(1,3) & leftHSV(:,:,3) <= beigeThreshold(2,3));
% Detect blue in the right half
blueMaskRight = (rightHSV(:,:,1) >= blueThreshold(1,1) & rightHSV(:,:,1) <= blueThreshold(2,1)) & ...
(rightHSV(:,:,2) >= blueThreshold(1,2) & rightHSV(:,:,2) <= blueThreshold(2,2)) & ...
(rightHSV(:,:,3) >= blueThreshold(1,3) & rightHSV(:,:,3) <= blueThreshold(2,3));
% Calculate the proportion of each color
beigeProportion = sum(beigeMaskLeft(:)) / numel(beigeMaskLeft);
blueProportion = sum(blueMaskRight(:)) / numel(blueMaskRight);
  • Check for any large black spots in the image.
blackThreshold = 0.1;
% Create a mask for black areas
blackMask = (img(:,:,1) < blackThreshold) & (img(:,:,2) < blackThreshold) & (img(:,:,3) < blackThreshold);
% Calculate the proportion of black areas
blackProportion = sum(blackMask(:)) / numel(blackMask);
For given image the output is given below:
For better understanding of color recognition using HSV, refer to the given documentation:
Hope that Helps!

제품


릴리스

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by