точное распознавание цветов in matlab

To achieve precise color recognition in Matlab, the first step is to convert the RGB image into a color space that separates the intensity information from the color information. One commonly used color space for this purpose is the HSV color space.

Here's an example code that reads an image, converts it to the HSV color space, and applies a color threshold to detect red objects in the image:

main.m
% read the image
img = imread('sample.jpg');

% convert to the HSV color space
img_hsv = rgb2hsv(img);

% extract the H (hue) channel, which encodes color information
hue_channel = img_hsv(:,:,1);

% define the red color range in the hue channel (here, red is displayed as yellow because the hue range is circular)
red_range = hue_channel > 0.95 | hue_channel < 0.05;

% apply the color threshold to the image
img_red = img;
img_red(repmat(~red_range, [1 1 3])) = 0;

% display the original and detected images side by side
figure;
subplot(1,2,1); imshow(img); title('Original image');
subplot(1,2,2); imshow(img_red); title('Detected red objects');
650 chars
21 lines

This code can be adapted to detect objects of other colors as well, by adjusting the hue range in the color threshold.

gistlibby LogSnag