Finding black region of an image

3 次查看(过去 30 天)
Hi I have an image like what I have attached (14.png) and I want to detect the black and white rectangles.
I have written a code to detect white rectangle like following:
img1 = imread('14.png');
img1r = imresize(img1,[240,320]);
subplot(1,2,1)
imshow(img1r)
title('Original&Resized Image')
img1rgray=im2gray(img1r);
white1 = img1rgray > 240; % Detect white parts of an image
maskwhite1 = bwareaopen(white1, 100); % Get rid of small blobs 100 pixels or smaller.
maskwhite1center = regionprops(maskwhite1, 'Centroid'); % Find centroids of what's left
subplot(1,2,2)
imshow(maskwhite1)
title('White Region and Its Center')
hold on
plot(maskwhite1center.Centroid(1),maskwhite1center.Centroid(2), 'r+', 'MarkerSize', 30, 'LineWidth', 2);
maskwhite1area = regionprops(maskwhite1, 'area');
fprintf('Area of White Part is: %d',maskwhite1area.Area);
It outputs area and center of the white region as I want it. How can I do it for black rectangle? Utilizing the same algorithms as above doesn't give good answer for black region... Any suggestions?

回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by