Go to Post Something has gone awry this year in FIRST. I think more teams are concerned with winning and their robot that they don't even stop to think about the kids on their team, or the learning experiences of others. - Amanda Morrison [more]
Home
Go Back   Chief Delphi > FIRST > General Forum
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 20-08-2013, 23:59
Maxwell777's Avatar
Maxwell777 Maxwell777 is offline
Is allowed to touch stuff now
AKA: Jackson Gray
FRC #0842 (Falcon Robotics)
Team Role: Programmer
 
Join Date: Aug 2013
Rookie Year: 2014
Location: Phoenix, AZ
Posts: 72
Maxwell777 has a spectacular aura aboutMaxwell777 has a spectacular aura aboutMaxwell777 has a spectacular aura about
Question Need help and suggestions for image comparison method

Things to note:
I am using C++, Qt, and OpenCV.

Background info:
I am trying to create a program (actually a method) in C++ that takes two (filtered to two color black & white) images from a moving robot, one within X amount of time of the other, then trys to find the amount of shift between each frame. It does this by overlaying the second picture ontop of the first, in every possible placement, and comparing the overlapping area, pixel per pixel.

I've gotten all of the bugs out (to the extent of my knowledge), with the exception of one. There is a method that takes two images, one from the overlapping area of each frame, counts each matching pixel, and returns a "score" of how similar the two images that were passed in. My issue with this is the "score" returned. This score only returns the number of matching pixels. If I were to give it larger images, it would return a larger score than if I gave it smaller ones, even if the smaller ones were more similar. I've already tried dividing the score by the area of the images.

The program does that for many different images to figure out which is the best match, which is dictated by the "score".

I was wondering what the best way to calculate the "score" is, that would always give the pair of images with the most in common the best score, regardless of size or shape.

Here is the troublesome code:

double ImgCompare (Mat Img1, Mat Img2, int ImgWidth, int ImgHeight) { //both images are the same diamentions

double Score = 0;

double ImgArea = ImgWidth * ImgHeight;

for (int Y = -1; Y < abs(ImgHeight-2); Y=Y) {

for (int X = -1; X < abs(ImgWidth-2); X=X) {

if (Img1.at<double>(X,Y) == Img2.at<double>(X,Y) ) {

Score++;

}

X++;

}

Y++;

}

//printf("Score = %f\n", Score);
return(Score);

}


Sorry for the lack of indents

Would anyone like the code for the rest?

Last edited by Maxwell777 : 21-08-2013 at 12:45.
Reply With Quote
  #2   Spotlight this post!  
Unread 21-08-2013, 00:45
SoftwareBug2.0's Avatar
SoftwareBug2.0 SoftwareBug2.0 is offline
Registered User
AKA: Eric
FRC #1425 (Error Code Xero)
Team Role: Mentor
 
Join Date: Aug 2004
Rookie Year: 2004
Location: Tigard, Oregon
Posts: 486
SoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant future
Re: Need help and suggestions for image comparison method

Are you using black and white or "color" (greyscale) or full color?

Your can do something like the average absolute value of the difference between the pixels. If you're using b&w or "color" that should be easy to calculate. If you're doing actual full color, then the computation will be a little more complex since there's a few different options.

Incedentily, the increments of your for loops bother me: "X=X" really shouldn't do anything.
Reply With Quote
  #3   Spotlight this post!  
Unread 21-08-2013, 07:24
Greg McKaskle Greg McKaskle is offline
Registered User
FRC #2468 (Team NI & Appreciate)
 
Join Date: Apr 2008
Rookie Year: 2008
Location: Austin, TX
Posts: 4,752
Greg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond repute
Re: Need help and suggestions for image comparison method

And what was the problem with dividing by the area? That will give score a range from 0 -- when no pixels match, to 1.0 when all pixels match. That normalizes the score.

I think the bigger problem is that your goal is to determine the shift, and counting matching pixels doesn't always act as a good predictor of shifted distance.

Lets look at two examples.
If the image is simple ... a black background and a smaller white square within it. If I shift the square one pixel to the right, two rows of pixels will contain errors. If I shift it two pixels four rows have errors. It seems to work well. Once the shift amount is greater than the edge size of the square, the score plateaus -- not that helpful in determining shift amount.

In the second image, lets have stripes, like a picket fence. The stripes are two pixels wide with two pixels of black in between. Shift one pixel and half of the rows have errors. Shift two and all rows have errors. Shift three and half have errors again, four and no errors (depends on what enters the frame).

Anyway this difficult problem, and a simple score like you are computing works well in some cases, but less well in others.

After you've completed this approach, perhaps you want to try another based on feature extraction. It could find the largest particle, or the ten largest and compare positions of those. It could identify corners within the image and track those. You may also find it useful to read about Optical Flow algorithms. They are pretty sophisticated to implement, but they are pretty robust.

Greg McKaskle
Reply With Quote
  #4   Spotlight this post!  
Unread 21-08-2013, 12:11
Maxwell777's Avatar
Maxwell777 Maxwell777 is offline
Is allowed to touch stuff now
AKA: Jackson Gray
FRC #0842 (Falcon Robotics)
Team Role: Programmer
 
Join Date: Aug 2013
Rookie Year: 2014
Location: Phoenix, AZ
Posts: 72
Maxwell777 has a spectacular aura aboutMaxwell777 has a spectacular aura aboutMaxwell777 has a spectacular aura about
Re: Need help and suggestions for image comparison method

Quote:
Originally Posted by SoftwareBug2.0 View Post
Are you using black and white or "color" (greyscale) or full color?

Your can do something like the average absolute value of the difference between the pixels. If you're using b&w or "color" that should be easy to calculate. If you're doing actual full color, then the computation will be a little more complex since there's a few different options.

Incedentily, the increments of your for loops bother me: "X=X" really shouldn't do anything.
Sorry about those incrementations. I did it to keep the loop indexed at 0, instead of the loop starting with an x value of 1.

The images are in literal black & white, as in the pixles are either black, or they are white.
Reply With Quote
  #5   Spotlight this post!  
Unread 21-08-2013, 12:43
Maxwell777's Avatar
Maxwell777 Maxwell777 is offline
Is allowed to touch stuff now
AKA: Jackson Gray
FRC #0842 (Falcon Robotics)
Team Role: Programmer
 
Join Date: Aug 2013
Rookie Year: 2014
Location: Phoenix, AZ
Posts: 72
Maxwell777 has a spectacular aura aboutMaxwell777 has a spectacular aura aboutMaxwell777 has a spectacular aura about
Re: Need help and suggestions for image comparison method

Quote:
Originally Posted by Greg McKaskle View Post
And what was the problem with dividing by the area? That will give score a range from 0 -- when no pixels match, to 1.0 when all pixels match. That normalizes the score.

I think the bigger problem is that your goal is to determine the shift, and counting matching pixels doesn't always act as a good predictor of shifted distance.

Lets look at two examples.
If the image is simple ... a black background and a smaller white square within it. If I shift the square one pixel to the right, two rows of pixels will contain errors. If I shift it two pixels four rows have errors. It seems to work well. Once the shift amount is greater than the edge size of the square, the score plateaus -- not that helpful in determining shift amount.

In the second image, lets have stripes, like a picket fence. The stripes are two pixels wide with two pixels of black in between. Shift one pixel and half of the rows have errors. Shift two and all rows have errors. Shift three and half have errors again, four and no errors (depends on what enters the frame).

Anyway this difficult problem, and a simple score like you are computing works well in some cases, but less well in others.

After you've completed this approach, perhaps you want to try another based on feature extraction. It could find the largest particle, or the ten largest and compare positions of those. It could identify corners within the image and track those. You may also find it useful to read about Optical Flow algorithms. They are pretty sophisticated to implement, but they are pretty robust.

Greg McKaskle
When you divide the score by the area, I think (as in it's possible there's a bug elsewhere and I lied to you) the program then becomes focused on reducing the area to achieve a better score. I also tried reducing the area's influence (Score = matches / Area/2 ) of the total score, which didn't help.

This program is actually a dumb program, in that it takes every possible shift, and gives it a score, and whichever shift(shiftX, shiftY) has the best score is the shift that it considers to be right. It disregards all scores other than the best one, so it doesn't matter if the score plateaus.

Images the program are working with have almost no pattern whatsoever (it's looking at patches of moss on the bottom of a pool), so I'm not worried about things like you're fence example.

This program is actually an optical flow program, and this was the simplest system I could think of.
Reply With Quote
  #6   Spotlight this post!  
Unread 21-08-2013, 20:48
Greg McKaskle Greg McKaskle is offline
Registered User
FRC #2468 (Team NI & Appreciate)
 
Join Date: Apr 2008
Rookie Year: 2008
Location: Austin, TX
Posts: 4,752
Greg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond repute
Re: Need help and suggestions for image comparison method

I didn't tie this to the post from Freddi. While this is good for learning the effectiveness of various algorithms, but this brute force approach will be thousands of times slower than more modern approaches. If you can provide some video sequences of the pool bottom, it would allow for different approaches to be compared in order to identify the performance and robustness tradeoffs.

Greg McKaskle
Reply With Quote
  #7   Spotlight this post!  
Unread 21-08-2013, 23:27
Maxwell777's Avatar
Maxwell777 Maxwell777 is offline
Is allowed to touch stuff now
AKA: Jackson Gray
FRC #0842 (Falcon Robotics)
Team Role: Programmer
 
Join Date: Aug 2013
Rookie Year: 2014
Location: Phoenix, AZ
Posts: 72
Maxwell777 has a spectacular aura aboutMaxwell777 has a spectacular aura aboutMaxwell777 has a spectacular aura about
Re: Need help and suggestions for image comparison method

I was looking at other methods of achieving this, but I would still like to get this one working. As for performance, I found this system works just fine, only at a lower resolution, say 64px by 64px. I could also improve performance later on by looking at every Nth frame, and only checking the match hotspots.

I think I'll try other, already done, forms of optical flow for now however.
Reply With Quote
  #8   Spotlight this post!  
Unread 22-08-2013, 00:30
SoftwareBug2.0's Avatar
SoftwareBug2.0 SoftwareBug2.0 is offline
Registered User
AKA: Eric
FRC #1425 (Error Code Xero)
Team Role: Mentor
 
Join Date: Aug 2004
Rookie Year: 2004
Location: Tigard, Oregon
Posts: 486
SoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant future
Re: Need help and suggestions for image comparison method

Something you can try is to multiply the score of each match by an estimate of how likely that result should be. For example, if you think the robot should be moving slowly, then results that would imply large motions should be penalized.

One way to do this is to use normal distributions. For example, you might estimate that you should have moved 10 px forward since the last frame with a standard deviation of 3. From That, you can give an initial estimate of how likely having moved 8 px is vs 10 px vs 1 px. So the initial estimate would say 10 px of movement is likely and 8 px is reasonable and 1 px is unlikely because it's 3 standard deviations away from what was expected.

I hope that makes sense.
Reply With Quote
  #9   Spotlight this post!  
Unread 22-08-2013, 07:40
Greg McKaskle Greg McKaskle is offline
Registered User
FRC #2468 (Team NI & Appreciate)
 
Join Date: Apr 2008
Rookie Year: 2008
Location: Austin, TX
Posts: 4,752
Greg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond reputeGreg McKaskle has a reputation beyond repute
Re: Need help and suggestions for image comparison method

One thing you may want to do is to plot your result as an image. You have image A, and image B, and you are skipping a few steps but are essentially computing image C=B==A and then summing all pixels in C. It may be helpful to visualize C and see if there are issues around the edges, or see if your algorithm does something unexpected. You may also want to compare the current approach to using C= |B-A| or similar functions that aren't just binary.

Greg McKaskle
Reply With Quote
  #10   Spotlight this post!  
Unread 22-08-2013, 07:59
Unsung FIRST Hero
Al Skierkiewicz Al Skierkiewicz is offline
Broadcast Eng/Chief Robot Inspector
AKA: Big Al WFFA 2005
FRC #0111 (WildStang)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1996
Location: Wheeling, IL
Posts: 10,792
Al Skierkiewicz has a reputation beyond reputeAl Skierkiewicz has a reputation beyond reputeAl Skierkiewicz has a reputation beyond reputeAl Skierkiewicz has a reputation beyond reputeAl Skierkiewicz has a reputation beyond reputeAl Skierkiewicz has a reputation beyond reputeAl Skierkiewicz has a reputation beyond reputeAl Skierkiewicz has a reputation beyond reputeAl Skierkiewicz has a reputation beyond reputeAl Skierkiewicz has a reputation beyond reputeAl Skierkiewicz has a reputation beyond repute
Re: Need help and suggestions for image comparison method

A technique we have used for years in broadcast is to invert one of the images and then add. The result is only that which is different. Counting that should be fairly easy. If you are comparing pixel by pixel the overall number shouldn't change with image/zoom should it?
__________________
Good Luck All. Learn something new, everyday!
Al
WB9UVJ
www.wildstang.org
________________________
Storming the Tower since 1996.
Reply With Quote
  #11   Spotlight this post!  
Unread 22-08-2013, 13:18
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 8,581
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
Re: Need help and suggestions for image comparison method

Quote:
Originally Posted by Maxwell777 View Post
When you divide the score by the area, I think (as in it's possible there's a bug elsewhere and I lied to you) the program then becomes focused on reducing the area to achieve a better score. I also tried reducing the area's influence (Score = matches / Area/2 ) of the total score, which didn't help.
I'm confused. Why do you have images of different resolutions being scored and compared to each other?
Reply With Quote
  #12   Spotlight this post!  
Unread 22-08-2013, 22:42
SoftwareBug2.0's Avatar
SoftwareBug2.0 SoftwareBug2.0 is offline
Registered User
AKA: Eric
FRC #1425 (Error Code Xero)
Team Role: Mentor
 
Join Date: Aug 2004
Rookie Year: 2004
Location: Tigard, Oregon
Posts: 486
SoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant futureSoftwareBug2.0 has a brilliant future
Re: Need help and suggestions for image comparison method

Quote:
Originally Posted by Joe Ross View Post
I'm confused. Why do you have images of different resolutions being scored and compared to each other?
I think what he's saying is that to compare two images that are offset he's just comparing the region of overlap. So if the upper left pixel is black in the first image and the lower right pixel is black in the second image, one of the combinations he would try would consist of only those two pixels. Then, since they're both black there would be a 100% match.
Reply With Quote
  #13   Spotlight this post!  
Unread 22-08-2013, 23:07
Pault's Avatar
Pault Pault is offline
Registered User
FRC #0246 (Overclocked)
Team Role: College Student
 
Join Date: Jan 2013
Rookie Year: 2012
Location: Boston
Posts: 618
Pault has a reputation beyond reputePault has a reputation beyond reputePault has a reputation beyond reputePault has a reputation beyond reputePault has a reputation beyond reputePault has a reputation beyond reputePault has a reputation beyond reputePault has a reputation beyond reputePault has a reputation beyond reputePault has a reputation beyond reputePault has a reputation beyond repute
Re: Need help and suggestions for image comparison method

Quote:
Originally Posted by Joe Ross View Post
I'm confused. Why do you have images of different resolutions being scored and compared to each other?
They aren't different resolutions. This is best explained using visuals.

Here is the first picture:

Code:
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
And the second picture:

Code:
YYYYY
YYYYY
YYYYY
YYYYY
YYYYY
Now, he is trying to see how far the robot has moved is a given direction. His unit of measurment is pixels that the camera sees. He makes this measurement by overlapping the 2 pictures in every possible way. Here are 2 examples:

(Z is the overlap between the pictures)
Code:
XXXXX
XXXXX
XXXXX
XXXXX
ZZZZZ
YYYYY
YYYYY
YYYYY
YYYYY
Code:
XXXXX
ZZZZZ
ZZZZZ
ZZZZZ
ZZZZZ
YYYYY
In the case that example 1 is the right match, the robot has moved backwards 4 pixels. If example 2 is the right match, the robot has moved backwards 1 pixel. To figure out which overlap is the right match, he scores the overlap based on how many pixels in the overlapping areas match color. So lets look at the overlap from the 2 examples:

Code:
ZZZZZ
Code:
ZZZZZ
ZZZZZ
ZZZZZ
ZZZZZ
Now, lets say just that all of the pixels in example 1 match up, but only half of the pixels in example 2 match. You would probably think "example 1 is the right match", but it only has a score of 5, where as the second picture has a score of 10. That is where the different areas come in.

I may have went a little overboard with this explanation .
Reply With Quote
  #14   Spotlight this post!  
Unread 22-08-2013, 23:17
Maxwell777's Avatar
Maxwell777 Maxwell777 is offline
Is allowed to touch stuff now
AKA: Jackson Gray
FRC #0842 (Falcon Robotics)
Team Role: Programmer
 
Join Date: Aug 2013
Rookie Year: 2014
Location: Phoenix, AZ
Posts: 72
Maxwell777 has a spectacular aura aboutMaxwell777 has a spectacular aura aboutMaxwell777 has a spectacular aura about
Re: Need help and suggestions for image comparison method

Good Example. I shoulda put that up originally.
Reply With Quote
  #15   Spotlight this post!  
Unread 22-08-2013, 23:41
Maxwell777's Avatar
Maxwell777 Maxwell777 is offline
Is allowed to touch stuff now
AKA: Jackson Gray
FRC #0842 (Falcon Robotics)
Team Role: Programmer
 
Join Date: Aug 2013
Rookie Year: 2014
Location: Phoenix, AZ
Posts: 72
Maxwell777 has a spectacular aura aboutMaxwell777 has a spectacular aura aboutMaxwell777 has a spectacular aura about
Re: Need help and suggestions for image comparison method

Quote:
Originally Posted by SoftwareBug2.0 View Post
Something you can try is to multiply the score of each match by an estimate of how likely that result should be. For example, if you think the robot should be moving slowly, then results that would imply large motions should be penalized.

One way to do this is to use normal distributions. For example, you might estimate that you should have moved 10 px forward since the last frame with a standard deviation of 3. From That, you can give an initial estimate of how likely having moved 8 px is vs 10 px vs 1 px. So the initial estimate would say 10 px of movement is likely and 8 px is reasonable and 1 px is unlikely because it's 3 standard deviations away from what was expected.

I hope that makes sense.
I hadn't tried that yet. Maybe instead of using a solid estimate, I could use the current speed as a base point, and from there it would reduce the score for shifts as they get father away from the avg shift. Actually wait no. That would probably screw up the accuracy. I'll just go with yours
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 17:44.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi