How can I detect black color and mark bounding box from overall gray image?

8 views (last 30 days)
Alexai on 23 Feb 2022
Edited: Image Analyst on 24 Feb 2022
I wanna detect black color from gray image and mark like this way. How can I detect like this. please know me..

Accepted Answer

KALYAN ACHARJYA on 23 Feb 2022
Foremost segment that Black ROI regions, then apply the following-
#1 It can be done using regionprops
#2 To add the bounding box use rectangle function.
Please try, it is absolutely possible.

More Answers (1)

Image Analyst
Image Analyst on 23 Feb 2022
What does "detect" mean to you. You can easily threshold it, like find pixels darker than, say, 20 by doing
mask = grayImage < 20;
Now what? What do you want to do now that it's been detected. Maybe get its Area and Centroid?
props = regionprops(mask, 'Area', 'Centroid');
You'll want to look at my Image Segmentation Tutorial for a quick start lesson:
Image Analyst
Image Analyst on 24 Feb 2022
You never tried my tutorial, did you? OK, so here it is adapted for your image:
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 22;
markerSize = 40;
folder = pwd;
baseFileName = 'Screenshot_20220224-200026_Samsung Notes.jpg';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Extract the blue channel.
grayImage = rgbImage(:, :, 3);
grayImage = rgbImage;
% Display the image.
subplot(2, 2, 1);
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
% Maximize window.
g = gcf;
g.WindowState = 'maximized'
% Get a histogram
subplot(2, 2, [2, 4]);
grid on;
title('Histogram of Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Take the histogram
counts = histcounts(grayImage);
% Set thresholds for each level.
lowThreshold = 0;
% Get the triangle threshold for the upper threshold
highThreshold = 150;
% Interactively and visually set a threshold on a gray scale image.
% [lowThreshold, highThreshold] = threshold(lowThreshold, highThreshold, grayImage)
% Draw threshold line over histogram.
xline(highThreshold, 'Color', 'r', 'LineWidth', 2);
% Binarize the image to get a mask.
mask = grayImage >= lowThreshold & grayImage <= highThreshold;
% Display mask image.
subplot(2, 2, 3);
hold on;
axis('on', 'image');
caption = sprintf('Mask with Threshold = %d gray levels', highThreshold);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
% Take the blob closest to the center of the image.
props = regionprops(mask, 'Centroid', 'BoundingBox');
for k = 1 : length(props)
xCentroid = props(k).Centroid(1);
yCentroid = props(k).Centroid(2);
rectangle('Position', props(k).BoundingBox, 'EdgeColor', 'r', 'LineWidth', 2);
str = sprintf('Centroid = (%.2f, %.2f)', xCentroid, yCentroid);
text(xCentroid, yCentroid, str, 'Color', 'r', 'FontSize', 14, 'FontWeight','bold');
% Tell the user
message = sprintf('Done!\n')

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!