S
S
SavaLLL2015-11-06 18:16:29
.NET
SavaLLL, 2015-11-06 18:16:29

How to find a red square on a gray background (C#)?

There is a jpg picture 300x200, gray background, it has a red square of about 10 pixels.
I need to find the center of this square.
Right now I'm implementing it like this:

1. Pixel by pixel looking for points close to red.
2. I find the leftmost-top pixel and the right-most-bottom pixel.
3. Then I add their coordinates and divide them in half.

But it turns out too long, and eats a lot of resources. (There are a lot of such pictures in several streams)
How else can I solve my problem? Or what amendments can be made to the existing algorithm?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
poiuy7, 2015-11-06
@SavaLLL

The task is solved in one pass through the picture (ie 200 * 300 operations) is that right for you?
You can first make a pass with a certain step, for example 3 (this will give an increase of 9 times)
when the first point is found, then scan only the local area (do not go further)
But I think that most of your time is spent not on searching, but on converting jpg to object (array) for further processing
I also wanted to add that if the quality of the picture is not very good (with artifacts), then your algorithm may not give correct answers.
It is more reliable to work with projections. That is:
1) For the first pass, we make the projection (sum) of the red gradient on the X and Y axes
2) Then we average the projections (for example, with a window of 5, smaller than the size of a possibly square)
3) We are looking for the average among the maxima of the projections
This is from a computational point of view more than yours (but not by much), but a more reliable method.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question