Selecting dark areas of an image
10 views (last 30 days)
Show older comments
I have a question on how to upload an image, and then get matlab to select areas of the image that are darker in color. The image has a colors from yellow to black. I would like to get the program to select the black areas, and draw an elipse around it.
4 Comments
Image Analyst
on 13 Dec 2018
Not exactly sure which region you're referring to. Can you attach another image where you've outlined the region of interest in red?
Answers (1)
Mark Sherstan
on 12 Dec 2018
Edited: Mark Sherstan
on 12 Dec 2018
Try the following. I just put it together quickly (results on the bottom). You will need to adjust the tolerances of channel 3 to meet the requirments of your data. From there you can use bwareafilt, regionprops and insertObjectAnnotation to draw your circles. I draw a circle around the largest blob (according to the random channel 3 - value settings).
I = imread('image.png');
[BW,maskedRGBImage] = createMask(I);
BW2 = bwareafilt(BW,1);
stats = regionprops(BW2,'Centroid');
RGB = insertObjectAnnotation(I,'circle',[stats.Centroid 20],'','Color','cyan');
imshow(RGB)
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 12-Dec-2018
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.000;
channel1Max = 1.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.000;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 0.275;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end

0 Comments
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!