How to move a double dimensional array along an image?
Hi all,
I am currently using template matching technique where a mouth template with the size VS2xUS2 needs to be move pixel by pixel in a clipped region with bigger size height1xwidth1 so that correlation value for each location can be calculated. The location with highest correlation will be defined as mouth region.
Code:
//binary[height1][width1] is the array for the clipped mouth region from the input image.
/* This loop is the sliding window size VS2xUS2 to be moved along the clipped mouth region height1xwidth1 */
for(hp=0; hp<VS2; hp++){
for(hj=0; hj<US2; hj++){
binary1[hp][hj] = binary[hp][hj];
}
}
cross_correlation2(TEMPLATE2,binary1,&C2);
// The subroutine for correlation calculation where the array of TEMPLATE2//and binary1 are needed
//TEMPLATE2 is the TEMPLATE2[VS2][US2] for mouth template,
// VS2=40;US2=110.
//binary1[VS2][US2] is the sliding array that will be move around the clipped region with height1xwidth1.
I have no idea how to move this window(the loop above) pixel by pixel so that the cross_correlation() can calculate value for different location.
In order to make it faster and avoid to slide for each pixel, how to program so that the window will jump to 10th pixels (horizontally and vertically) after calculation from the 1st pixel position? This will save time as the calculation will only be done after 10 pixels from the previous location.
Please help if you all can understand. Thank you very much.
Re: How to move a double dimensional array along an image?
Code:
for(i = 0; i < height_image - height_window; i += step_h)
{
for(j = 0; j < width_image - width_window; j+= step_w)
{
for(ii = 0; ii < height_window; ii++)
for(jj = 0; jj < width_window; jj++)
window[ii][jj] = image[i + ii][j + jj];
// your window is here
}
}
Both image and window are two dimensional arrays as image[height_image][width_image] and window[height_window][width_window]. step_h is the horizontal step, and step_w is the vertical step. You want them both as 10. At the location your window is here, you have a portion of image inside window. After comleting the two inner loops, this code jumps step_w columns on the same row, and starts another round. At the end of a row, it jumps step_h rows, and so on.
Last edited by mgrey; March 13th, 2008 at 06:51 PM.
Reason: fix typo
Hi my friend, I have checked by write out the window. Actually the window is the portion of the height1xwidth1 image. However, I found that, the loop works but the window is the same. That's why, the calculation of correlation have given me the same value. Did you see anything wrong in my code which will make the window not moving?
Re: How to move a double dimensional array along an image?
Here I attached the image and template where I used in my program. The file type I use is .ppm. I have changed it to .jpg in order to upload it. The program of my subroutine is:
Code:
void cross_correlation2(template2,image_out2,C2)
unsigned char template2[VS2][US2];
unsigned char image_out2[VS2][US2];
float *C2;
{
int i,j,flt1,flt2;
float ET,dT,VT;
float S,R,M;
float EI,dI,A;
float templ2[VS2][US2];
float img2[VS2][US2];
float cc,ccmax;
for(i=0; i<VS2; i++){
for(j=0; j<US2; j++){
templ2[i][j] = (float)(template2[i][j]) / 255.0;
img2[i][j] = (float)(image_out2[i][j]) / 255.0;
}
}
S = R = 0.0;
for(i=0; i<VS2; i++){
for(j=0; j<US2; j++){
S = S + templ2[i][j];
R = R + POW[template2[i][j]];
}
}
ET = S/(float)(VS2*US2);
dT = (R/(float)(VS2*US2)) - (ET*ET);
if (dT > 0.0){
dT = sqrt(dT);
}
else{
dT = 0.0;
}
S = R = M = 0.0;
for(i=0; i<VS2; i++){
for(j=0; j<US2; j++){
S = S + img2[i][j];
R = R + POW[image_out2[i][j]];
M = M + (img2[i][j]*templ2[i][j]);
}
}
EI = S/(float)(VS2*US2);
dI = (R/(float)(VS2*US2)) - (EI*EI);
if (dI > 0.0){
dI = sqrt(dI);
}
else{
dI = 0.0;
}
A = M/(float)(VS2*US2);
// printf("A = %f, EI = %f, ET = %f, dI= %f, dT= %f,\n",A,EI,ET,dI,dT);
if((dT*dI) != 0.0){
cc = (A-EI*ET)/(dI*dT);
}
else{
cc = 0.0;
}
if(cc < 0.1) cc = 0.1;
*C2 = cc;
}
I keep on getting the same correlation value while moving the window but I fail to figure out what is happening. Anyone please feel free to point out my errors.
When I run it with height1=80, width1=142, VS2=40, US2=110, it hits the printf line a total of 16 times with i from 0 to 30 for each j from 0 to 30. Sorry but I can see no reason why the window should be always the same.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.