11/14/2014

cvCalcBackProjectPatch example source code


...
#include< cv.h>  
#include< highgui.h>  
  
void GetHSV (const IplImage* image, IplImage** h, IplImage** s, IplImage** v);  
  
int main()  
{  
    IplImage* src = cvLoadImage ("bluecup.jpg", 1);  
    IplImage* h_src = NULL;  
    IplImage* s_src = NULL;  
    GetHSV (src, &h_src, &s_src, NULL);  
    IplImage *images[] = {h_src,s_src};  
    CvHistogram* hist_src = NULL;  
  
    /*่ฎก็ฎ—ไบŒ็ปด็›ดๆ–นๅ›พ*/  
    int dims = 2;  
    int size[] = {30, 32};  
    float range_h[] = {0, 180};  
    float range_s[] = {0, 256};  
    float* ranges[] = {range_h, range_s};  
    hist_src = cvCreateHist (dims, size, CV_HIST_ARRAY, ranges);  
    cvCalcHist (images, hist_src);  
    cvNormalizeHist (hist_src, 1);  
  
    IplImage* dst = cvLoadImage ("adrian1.jpg", 1);  
    IplImage* h_dst = NULL;  
    IplImage* s_dst = NULL;  
    GetHSV (dst, &h_dst, &s_dst, NULL);  
    images[0] = h_dst;  
    images[1] = s_dst;  
  
    CvSize patch_size = cvSize (src->width, src->height);  
    IplImage* result = cvCreateImage (cvSize(h_dst->width - patch_size.width + 1, h_dst->height - patch_size.height + 1),  
        IPL_DEPTH_32F, 1);  
    cvCalcBackProjectPatch (images, result, patch_size, hist_src, CV_COMP_CORREL, 1);  
    cvShowImage ("result", result);  
      

    CvPoint max_location;  
    cvMinMaxLoc(result, NULL, NULL, NULL, &max_location, NULL);  
    max_location.x += cvRound (patch_size.width / 2);  
    max_location.y += cvRound (patch_size.height / 2);  
  

    CvPoint top = cvPoint(max_location.x - patch_size.width / 2,max_location.y - patch_size.height / 2);  
    CvPoint bottom = cvPoint(max_location.x + patch_size.width / 2, max_location.y + patch_size.height / 2);  
    cvRectangle (dst, top, bottom, CV_RGB(255, 0, 0), 1, 8, 0);  
    cvShowImage ("dst", dst);  
  
    cvWaitKey (0);  
  
    cvReleaseImage(&src);    
    cvReleaseImage(&dst);    
    cvReleaseImage(&h_src);    
    cvReleaseImage(&h_dst);    
    cvReleaseImage(&s_dst);    
    cvReleaseImage(&s_src);    
    cvReleaseHist(&hist_src);    
    cvReleaseImage(&result);    
    cvDestroyAllWindows();  
}  
  
void GetHSV (const IplImage* image, IplImage** h, IplImage** s, IplImage** v)  
{  
    IplImage* hsv = cvCreateImage (cvGetSize (image), 8, 3);  
    cvCvtColor (image, hsv, CV_BGR2HSV);  
      
    if ((h != NULL) && (*h == NULL))  
        *h = cvCreateImage (cvGetSize(image), 8, 1);  
    if ((s != NULL) && (*s == NULL))  
        *s = cvCreateImage (cvGetSize(image), 8, 1);  
    if ((v != NULL) && (*v == NULL))  
        *v = cvCreateImage (cvGetSize(image), 8, 1);  
  
    cvSplit (hsv, *h, (s == NULL)?NULL:*s, (v == NULL)?NULL:*v, NULL);  
    cvReleaseImage (&hsv);  
}  


---

No comments:

Post a Comment