Straighten edges of black rectangle in binary image
Show older comments
I have a binary image that represents a 'rectangle'. The rectangle is not perfect because a top view of the box was taken (then converted to a binary image). My objective is to find four corner points of the black rectangle. In order for the corner function to work the edges must be completely straight.

clc; clear;
image = imread('0148pm.jpg');
g = rgb2gray(image)
level = graythresh(g);
binary = im2bw(image,level);
imwrite(binary,'imageBinary.jpg');
% Iinv = ~binary; %Invert your binary image
% Iinv = bwareaopen(Iinv,20); %Get rid of small areas (below your size criterion)
% I = ~Inv; %Invert back
imshow(I);
% fill = bwmorph(binary,'hbreak');
%
% f = bwmorph(fill,'majority');
%
% k = bwmorph(f,'close');
%corner algorithm///////////////////////////////////////////////////
% C = corner(k,'MinimumEigenvalue', 4)
% imtool(k);
% hold on
% plot(C(:,1), C(:,2), 'r*');
%end corner algorithm///////////////////////////////////////////////
3 Comments
Image Analyst
on 24 Apr 2013
What do you really want to do: find corners or straighten edges? Do you know it's possible to straighten the square without even finding the corners?
Kal
on 24 Apr 2013
Kal
on 24 Apr 2013
Edited: Image Analyst
on 24 Apr 2013
Accepted Answer
More Answers (2)
Image Analyst
on 24 Apr 2013
0 votes
Why not use hough() and look for line intersections? There is an example for it in the help.
Jeff E
on 24 Apr 2013
The following may be helpful in validating any solution you do end up finding. If the rectangles in your images are rotated to any significant degree, just change "diamond" into "disk" to get a more robust, but slightly more noisy result.
bw = im2bw(imread('w0lwro.jpg'));
bw = ~bwareaopen(~bw, 5000);
cmask = imclose(bw, strel('diamond', 20));
cornermask = cmask & ~bw;
cornermask = bwareaopen(cornermask, 50);
The result is a binary mask that should contain, or be very close to, your corners, either through corner point detection or hough transform as suggested by Image Analyst.
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




