Selecting dark areas of an image

10 views (last 30 days)
justin kain
justin kain on 14 Nov 2018
Commented: Image Analyst on 13 Dec 2018
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
justin kain
justin kain on 15 Nov 2018
ImageLog.jpg
This is not the image, but a similar image to this one, Selecting dark regions like the one in the middle towards the bottom.
Image Analyst
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?

Sign in to comment.

Answers (1)

Mark Sherstan
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

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!