Modulus

I’m new to OF and OpenCV. I was reviewing some example code:(10. Color Tracking with OpenCV).

https://sites.google.com/site/ofauckland/examples/10-testing

The program uses seven images (3 across) within the main window. It splits RGB into HSV.
You then can use the mouse to pick a Hue value in one of the images.
It used the FindHue variable to find the correct Hue value and then uses that to find the contours.
It then can draw a filled circle on the very first image for a detected object.
I can’t figure out why the circles wind up on the first image.

My other question is: why exactly does the mouse pointer use modulo for mx and my?

void testApp::mousePressed(int x, int y, int button) {
//calculate local mouse x,y in image
int mx = x % w;
int my = y % h;

//get hue value on mouse position
findHue = hue.getPixels()[my*w+mx];
}

For example:
x = 432, y = 376
w = 320, h = 240
mx = 112, my = 136
FindHue = -842150451

I just can’t seem to get my head around it.
I understand the modulus operation in c++.
I also understand the reason for [my*w+mx]
Is it used to get back to the first image? The images are 240x320 each.
If I click on image #5 does the use of modulus reduce the pointer to image #1?
Thanks in advance.

I have not had a chance to look at the example, but I think I can explain the use of the modulus operator in this case.

The mouse position is read from the entire window which in this case is larger than the size of the images. For example, x = 432 would be outside the image width (w=320) and so would result in an error if used. The value needs to be changed to something within the range of the image. The modulo is a really easy way to make value loop into the range you want. So if I had an fixed array with only 3 values, I could use % 3 to make sure I only choose indexes of 0,1,3:

  
  
0 % 3 = 0  
1 % 3 = 1  
2 % 3 = 2  
3 % 3 = 0  
4 % 3 = 1  
5 % 3 = 2