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

2 次查看(过去 30 天)
I wanna detect black color from gray image and mark like this way. How can I detect like this. please know me..

采纳的回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2022-2-23
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.

更多回答(1 个)

Image Analyst
Image Analyst 2022-2-23
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:
  3 个评论
Alexai
Alexai 2022-2-24
Thank you so much but I want to know more. can you know me how can I detect a lot of bounding box like this photo.
Image Analyst
Image Analyst 2022-2-24
编辑:Image Analyst 2022-2-24
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;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
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);
uiwait(warndlg(errorMessage));
return;
end
end
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);
else
grayImage = rgbImage;
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(grayImage);
impixelinfo;
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
% Maximize window.
g = gcf;
g.WindowState = 'maximized'
drawnow;
%--------------------------------------------------------------------------------------------------------
% Get a histogram
subplot(2, 2, [2, 4]);
imhist(grayImage);
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.
% https://www.mathworks.com/matlabcentral/fileexchange/29372-thresholding-an-image?s_tid=srchtitle
% [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);
imshow(mask);
hold on;
impixelinfo;
axis('on', 'image');
drawnow;
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');
end
% Tell the user
message = sprintf('Done!\n')
uiwait(helpdlg(message))

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with Image Processing Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by