Detecting rectangle shape in an image

120 ビュー (過去 30 日間)
Natsu
Natsu 2012 年 4 月 12 日
コメント済み: Image Analyst 2019 年 11 月 15 日
Hi, I would like to know is it possible to detect a certain image feature and crop it from the original image? For example, I would like MATLAB to automatically detect the rectangle area marked by the red color in the image below, then crop that part only. Any help would be appreciated.
  1 件のコメント
Ashish Uthama
Ashish Uthama 2012 年 4 月 12 日
Natsu, it helps to provide a lot more detail to get a clear answer. For example, will all your images have the exact same orientation and size? (In which case, you can could hard code the edges after one manual attempt), or at least have a better estimate to feed into your detection step.

サインインしてコメントする。

回答 (2 件)

Yunas Qazi
Yunas Qazi 2013 年 10 月 3 日
編集済み: Yunas Qazi 2013 年 10 月 3 日
img = imread('rect.jpg');
bw = im2bw(img);
% find both black and white regions
stats = [regionprops(bw); regionprops(not(bw))]
% show the image and draw the detected rectangles on it
imshow(bw);
hold on;
for i = 1:numel(stats)
rectangle('Position', stats(i).BoundingBox, ...
'Linewidth', 3, 'EdgeColor', 'r', 'LineStyle', '--');
end
Reference : Stackover flow
  2 件のコメント
Mehul Bhardwaj
Mehul Bhardwaj 2017 年 5 月 2 日
How exactly are the rectangles being identified? What information is being stored in 'stats' after this command?
stats= [regionprops(bw);regionprops(not(bw))]
Image Analyst
Image Analyst 2017 年 5 月 2 日
All the shape information about all black or white objects. The problem is that you don't know where in the list is the dividing line between white object information and black object information. I recommend you don't use that code.

サインインしてコメントする。


Image Analyst
Image Analyst 2012 年 4 月 12 日
I'd probably try this
  1. threshold to get all (dark and light rectangles)
  2. do a hole file
  3. do an imclose with a vertical kernel to get rid of spurs out to the left
  4. Threshold again to get only the dark rectangle
  5. hole fill
  6. subtract (or NAND) that against your first (larger) rectangle
There are other ways to do it. You could even use hough() or houghlines() or corner() or other methods.
  5 件のコメント
Mussarat Begam
Mussarat Begam 2019 年 11 月 15 日
Image Analyst please guide on this.I shall be very grateful to you
Image Analyst
Image Analyst 2019 年 11 月 15 日
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
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;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
%===============================================================================
% Read in a standard MATLAB color demo image.
folder = pwd;
baseFileName = 'gridOfNumbers.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
grayImage = rgb2gray(grayImage);
end
% Display the original color image.
subplot(2, 3, 1);
imshow(grayImage);
axis on;
title('Original Image', 'FontSize', fontSize);
impixelinfo;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.1, 1, 0.9]);
subplot(2, 3, 2);
imhist(grayImage);
grid on;
title('Histogram of Gray Scale Image', 'FontSize', fontSize);
% Get the binaryImage
binaryImage = grayImage < 222;
% Display the original color image.
subplot(2, 3, 3);
imshow(binaryImage);
axis on;
title('Binary Image', 'FontSize', fontSize);
binaryImage = imfill(binaryImage, 'holes');
subplot(2, 3, 4);
imshow(binaryImage);
title('Filled Binary Image', 'FontSize', fontSize);
% Do an opening in the vertical direction by 9 pixels
% to get rid of horizontal lines sticking outside the main box.
se = true(9, 1); % Initialize
binaryImage = imopen(binaryImage, se);
binaryImage = bwareafilt(binaryImage, 1); % Make sure there is only one blob left.
subplot(2, 3, 5);
imshow(binaryImage);
title('Final Binary Image', 'FontSize', fontSize);
% Find bounding box
props = regionprops(binaryImage, 'BoundingBox');
boundingBox = props.BoundingBox;
width = boundingBox(3)
height = boundingBox(4)
% Crop off the top, shaded portion of column headers,
% which are known to be a fixed 29% of the height of the box.
newHeight = height * (1 - 0.29)
% Make a new bounding box.
xLeft = boundingBox(1);
yTop = boundingBox(2);
boundingBox2 = [xLeft, yTop + 0.29 * height, width, newHeight];
% Crop the image
newGrayScaleImage = imcrop(grayImage, boundingBox2);
subplot(2, 3, 6);
imshow(newGrayScaleImage);
axis('on', 'image');
impixelinfo;
title('New Gray Scale Image', 'FontSize', fontSize);
0000 Screenshot.png

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by