How can I detect black color and mark bounding box from overall gray image?
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Alexai
am 23 Feb. 2022
Bearbeitet: Image Analyst
am 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..
0 Kommentare
Akzeptierte Antwort
KALYAN ACHARJYA
am 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.
0 Kommentare
Weitere Antworten (1)
Image Analyst
am 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:
3 Kommentare
Image Analyst
am 24 Feb. 2022
Bearbeitet: Image Analyst
am 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;
%--------------------------------------------------------------------------------------------------------
% 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))

Siehe auch
Kategorien
Find more on Image Processing and Computer Vision in Help Center and File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!