I got a trouble in using a trackbar in opencv . I want it for changing the reference image in SURF for object recognition but the problem is only one reference image is detected in my program.
I know this is very basic but I am new to opencv. I hope someone give me solution.
Thanks.
#include <iostream>
#include <opencv/cxcore.h>
#include <opencv2\imgproc\imgproc_c.h>
#include <stdio.h>
#include <math.h>
#include <opencv\highgui.h>
#include <opencv\cv.h>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/legacy/legacy.hpp>
#include <opencv2\nonfree\nonfree.hpp>
using namespace std;
#include "MareMatchingClass.h"
int colorInt;
int g_switch_value=0;
// Trackbar/switch callback
void switch_callback( int position ){
if( g_switch_value == 0 ){
colorInt = 0;
}else{
colorInt = 1;
}
}
int main()
{
cv::initModule_nonfree();
IplImage* PatchImg;
cvNamedWindow("mainWin",CV_WINDOW_AUTOSIZE);
cvCreateTrackbar( "Switch","mainWin", &g_switch_value, 1, switch_callback );
if( colorInt == 0)
{
PatchImg = cvLoadImage("D:/id2.jpg", CV_LOAD_IMAGE_GRAYSCALE);
}
else
{
PatchImg = cvLoadImage("D:/id.jpg", CV_LOAD_IMAGE_GRAYSCALE);
}
//Create Matching class
CMareMatchingClass MMathing;
//Input PatchImg
MMathing.ExtractPatchSurf(PatchImg);
double cornerPt[9]={
MMathing.PatchWidth/2, 0, MMathing.PatchWidth,
0, MMathing.PatchHeight, MMathing.PatchHeight,
1, 1, 1};
CvMat* McornerPt2 = cvCreateMat(3, 3, CV_64FC1);
memcpy(McornerPt2->data.db, cornerPt, sizeof(double)*9);
int c;
IplImage* img;
IplImage* BackGroundImg = NULL;
CvCapture* capture = cvCaptureFromCAM(0);
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 320);
cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT, 240 );
while(1)
{
cvGrabFrame(capture);
img = cvRetrieveFrame(capture);
if(BackGroundImg == NULL)
{
BackGroundImg = cvCreateImage(cvSize(img->width, img->height), 8, 1);
}
cvCvtColor(img, BackGroundImg, CV_RGB2GRAY);
Rect4Pt rect4pt;
if(MMathing.GetObjectRectAndBestH(BackGroundImg, &rect4pt) != 0)
{
cvLine(img, cvPoint(rect4pt.LeftTop.x, rect4pt.LeftTop.y),
cvPoint(rect4pt.RightTop.x, rect4pt.RightTop.y), cvScalar(255,0,0), 2);
cvLine(img, cvPoint(rect4pt.RightTop.x, rect4pt.RightTop.y),
cvPoint(rect4pt.RightBottom.x, rect4pt.RightBottom.y), cvScalar(255,0,0), 2);
cvLine(img, cvPoint(rect4pt.RightBottom.x, rect4pt.RightBottom.y),
cvPoint(rect4pt.LeftBottom.x, rect4pt.LeftBottom.y), cvScalar(255,0,0), 2);
cvLine(img, cvPoint(rect4pt.LeftBottom.x, rect4pt.LeftBottom.y),
cvPoint( rect4pt.LeftTop.x, rect4pt.LeftTop.y), cvScalar(255,0,0), 2);
Rect4Pt rect4ptZ;
MMathing.GetHMulRect(McornerPt2,&rect4ptZ);
}
cvShowImage("mainWin",img);
c = cvWaitKey(10);
if(c == 27)
break;
}
cvReleaseCapture(&capture);
cvDestroyAllWindows();
cvReleaseImage(&PatchImg);
cvReleaseImage(&BackGroundImg);
cvReleaseMat(&McornerPt2);
}