Showing posts with label compare. Show all posts
Showing posts with label compare. Show all posts

2/09/2015

Extracting two hog feature and comparing by vectors of descriptor in opencv (example source code)


I am wondering that two hog features can compare or not.

There was a article about this question on this page ->
http://stackoverflow.com/questions/11626140/extracting-hog-features-using-opencv

This article introduces that to compare the descriptor values of HOG.
I don't know this comparison is surely exact or not.

I think this comparison need more experiment.




similarity test in video





#include < opencv2\opencv.hpp>  
#include < stdio.h>  


#ifdef _DEBUG          
#pragma comment(lib, "opencv_core249d.lib")  
#pragma comment(lib, "opencv_imgproc249d.lib")   //MAT processing  
#pragma comment(lib, "opencv_highgui249d.lib")  
#pragma comment(lib, "opencv_objdetect249d.lib")  
#else  
#pragma comment(lib, "opencv_core249.lib")  
#pragma comment(lib, "opencv_imgproc249.lib")  
#pragma comment(lib, "opencv_highgui249.lib")  
#pragma comment(lib, "opencv_objdetect249.lib")  
#endif     

#define M_PI 3.1415

using namespace std;  
using namespace cv;  


Mat get_hogdescriptor_visual_image(Mat& origImg,
                                   vector< float>& descriptorValues,
                                   Size winSize,
                                   Size cellSize,                                   
                                   int scaleFactor,
                                   double viz_factor);

int main()  
{  

 //image load
 Mat img1 = imread("./b1.jpg");
 Mat img2 = imread("./c5.jpg");

 //rgb 2 gray
 Mat img1_gray; 
 cvtColor(img1, img1_gray, CV_RGB2GRAY);

 Mat img2_gray; 
 cvtColor(img2, img2_gray, CV_RGB2GRAY);

 //resize smaller
 Mat r_img1_gray;
 resize(img1_gray, r_img1_gray, Size(64, 8));
 Mat r_img2_gray;
 resize(img2_gray, r_img2_gray, Size(64, 8));


 //extractino hog feature
 HOGDescriptor d1( Size(64,8), Size(8,8), Size(4,4), Size(4,4), 9);
 HOGDescriptor d2( Size(64,8), Size(8,8), Size(4,4), Size(4,4), 9);
 // Size(32,16), //winSize
 // Size(8,8), //blocksize
 // Size(4,4), //blockStride,
 // Size(4,4), //cellSize,
 // 9, //nbins,

 //hog feature compute
 vector< float> descriptorsValues1;
 vector< Point> locations1;
 d1.compute( r_img1_gray, descriptorsValues1, Size(0,0), Size(0,0), locations1);
 vector< float> descriptorsValues2;
 vector< Point> locations2;
 d2.compute( r_img2_gray, descriptorsValues2, Size(0,0), Size(0,0), locations2);

 //hog feature size
 //cout << descriptorsValues1.size() << endl;


 ///////////////////////
 //copy vector to mat  
 //create Mat  
 Mat A(descriptorsValues1.size(),1,CV_32FC1); 
 //copy vector to mat  
 memcpy(A.data,descriptorsValues1.data(),descriptorsValues1.size()*sizeof(float));
 //create Mat  
 Mat B(descriptorsValues2.size(),1,CV_32FC1); 
 //copy vector to mat  
 memcpy(B.data,descriptorsValues2.data(),descriptorsValues2.size()*sizeof(float));


 /////////////////////////
 //sum( sqrt( (A.-B)^2 ) )
 Mat C = A-B;
 C = C.mul(C);
 cv::sqrt(C, C);
 cv::Scalar rr = cv::sum(C);
 float rrr = rr(0);
 cout << "Distance: " << rrr << endl;

 
 //hog visualization
 Mat r1 = get_hogdescriptor_visual_image(r_img1_gray, descriptorsValues1, Size(64,8), Size(4,4), 10, 3);
 Mat r2 = get_hogdescriptor_visual_image(r_img2_gray, descriptorsValues2, Size(64,8), Size(4,4), 10, 3);

 imshow("hog visualization1", r1);
 imshow("hog visualization2", r2);

 waitKey(0);

 return 0;
}






refer to this page http://feelmare.blogspot.kr/2015/02/opencv-hog-descriptor-computation-and.html about the function "get_hogdescriptor_visual_image" in example source code.


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);  
}  


---