필터 지우기
필터 지우기

detect the blue color

조회 수: 5 (최근 30일)
Shilpa
Shilpa 2024년 5월 6일
답변: Image Analyst 2024년 5월 7일
Below is the code which i used there is no error in the code but not detecting the blue color
% Capture image from Kinect sensor
v = videoinput("kinect", 1, "RGB_640x480");
Error using matlab.internal.capability.Capability.require (line 94)
This functionality is not available on remote platforms.

Error in imaqhwinfo (line 56)
Capability.require(Capability.LocalClient);

Error in videoinput (line 354)
hwinfo = imaqhwinfo;
v.ReturnedColorspace = "rgb";
src = getselectedsource(v);
src.CameraElevationAngle = 15;
snapshot4 = getsnapshot(v);
delete(v);
clear src v;
% Convert RGB image to LAB color space
labImage = rgb2lab(snapshot4);
% Extract the L-channel (luminance)
LChannel = labImage(:,:,1);
% Define a threshold for blue color in the L-channel
blueThresholdLow = 0.85; % Adjust as needed
blueThresholdHigh = 0.10; % Adjust as needed
% Create binary mask for blue regions
blueMask = (LChannel >= blueThresholdLow) & (LChannel <= blueThresholdHigh);
% Apply the blue mask to the original RGB image to get the refined blue objects
refinedBlueObjects = bsxfun(@times, snapshot4, cast(blueMask, 'like', snapshot4));
% Display the original image and the refined blue objects
figure;
subplot(1, 2, 1), imshow(snapshot4), title('Original Image');
subplot(1, 2, 2), imshow(refinedBlueObjects), title('Refined Blue Objects');
  댓글 수: 1
DGM
DGM 2024년 5월 6일
If you want to find blue regions, why are you thresholding L*? Without knowing how saturated the image is, why not threshold H in HSV?

댓글을 달려면 로그인하십시오.

답변 (2개)

Mann Baidi
Mann Baidi 2024년 5월 6일
편집: Mann Baidi 2024년 5월 6일
I am assuming you would like to detect the blue color in the image using the LAB Color space.
For detecting the blue color you need to extract the 'blue-yellow' color from image instead of 'Luminance'.
For resolving the query, you can replace
% Extract the L-channel (luminance)
LChannel = labImage(:,:,1);
with the following code:
% Extract the 'b' channel (blue-yellow) from the LAB color space
BChannel = labImage(:,:,3);
  댓글 수: 1
DGM
DGM 2024년 5월 6일
편집: DGM 2024년 5월 7일
Does it make sense to do thresholding in b* in order to discriminate hues? Do you know what colors you're actually selecting by doing that?
% the image (a 16x16x32 representation of sRGB)
inpict = imread('rgbcubesmaller_flat.png');
imshow(inpict,'border','tight')
% the color information being used
labpict = rgb2lab(inpict);
Bs = labpict(:,:,3);
% select the most "blue" fraction of sRGB along this axis
tol = 0.2; % the fraction
brange = [-107.9 94.48]; % nominal span of sRGB on this axis
mask = Bs < brange(1) + diff(brange)*tol;
% apply the mask to show which colors are actually being selected
outpict = inpict.*uint8(mask);
imshow(outpict,'border','tight')
Using H in HSV instead:
% the color information being used
hsvpict = rgb2hsv(inpict);
H = hsvpict(:,:,1);
% select the most "blue" fraction of sRGB along this axis
tol = 0.2; % the fraction
bcenter = 2/3; % position of blue in H
mask = (H < bcenter + tol/2) & (H > bcenter - tol/2);
% apply the mask to show which colors are actually being selected
outpict = inpict.*uint8(mask);
imshow(outpict,'border','tight')
Note that H extends to the neutral axis, whereas selecting the extremes of opponent color axes like b* just gets the highest-chroma regions in the direction of -b*, which isn't the same as selecting "blue" colors. Although we have no idea what the image exposure looks like, I would think that inclusion of neutral colors might be important for the selection of a luminous object in the field of view. If overexposure makes that necessary, it's not something that's practically done in b* alone.
If you want to discriminate hues in LAB or LUV or YCbCr or whatever, I don't see the point in avoiding the polar conversion. It's not like we're starved for computational capacity in hardware. If we were, we probably wouldn't be using LAB to begin with. Just use H in LCHab instead of trying to use b* as a proxy for blue or hue.
... or better yet, don't try to discriminate hues around blue in LAB. Use something like OKLAB or SRLAB:
Of course, all of this is probably extreme overkill for object tracking. We're also only utilizing one color component for the mask generation.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2024년 5월 7일
Use the Color Thresholder app to adjust thresholds. It's on the Apps tab of the tool ribbon. Tell it to use HSV color space, then adjust your threshold sliders, and finally tell it to export the function for you to build into your main program. Pretty easy but let me know if you can't figure it out.

Community Treasure Hunt

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

Start Hunting!

Translated by