Clear Filters
Clear Filters

detect the blue color

6 views (last 30 days)
Shilpa
Shilpa on 6 May 2024
Answered: Image Analyst on 7 May 2024
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 Comment
DGM
DGM on 6 May 2024
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?

Sign in to comment.

Answers (2)

Mann Baidi
Mann Baidi on 6 May 2024
Edited: Mann Baidi on 6 May 2024
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 Comment
DGM
DGM on 6 May 2024
Edited: DGM on 7 May 2024
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.

Sign in to comment.


Image Analyst
Image Analyst on 7 May 2024
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!