Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Fitting lines to rectangle edges

Subject: Fitting lines to rectangle edges

From: Jonas

Date: 9 Sep, 2009 15:26:19

Message: 1 of 12

Hello,

I have a simple binary image containing a white rectangle (filled) somewhere. What I need to do now is detect the corners of this rectangle. I'd like to do this by some kind of fitting four lines to the four edges of the rectangle, but i cannot figure out, how. Can I do this using polyfit or spline? Or is there another way?
I tried using a Harris corner detector, but it returns way too many corners.

Any other ideas? As long as it safely detects the four corners anything is fine.

Thanks for the help,
Jonas

Subject: Fitting lines to rectangle edges

From: Dave Brackett

Date: 9 Sep, 2009 16:22:17

Message: 2 of 12

"Jonas " <not@set.com> wrote in message <h88her$r86$1@fred.mathworks.com>...
> Hello,
>
> I have a simple binary image containing a white rectangle (filled) somewhere. What I need to do now is detect the corners of this rectangle. I'd like to do this by some kind of fitting four lines to the four edges of the rectangle, but i cannot figure out, how. Can I do this using polyfit or spline? Or is there another way?
> I tried using a Harris corner detector, but it returns way too many corners.
>
> Any other ideas? As long as it safely detects the four corners anything is fine.
>
> Thanks for the help,
> Jonas

is there anything else in the image apart from the rectangle? an example image would be useful.

Subject: Fitting lines to rectangle edges

From: Jonas

Date: 9 Sep, 2009 17:13:21

Message: 3 of 12

Hi, thanks for your answer!

> is there anything else in the image apart from the rectangle? an example image would be useful.

No, it's only the rectangle. I eliminated all other content beforehand so that only the relevant content remains
I uploaded some iimages to tinypic for explanation:

http://i32.tinypic.com/2mcvjnc.jpg
This is the rectangle

http://i32.tinypic.com/1124zmq.jpg
and this is the result i'd like to get... four lines with their intersections specifying the corners of the rectangle.

Thanks for helping,
Jonas

Subject: Fitting lines to rectangle edges

From: Dave Brackett

Date: 9 Sep, 2009 18:06:20

Message: 4 of 12

"Jonas " <not@set.com> wrote in message <h88nnh$ouf$1@fred.mathworks.com>...
> Hi, thanks for your answer!
>
> > is there anything else in the image apart from the rectangle? an example image would be useful.
>
> No, it's only the rectangle. I eliminated all other content beforehand so that only the relevant content remains
> I uploaded some iimages to tinypic for explanation:
>
> http://i32.tinypic.com/2mcvjnc.jpg
> This is the rectangle
>
> http://i32.tinypic.com/1124zmq.jpg
> and this is the result i'd like to get... four lines with their intersections specifying the corners of the rectangle.
>
> Thanks for helping,
> Jonas


This file exchange submission will find the corners of the rectangle and give you their location: http://www.mathworks.com/matlabcentral/fileexchange/7652. You just run the file directly and select your input image file.

It also outputs an edge map plotting your rectangle, but you could recreate this by plotting lines between the corner points.

Hope that helps.

Subject: Fitting lines to rectangle edges

From: Andrew

Date: 9 Sep, 2009 19:12:04

Message: 5 of 12

% This is trickier with the jpg because of compression, and because of the
% white border around you image. The first half of this is just cropping
% and thresholding.

% Assuming you have a true binary image, this is a dirty,
% dirty way to do it, but should work, without any special toolboxes

% Approach:
% use [i1, i2] = find(C, 1, 'first') to find the column of the corner i2
% use i1 = find(C(:, i2), 1, 'last') to find the row i1
% and keep rotating the image using rot90

clear;
close all;

% A = imread('2mcvjnc.jpg');
figure;
hA = image(A);

B = sum(A, 3)>128; % dirty threshold for uint8; B is logical
figure;
hB = imagesc(B);
colormap('gray');
[i1_first, i2_first] = find(~B, 1, 'first');
[i1_last, i2_last] = find(~B, 1, 'last');

C = B(i1_first:i1_last, i2_first:i2_last);
figure;
imagesc(C);
colormap('gray');
hold all;

% All of the preceeding was just to get the cropped binary image I assume
% you're starting with.

[m, n] = size(C);

[a1, a2] = find(C, 1, 'first');
a1 = find(C(:, a2), 1, 'last');
C = rot90(C);

[b2, b1] = find(C, 1, 'first'); % note the index reversal
b2 = find(C(:, b1), 1, 'last');
b2 = n-b2; % compensate indices for rot90
C = rot90(C);

[c1, c2] = find(C, 1, 'first');
c1 = find(C(:, c2), 1, 'last');
c1 = m-c1; c2 = n-c2;
C = rot90(C);

[d2, d1] = find(C, 1, 'first'); % reversed again
d2 = find(C(:, d1), 1, 'last');
d1 = m-d1;
C = rot90(C);

plot([a2, b2, c2, d2], [a1, b1, c1, d1], '+');
% note reversal due to the way image plots matrix rows as columns

% Obviously, you could clean this up quite a bit.

--

"Jonas " <not@set.com> wrote in message <h88nnh$ouf$1@fred.mathworks.com>...
> Hi, thanks for your answer!
>
> > is there anything else in the image apart from the rectangle? an example image would be useful.
>
> No, it's only the rectangle. I eliminated all other content beforehand so that only the relevant content remains
> I uploaded some iimages to tinypic for explanation:
>
> http://i32.tinypic.com/2mcvjnc.jpg
> This is the rectangle
>
> http://i32.tinypic.com/1124zmq.jpg
> and this is the result i'd like to get... four lines with their intersections specifying the corners of the rectangle.
>
> Thanks for helping,
> Jonas

Subject: Fitting lines to rectangle edges

From: Jonas

Date: 10 Sep, 2009 11:39:04

Message: 6 of 12

"Dave Brackett" <davebrackett@hotmail.com> wrote in message <h88qqs$jjf$1@fred.mathworks.com>...

> This file exchange submission will find the corners of the rectangle and give you their location: http://www.mathworks.com/matlabcentral/fileexchange/7652. You just run the file directly and select your input image file.
>
> It also outputs an edge map plotting your rectangle, but you could recreate this by plotting lines between the corner points.
>
> Hope that helps.

Hi,
thanks a lot for that answer, this corner detector works way better than the Harris one I used before... awesome. How come I didn't find the code before?

Anyways, consider this solved...

Thanks to everyone,
Jonas

Subject: Fitting lines to rectangle edges

From: Jonas

Date: 10 Sep, 2009 11:45:06

Message: 7 of 12

"Andrew " <|a|r|i|c|8|2|@hotmail.com> wrote in message <h88um4$896$1@fred.mathworks.com>...
> % This is trickier with the jpg because of compression, and because of the
> % white border around you image. The first half of this is just cropping
> % and thresholding.
>
> % Assuming you have a true binary image, this is a dirty,
> % dirty way to do it, but should work, without any special toolboxes
>
> % Approach:
> % use [i1, i2] = find(C, 1, 'first') to find the column of the corner i2
> % use i1 = find(C(:, i2), 1, 'last') to find the row i1
> % and keep rotating the image using rot90
>
> clear;
> close all;
>
> % A = imread('2mcvjnc.jpg');
> figure;
> hA = image(A);
>
> B = sum(A, 3)>128; % dirty threshold for uint8; B is logical
> figure;
> hB = imagesc(B);
> colormap('gray');
> [i1_first, i2_first] = find(~B, 1, 'first');
> [i1_last, i2_last] = find(~B, 1, 'last');
>
> C = B(i1_first:i1_last, i2_first:i2_last);
> figure;
> imagesc(C);
> colormap('gray');
> hold all;
>
> % All of the preceeding was just to get the cropped binary image I assume
> % you're starting with.
>
> [m, n] = size(C);
>
> [a1, a2] = find(C, 1, 'first');
> a1 = find(C(:, a2), 1, 'last');
> C = rot90(C);
>
> [b2, b1] = find(C, 1, 'first'); % note the index reversal
> b2 = find(C(:, b1), 1, 'last');
> b2 = n-b2; % compensate indices for rot90
> C = rot90(C);
>
> [c1, c2] = find(C, 1, 'first');
> c1 = find(C(:, c2), 1, 'last');
> c1 = m-c1; c2 = n-c2;
> C = rot90(C);
>
> [d2, d1] = find(C, 1, 'first'); % reversed again
> d2 = find(C(:, d1), 1, 'last');
> d1 = m-d1;
> C = rot90(C);
>
> plot([a2, b2, c2, d2], [a1, b1, c1, d1], '+');
> % note reversal due to the way image plots matrix rows as columns
>
> % Obviously, you could clean this up quite a bit.

Hi, thanks for your idea!
Unfortunately I can think of a szenario where this will fail, i forgot to mention that this rectangle can be slightly slanted in some cases... but thanks, i would have never thought about such a simple approach.

Jonas

Subject: Fitting lines to rectangle edges

From: arich82

Date: 19 Sep, 2009 07:38:03

Message: 8 of 12

Jonas--

<quote>
> Unfortunately I can think of a szenario where this will fail, i forgot to mention that this rectangle can be slightly slanted in some cases... but thanks, i would have never thought about such a simple approach.
</quote>

Not sure if you're still interested in this thread, but if you are, could you post another image of the case you're concerned about? This scheme, though ugly, seems to work for me with both rotated rectangles and skewed parallelograms.


--

Subject: Fitting lines to rectangle edges

From: muge Pirtini

Date: 16 Dec, 2009 18:16:19

Message: 9 of 12

I couldn't the file. Do you know where the file is?

 http://www.mathworks.com/matlabcentral/fileexchange/7652

Thanks


"Dave Brackett" <davebrackett@hotmail.com> wrote in message <h88qqs$jjf$1@fred.mathworks.com>...
> "Jonas " <not@set.com> wrote in message <h88nnh$ouf$1@fred.mathworks.com>...
> > Hi, thanks for your answer!
> >
> > > is there anything else in the image apart from the rectangle? an example image would be useful.
> >
> > No, it's only the rectangle. I eliminated all other content beforehand so that only the relevant content remains
> > I uploaded some iimages to tinypic for explanation:
> >
> > http://i32.tinypic.com/2mcvjnc.jpg
> > This is the rectangle
> >
> > http://i32.tinypic.com/1124zmq.jpg
> > and this is the result i'd like to get... four lines with their intersections specifying the corners of the rectangle.
> >
> > Thanks for helping,
> > Jonas
>
>
> This file exchange submission will find the corners of the rectangle and give you their location: http://www.mathworks.com/matlabcentral/fileexchange/7652. You just run the file directly and select your input image file.
>
> It also outputs an edge map plotting your rectangle, but you could recreate this by plotting lines between the corner points.
>
> Hope that helps.

Subject: Fitting lines to rectangle edges

From: Doug Hull

Date: 16 Dec, 2009 19:50:06

Message: 10 of 12

"Jonas " <not@set.com> wrote in message <h88nnh$ouf$1@fred.mathworks.com>...
> Hi, thanks for your answer!
>
> > is there anything else in the image apart from the rectangle? an example image would be useful.
>
> No, it's only the rectangle. I eliminated all other content beforehand so that only the relevant content remains

Wouldn't the corners always be the uppermost, lowermost, leftmost and rightmost pixels (except in the degenerate case?)

From there, the rest is easy.

Doug

Subject: Fitting lines to rectangle edges

From: deep bhimani

Date: 31 Jan, 2012 15:46:10

Message: 11 of 12

"Dave Brackett" wrote in message <h88qqs$jjf$1@fred.mathworks.com>...
> "Jonas " <not@set.com> wrote in message <h88nnh$ouf$1@fred.mathworks.com>...
> > Hi, thanks for your answer!
> >
> > > is there anything else in the image apart from the rectangle? an example image would be useful.
> >
> > No, it's only the rectangle. I eliminated all other content beforehand so that only the relevant content remains
> > I uploaded some iimages to tinypic for explanation:
> >
> > http://i32.tinypic.com/2mcvjnc.jpg
> > This is the rectangle
> >
> > http://i32.tinypic.com/1124zmq.jpg
> > and this is the result i'd like to get... four lines with their intersections specifying the corners of the rectangle.
> >
> > Thanks for helping,
> > Jonas
>
>
> This file exchange submission will find the corners of the rectangle and give you their location: http://www.mathworks.com/matlabcentral/fileexchange/7652. You just run the file directly and select your input image file.
>
> It also outputs an edge map plotting your rectangle, but you could recreate this by plotting lines between the corner points.
>
> Hope that helps.

Subject: Fitting lines to rectangle edges

From: deep bhimani

Date: 31 Jan, 2012 15:47:10

Message: 12 of 12

"Dave Brackett" wrote in message <h88qqs$jjf$1@fred.mathworks.com>...
> "Jonas " <not@set.com> wrote in message <h88nnh$ouf$1@fred.mathworks.com>...
> > Hi, thanks for your answer!
> >
> > > is there anything else in the image apart from the rectangle? an example image would be useful.
> >
> > No, it's only the rectangle. I eliminated all other content beforehand so that only the relevant content remains
> > I uploaded some iimages to tinypic for explanation:
> >
> > http://i32.tinypic.com/2mcvjnc.jpg
> > This is the rectangle
> >
> > http://i32.tinypic.com/1124zmq.jpg
> > and this is the result i'd like to get... four lines with their intersections specifying the corners of the rectangle.
> >
> > Thanks for helping,
> > Jonas
>
>
> This file exchange submission will find the corners of the rectangle and give you their location: http://www.mathworks.com/matlabcentral/fileexchange/7652. You just run the file directly and select your input image file.
>
> It also outputs an edge map plotting your rectangle, but you could recreate this by plotting lines between the corner points.
>
> Hope that helps.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us