473,387 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

GDI+ - finding a rectangle inside another rectangle

I have a situation where I'm getting in Image that has a gray (solid, same
color) background with a smaller white rectangle inside. The position is
not always the same. What I need to do is locate the postion and determine
the size fo the white rectangle and then crop the image to leave only the
white rectangle remaining.

I'm very new to GDI+ and have really no idea where to start. Can anyone
suggest a good way to accomplish this?

Thanks for any help,
Steve
Jul 19 '07 #1
10 3672

"sklett" <s@s.comha scritto nel messaggio
news:u8****************@TK2MSFTNGP04.phx.gbl...
>I have a situation where I'm getting in Image that has a gray (solid, same
color) background with a smaller white rectangle inside. The position is
not always the same. What I need to do is locate the postion and determine
the size fo the white rectangle and then crop the image to leave only the
white rectangle remaining.
But you know the position of all the rectangles?

--
Help The New .Net Site! http://www.devbox4.net
Jul 19 '07 #2

"Fabio" <zn*******@virgilio.itwrote in message
news:uj**************@TK2MSFTNGP04.phx.gbl...
>
"sklett" <s@s.comha scritto nel messaggio
news:u8****************@TK2MSFTNGP04.phx.gbl...
>>I have a situation where I'm getting in Image that has a gray (solid, same
color) background with a smaller white rectangle inside. The position is
not always the same. What I need to do is locate the postion and
determine the size fo the white rectangle and then crop the image to leave
only the white rectangle remaining.

But you know the position of all the rectangles?
There is just one rectangle that I'm interested in and I do not know the
position.

If you picture a solid color windows desktop with a single window located
*somewhere* on the desktop, I want to find the location and dimension of the
window. I don't know if that odd example makes sense, I hope it does.

>
--
Help The New .Net Site! http://www.devbox4.net

Jul 19 '07 #3

"sklett" <s@s.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>
"Fabio" <zn*******@virgilio.itwrote in message
news:uj**************@TK2MSFTNGP04.phx.gbl...
>>
"sklett" <s@s.comha scritto nel messaggio
news:u8****************@TK2MSFTNGP04.phx.gbl...
>>>I have a situation where I'm getting in Image that has a gray (solid,
same color) background with a smaller white rectangle inside. The
position is not always the same. What I need to do is locate the postion
and determine the size fo the white rectangle and then crop the image to
leave only the white rectangle remaining.

But you know the position of all the rectangles?

There is just one rectangle that I'm interested in and I do not know the
position.

If you picture a solid color windows desktop with a single window located
*somewhere* on the desktop, I want to find the location and dimension of
the window. I don't know if that odd example makes sense, I hope it does.
Divide and conquer... find the x-coordinate of the center of the screen.
Start at the top of the screen, iterating over the y-coordinate and
inspecting the color of each pixel. If you find any non-background pixels,
you'll have the top and bottom coordinates. Then average those two to get
the y-coordinate of the center of the window, and iterate across x. If the
entire center column was background, split the screen in two and test each
half. Repeat until you find the window.
>
>>
--
Help The New .Net Site! http://www.devbox4.net


Jul 19 '07 #4

"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl...
>
"sklett" <s@s.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>>
"Fabio" <zn*******@virgilio.itwrote in message
news:uj**************@TK2MSFTNGP04.phx.gbl...
>>>
"sklett" <s@s.comha scritto nel messaggio
news:u8****************@TK2MSFTNGP04.phx.gbl.. .

I have a situation where I'm getting in Image that has a gray (solid,
same color) background with a smaller white rectangle inside. The
position is not always the same. What I need to do is locate the
postion and determine the size fo the white rectangle and then crop the
image to leave only the white rectangle remaining.

But you know the position of all the rectangles?

There is just one rectangle that I'm interested in and I do not know the
position.

If you picture a solid color windows desktop with a single window located
*somewhere* on the desktop, I want to find the location and dimension of
the window. I don't know if that odd example makes sense, I hope it
does.

Divide and conquer... find the x-coordinate of the center of the screen.
Start at the top of the screen, iterating over the y-coordinate and
inspecting the color of each pixel. If you find any non-background
pixels, you'll have the top and bottom coordinates. Then average those
two to get the y-coordinate of the center of the window, and iterate
across x. If the entire center column was background, split the screen in
two and test each half. Repeat until you find the window.
Thanks for the reply Ben.

Your solution makes perfect sense, I need to research how to access the
pixels of an Image and I should be good to go. I know I've seen code before
that used unsafe code to work with pixels, but I think from yoru suggestion
I should need to do more than 2 passes.

Thanks again,
Steve
>
>>
>>>
--
Help The New .Net Site! http://www.devbox4.net



Jul 20 '07 #5
On Thu, 19 Jul 2007 16:51:06 -0700, sklett <s@s.comwrote:
Your solution makes perfect sense, I need to research how to access the
pixels of an Image and I should be good to go.
You can use Bitmap.GetPixel. It's slow, but it may be fast enough for
your purposes. You wouldn't want to use it if you needed to look at each
and every pixel, but in this case that shouldn't be necessary.
I know I've seen code before
that used unsafe code to work with pixels,
The fast way to work with Bitmap data is to use LockBits, which returns an
IntPtr. I'm not sure whether you need unsafe code to manipulate that or
not, since I haven't done that sort of thing in .NET yet.
but I think from yoru suggestion
I should need to do more than 2 passes.
I'm not sure what the second clause in the sentence has with the first.
That said...

"Divide and conquer" always implies multiple passes (or at least the
potential for multiple passes...when you're lucky, the algorithm requires
only a single pass :) ).

In this case, Ben is suggesting (I believe) that you "divide and conquer"
relative to finding the top and bottom of the rectangle. If the rectangle
you're looking for does not straddle the vertical line in the middle of
the containing rectangle, then you conceptually split the containing
rectangle into two halves on that vertical line, and run the search again.

Keep doing this until you find a vertical line that _does_ intersect the
rectangle you're looking for.

When you do find a vertical line that intersects the rectangle, then you
necessarily also have found the top and bottom Y coordinates of the
rectangle. Using those coordinates, select a horizontal line to scan (it
could be any line between the top and bottom, and Ben suggests simply
using the average of the top and bottom Y coordinates), which will in a
single pass across the containing rectangle tell you the left and right X
coordinates of the rectangle.

Note that the "divide and conquer" part of the algorithm should be done as
a breadth-first search. That is, rather than completely searching one
half, and then completely searching the other half, do the initial search
of the middle of each half first, and only if that fails to find the
rectangle would you do the "divide" part of the algorithm. The reason
being that if you do it depth-first, you have a 50/50 chance of having to
visit literally every pixel in the half that _doesn't_ contain the
rectangle, which is obviously counter to the whole point of doing the
search quickly. You might as well just start doing the vertical scans at
the left and work your way right.

I sure hope this isn't a homework assignment. I hate doing people's
homework for them. :)

Pete
Jul 20 '07 #6

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Thu, 19 Jul 2007 16:51:06 -0700, sklett <s@s.comwrote:
>Your solution makes perfect sense, I need to research how to access the
pixels of an Image and I should be good to go.

You can use Bitmap.GetPixel. It's slow, but it may be fast enough for
your purposes. You wouldn't want to use it if you needed to look at each
and every pixel, but in this case that shouldn't be necessary.
>I know I've seen code before
that used unsafe code to work with pixels,

The fast way to work with Bitmap data is to use LockBits, which returns an
IntPtr. I'm not sure whether you need unsafe code to manipulate that or
not, since I haven't done that sort of thing in .NET yet.
>but I think from yoru suggestion
I should need to do more than 2 passes.

I'm not sure what the second clause in the sentence has with the first.
That said...

"Divide and conquer" always implies multiple passes (or at least the
potential for multiple passes...when you're lucky, the algorithm requires
only a single pass :) ).

In this case, Ben is suggesting (I believe) that you "divide and conquer"
relative to finding the top and bottom of the rectangle. If the rectangle
you're looking for does not straddle the vertical line in the middle of
the containing rectangle, then you conceptually split the containing
rectangle into two halves on that vertical line, and run the search again.

Keep doing this until you find a vertical line that _does_ intersect the
rectangle you're looking for.

When you do find a vertical line that intersects the rectangle, then you
necessarily also have found the top and bottom Y coordinates of the
rectangle. Using those coordinates, select a horizontal line to scan (it
could be any line between the top and bottom, and Ben suggests simply
using the average of the top and bottom Y coordinates), which will in a
single pass across the containing rectangle tell you the left and right X
coordinates of the rectangle.

Note that the "divide and conquer" part of the algorithm should be done as
a breadth-first search. That is, rather than completely searching one
half, and then completely searching the other half, do the initial search
of the middle of each half first, and only if that fails to find the
rectangle would you do the "divide" part of the algorithm. The reason
being that if you do it depth-first, you have a 50/50 chance of having to
visit literally every pixel in the half that _doesn't_ contain the
rectangle, which is obviously counter to the whole point of doing the
search quickly. You might as well just start doing the vertical scans at
the left and work your way right.

I sure hope this isn't a homework assignment. I hate doing people's
homework for them. :)
Hi Peter,

Thanks for the great detailed post, I'm very clear on it now.
I assure this is not homework, those days are long, long gone.

I will post my code I end up using in case anyone else you like to see it.

Thanks again,
Steve
>
Pete

Jul 20 '07 #7
On 20 Jul., 03:44, "sklett" <s...@s.comwrote:
"Peter Duniho" <NpOeStPe...@nnowslpianmk.comwrote in message

news:op***************@petes-computer.local...


On Thu, 19 Jul 2007 16:51:06 -0700, sklett <s...@s.comwrote:
Your solution makes perfect sense, I need to research how to access the
pixels of an Image and I should be good to go.
You can use Bitmap.GetPixel. It's slow, but it may be fast enough for
your purposes. You wouldn't want to use it if you needed to look at each
and every pixel, but in this case that shouldn't be necessary.
I know I've seen code before
that used unsafe code to work with pixels,
The fast way to work with Bitmap data is to use LockBits, which returns an
IntPtr. I'm not sure whether you need unsafe code to manipulate that or
not, since I haven't done that sort of thing in .NET yet.
but I think from yoru suggestion
I should need to do more than 2 passes.
I'm not sure what the second clause in the sentence has with the first.
That said...
"Divide and conquer" always implies multiple passes (or at least the
potential for multiple passes...when you're lucky, the algorithm requires
only a single pass :) ).
In this case, Ben is suggesting (I believe) that you "divide and conquer"
relative to finding the top and bottom of the rectangle. If the rectangle
you're looking for does not straddle the vertical line in the middle of
the containing rectangle, then you conceptually split the containing
rectangle into two halves on that vertical line, and run the search again.
Keep doing this until you find a vertical line that _does_ intersect the
rectangle you're looking for.
When you do find a vertical line that intersects the rectangle, then you
necessarily also have found the top and bottom Y coordinates of the
rectangle. Using those coordinates, select a horizontal line to scan (it
could be any line between the top and bottom, and Ben suggests simply
using the average of the top and bottom Y coordinates), which will in a
single pass across the containing rectangle tell you the left and right X
coordinates of the rectangle.
Note that the "divide and conquer" part of the algorithm should be done as
a breadth-first search. That is, rather than completely searching one
half, and then completely searching the other half, do the initial search
of the middle of each half first, and only if that fails to find the
rectangle would you do the "divide" part of the algorithm. The reason
being that if you do it depth-first, you have a 50/50 chance of having to
visit literally every pixel in the half that _doesn't_ contain the
rectangle, which is obviously counter to the whole point of doing the
search quickly. You might as well just start doing the vertical scans at
the left and work your way right.
I sure hope this isn't a homework assignment. I hate doing people's
homework for them. :)

Hi Peter,

Thanks for the great detailed post, I'm very clear on it now.
I assure this is not homework, those days are long, long gone.

I will post my code I end up using in case anyone else you like to see it.

Thanks again,
Steve


Pete- Zitierten Text ausblenden -

- Zitierten Text anzeigen -- Zitierten Text ausblenden -

- Zitierten Text anzeigen -
Just in case you do decide to work with LockBits for performance
reasons, I found that this is a good place to start:
http://www.bobpowell.net/lockingbits.htm

hth,
Kevin Wienhold

Jul 20 '07 #8

"sklett" <s@s.comwrote in message
news:e7**************@TK2MSFTNGP03.phx.gbl...
>
"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl...
>>
"sklett" <s@s.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>>>
"Fabio" <zn*******@virgilio.itwrote in message
news:uj**************@TK2MSFTNGP04.phx.gbl...

"sklett" <s@s.comha scritto nel messaggio
news:u8****************@TK2MSFTNGP04.phx.gbl. ..

>I have a situation where I'm getting in Image that has a gray (solid,
>same color) background with a smaller white rectangle inside. The
>position is not always the same. What I need to do is locate the
>postion and determine the size fo the white rectangle and then crop the
>image to leave only the white rectangle remaining.

But you know the position of all the rectangles?

There is just one rectangle that I'm interested in and I do not know the
position.

If you picture a solid color windows desktop with a single window
located *somewhere* on the desktop, I want to find the location and
dimension of the window. I don't know if that odd example makes sense,
I hope it does.

Divide and conquer... find the x-coordinate of the center of the screen.
Start at the top of the screen, iterating over the y-coordinate and
inspecting the color of each pixel. If you find any non-background
pixels, you'll have the top and bottom coordinates. Then average those
two to get the y-coordinate of the center of the window, and iterate
across x. If the entire center column was background, split the screen
in two and test each half. Repeat until you find the window.
One very effective way to do this is to maintain a queue (i.e. LinkedList)
of Rectangles. Start with the whole image. Each time you bisect the image
without hitting the target, add both halves to the queue. Loop on the
queue. Then you can implement one additional optimization -- bisect each
rectangle along the shorter dimension, keeping the resulting halves at less
than a 2:1 aspect ratio.
>
Thanks for the reply Ben.

Your solution makes perfect sense, I need to research how to access the
pixels of an Image and I should be good to go. I know I've seen code
before that used unsafe code to work with pixels, but I think from yoru
suggestion I should need to do more than 2 passes.

Thanks again,
Steve
>>
>>>
--
Help The New .Net Site! http://www.devbox4.net



Jul 20 '07 #9

"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>
"sklett" <s@s.comwrote in message
news:e7**************@TK2MSFTNGP03.phx.gbl...
>>
"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl. ..
>>>
"sklett" <s@s.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl.. .

"Fabio" <zn*******@virgilio.itwrote in message
news:uj**************@TK2MSFTNGP04.phx.gbl...
>
"sklett" <s@s.comha scritto nel messaggio
news:u8****************@TK2MSFTNGP04.phx.gbl.. .
>
>>I have a situation where I'm getting in Image that has a gray (solid,
>>same color) background with a smaller white rectangle inside. The
>>position is not always the same. What I need to do is locate the
>>postion and determine the size fo the white rectangle and then crop
>>the image to leave only the white rectangle remaining.
>
But you know the position of all the rectangles?

There is just one rectangle that I'm interested in and I do not know
the position.

If you picture a solid color windows desktop with a single window
located *somewhere* on the desktop, I want to find the location and
dimension of the window. I don't know if that odd example makes sense,
I hope it does.

Divide and conquer... find the x-coordinate of the center of the screen.
Start at the top of the screen, iterating over the y-coordinate and
inspecting the color of each pixel. If you find any non-background
pixels, you'll have the top and bottom coordinates. Then average those
two to get the y-coordinate of the center of the window, and iterate
across x. If the entire center column was background, split the screen
in two and test each half. Repeat until you find the window.

One very effective way to do this is to maintain a queue (i.e. LinkedList)
of Rectangles. Start with the whole image. Each time you bisect the
image without hitting the target, add both halves to the queue. Loop on
the queue. Then you can implement one additional optimization -- bisect
each rectangle along the shorter dimension, keeping the resulting halves
at less than a 2:1 aspect ratio.
Yes, that is a good suggestion and I have already implemented my solution
that way, nice to know it's a sound decision! :)
Have a good weekend,
Steve
>
>>
Thanks for the reply Ben.

Your solution makes perfect sense, I need to research how to access the
pixels of an Image and I should be good to go. I know I've seen code
before that used unsafe code to work with pixels, but I think from yoru
suggestion I should need to do more than 2 passes.

Thanks again,
Steve
>>>
>
--
Help The New .Net Site! http://www.devbox4.net
>




Jul 20 '07 #10

"KWienhold" <he******@trashmail.netwrote in message
news:11**********************@57g2000hsv.googlegro ups.com...
On 20 Jul., 03:44, "sklett" <s...@s.comwrote:
>"Peter Duniho" <NpOeStPe...@nnowslpianmk.comwrote in message

news:op***************@petes-computer.local...


On Thu, 19 Jul 2007 16:51:06 -0700, sklett <s...@s.comwrote:
>Your solution makes perfect sense, I need to research how to access
the
pixels of an Image and I should be good to go.
You can use Bitmap.GetPixel. It's slow, but it may be fast enough for
your purposes. You wouldn't want to use it if you needed to look at
each
and every pixel, but in this case that shouldn't be necessary.
>I know I've seen code before
that used unsafe code to work with pixels,
The fast way to work with Bitmap data is to use LockBits, which returns
an
IntPtr. I'm not sure whether you need unsafe code to manipulate that
or
not, since I haven't done that sort of thing in .NET yet.
>but I think from yoru suggestion
I should need to do more than 2 passes.
I'm not sure what the second clause in the sentence has with the first.
That said...
"Divide and conquer" always implies multiple passes (or at least the
potential for multiple passes...when you're lucky, the algorithm
requires
only a single pass :) ).
In this case, Ben is suggesting (I believe) that you "divide and
conquer"
relative to finding the top and bottom of the rectangle. If the
rectangle
you're looking for does not straddle the vertical line in the middle of
the containing rectangle, then you conceptually split the containing
rectangle into two halves on that vertical line, and run the search
again.
Keep doing this until you find a vertical line that _does_ intersect
the
rectangle you're looking for.
When you do find a vertical line that intersects the rectangle, then
you
necessarily also have found the top and bottom Y coordinates of the
rectangle. Using those coordinates, select a horizontal line to scan
(it
could be any line between the top and bottom, and Ben suggests simply
using the average of the top and bottom Y coordinates), which will in a
single pass across the containing rectangle tell you the left and right
X
coordinates of the rectangle.
Note that the "divide and conquer" part of the algorithm should be done
as
a breadth-first search. That is, rather than completely searching one
half, and then completely searching the other half, do the initial
search
of the middle of each half first, and only if that fails to find the
rectangle would you do the "divide" part of the algorithm. The reason
being that if you do it depth-first, you have a 50/50 chance of having
to
visit literally every pixel in the half that _doesn't_ contain the
rectangle, which is obviously counter to the whole point of doing the
search quickly. You might as well just start doing the vertical scans
at
the left and work your way right.
I sure hope this isn't a homework assignment. I hate doing people's
homework for them. :)

Hi Peter,

Thanks for the great detailed post, I'm very clear on it now.
I assure this is not homework, those days are long, long gone.

I will post my code I end up using in case anyone else you like to see
it.

Thanks again,
Steve


Pete- Zitierten Text ausblenden -

- Zitierten Text anzeigen -- Zitierten Text ausblenden -

- Zitierten Text anzeigen -

Just in case you do decide to work with LockBits for performance
reasons, I found that this is a good place to start:
http://www.bobpowell.net/lockingbits.htm
Thanks Kevin, looks like a very good article, in fact looks like there is
lots of good stuff on this site!
Have a good one,
Steve
hth,
Kevin Wienhold

Jul 20 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Charles Law | last post by:
If I draw a rectangle, 6 inches by 2 inches, on a user control with a PageUnit of Inches, I get a rectangle of 7 by 2.2 inches. Is this what people would expect? I would have hoped that it was at...
3
by: Tommy Lang | last post by:
Hi, I need some help. I am trying to draw cards. I am using DrawRectagle() to draw upp the outlining of the card. Then I fill the card with white color FillRectangle(). Then with DrawString I can...
5
by: anonymous | last post by:
I'm writing a program that deals extensively with the printer. For the most part my application runs fine, but occasionally I run into some Exceptions. The most common exceptions I run into are...
3
by: Richard | last post by:
I have a requirement to put a GDI style circle or rectangle border around the selected row of a datagrid/ It will overlap into the row above and below the selected row. Doing this in a the OnPaint...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
7
by: Marcin Rzeznicki | last post by:
Hello, Do you think it is legitimate practice to mix GDI+ and GDI calls (via Get/ReleaseHDC()) in paint event of a control? I've heard there is possibility of performance loss while "locking"...
0
by: Brian Henry | last post by:
Ok I've never implemented a snap location before so I dont really know what im doing wrong here... anyways, I am making a custom slider control that takes dates as its values instead of integers......
3
by: Fabio | last post by:
Hi all! A question about transformations. Let's have this piece of code that transforms the coordinates of the Graphics object: --------- Graphics g = e.Graphics; Matrix m = g.Transform;
7
by: j4richard | last post by:
Help please, I am getting this "Unhandled Exception has occurred in your application" " A Generic error occurred in GDI+" See the end of this message for details on...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.