To retrieve and display 2 or more nearest neighbors

asked 2013-06-13 00:15:54 -0500

Mansa gravatar image

updated 2019-10-12 12:38:45 -0500

Hi All,

Below is the program that find nearest patches between source and target images.I could retrive and display one match for the patch.How could i extend to find many other matches?

#include <cv.h>
#include <iostream>
#include <cxcore.h>
#include <highgui.h>
#include <time.h>

using namespace std;

void Brute_Force(IplImage *, IplImage *, int , int);
int PatchDist(IplImage *, IplImage *, int, int , int , int , int , int);
int minDist = INT_MAX;  
int minX_s = 0, minY_s = 0;
int minX_t = 0, minY_t = 0;
int main(int argc, char** argv[])

{
    int patchWidth=8, patchHeight=8;
    time_t start, stop;

    IplImage *img1 = cvLoadImage("E:/1.png");
    cvNamedWindow("Image1:",1);
    cvShowImage("Image1:",img1);

    IplImage *img2 = cvLoadImage("E:/2.png");
    cvNamedWindow("Image2:",1);
    cvShowImage("Image2:",img2);

    printf("Patch width: %d, Patch height: %d\n\n", patchWidth, patchHeight);
    time(&start);
    minDist = INT_MAX;
    CompareAllPatchesO_NxN(img1, img2, patchHeight, patchWidth);
    time(&stop);

      printf("\n Brute_Forcealgorithm:\n");
      printf("Patches with min distance %d are at %d,%d in source image and %d,%d in target image.\n", minDist, minX_s, minY_s, minX_t, minY_t);
      printf("Finished in about %.0f seconds. \n", difftime(stop, start)); 

      cvRectangle(img1, cvPoint(minX_s,minY_s), cvPoint(minX_s+patchWidth,minY_s+patchHeight), cvScalar(255, 255, 255), 1);
      cvRectangle(img2, cvPoint(minX_t,minY_t), cvPoint(minX_t+patchWidth,minY_t+patchHeight), cvScalar(255, 255, 255), 1);

      cvShowImage("Image1:",img1);
      cvShowImage("Image2:",img2);

      cvWaitKey();

      cvDestroyWindow("Image1:");
      cvDestroyWindow("Image2:");

      cvReleaseImage(&img1);
      cvReleaseImage(&img2);

      return 0;  
}

void CompareAllPatchesO_NxN(IplImage *img1, IplImage *img2, int patchHeight, int patchWidth)
{
    int i,j,k,l;
      int dist;

      int SWidth=img1->width-patchWidth+1;
      int SHeight=img1->height-patchHeight+1;

    int TWidth=img2->width-patchWidth+1; 
    int THeight=img2->height-patchHeight+1;

      for (i=0; i<SWidth; i++)
      {
            for (j=0; j<SHeight; j++)
            {
                  for(k=0;k<TWidth;k++)
                  {
                        for(l=0;l<THeight;l++)
                        {
                              dist=PatchDist(img1, img2, patchHeight,patchWidth,i,j,k,l);  
                              if (minDist > dist)
                              {
                                    minDist = dist;
                                    minX_s = i;
                                    minY_s = j;
                                    minX_t = k;
                                    minY_t = l;
                              }
                        }   
                  }
            }
      }
}

int PatchDist(IplImage *img1, IplImage *img2, int patchHeight, int patchWidth, int patchXPosImg1, int patchYPosImg1, int patchXPosImg2, int patchYPosImg2)

{
      int i1, i2, j1, j2;
      int patchDist = 0;

      for (int i=0; i<patchHeight; i++)
      {
            i1 = patchYPosImg1 + i;
            i2 = patchYPosImg2 + i;

            for (int j=0; j<patchWidth; j++)
            {
                  j1 = patchXPosImg1 + j;
                  j2= patchXPosImg2 + j;
                  CvScalar s1 = cvGet2D(img1,i1,j1);
                  CvScalar s2 = cvGet2D(img2,i2,j2);
                  patchDist += (s1.val[0] - s2.val[0])* (s1.val[0] - s2.val[0]) + (s1.val[1] - s2.val[1])* (s1.val[1] - s2.val[1]) + (s1.val[2] - s2.val[2])* (s1.val[2] - s2.val[2]);
            }
      }

      return patchDist;
}
edit retag flag offensive close merge delete