Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Trouble in OPENCV trackbar.

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



}

Trouble in OPENCV trackbar.

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 <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 #include "MareMatchingClass1.h"

void switch_callback( int position ){

if( g_switch_value == 0 ){
 colorInt = 0;
}else{
 colorInt = 1;
printMat(CvMat& M)
{
for(int i=0; i<M.rows; ++i)
{
    for(int j=0;j<M.cols; ++j)
    {
        printf("%lf ", cvmGet(&M,i,j) );
    }
    printf("\n");
}
}

int 
void main()
{

//Camera Version
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 
IplImage* PatchImg2;
PatchImg2 = cvLoadImage("D:/id.jpg", CV_LOAD_IMAGE_GRAYSCALE);

}

//Create Matching class
CMareMatchingClass MMathing;    
//Input PatchImg
MMathing.ExtractPatchSurf(PatchImg);

//Create Matching class
CMareMatchingClass2 MMathing2;  
//Input PatchImg
MMathing2.ExtractPatchSurf2(PatchImg2);

double cornerPt2[9]={
    MMathing2.PatchWidth2/2,  0,                     MMathing2.PatchWidth2,
    0,                      MMathing2.PatchHeight2,  MMathing2.PatchHeight2,
    1,                      1,                     1};

CvMat* McornerPt22 = cvCreateMat(3, 3, CV_64FC1);
memcpy(McornerPt22->data.db, cornerPt2, sizeof(double)*9);

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 );
 cvNamedWindow("mainWin",CV_WINDOW_AUTOSIZE);
while(1)
{
 cvGrabFrame(capture);
 img = cvRetrieveFrame(capture);

 if(BackGroundImg == NULL)
 {
     BackGroundImg = cvCreateImage(cvSize(img->width, img->height), 8, 1);
 }
 cvCvtColor(img, BackGroundImg, CV_RGB2GRAY);

    Rect4Pt2 rect4pt2;
    Rect4Pt rect4pt;

    if(MMathing2.GetObjectRectAndBestH2(BackGroundImg, &rect4pt2) != 0)
    {
        cvLine(img, cvPoint( rect4pt2.LeftTop.x, rect4pt2.LeftTop.y),
cvPoint(rect4pt2.RightTop.x, rect4pt2.RightTop.y), cvScalar(255,0,0), 2);
        cvLine(img, cvPoint(rect4pt2.RightTop.x, rect4pt2.RightTop.y),
cvPoint(rect4pt2.RightBottom.x, rect4pt2.RightBottom.y), cvScalar(255,0,0), 2);
        cvLine(img, cvPoint(rect4pt2.RightBottom.x, rect4pt2.RightBottom.y),
cvPoint(rect4pt2.LeftBottom.x, rect4pt2.LeftBottom.y), cvScalar(255,0,0), 2);
        cvLine(img, cvPoint(rect4pt2.LeftBottom.x, rect4pt2.LeftBottom.y), cvPoint(
rect4pt2.LeftTop.x, rect4pt2.LeftTop.y), cvScalar(255,0,0), 2);

        Rect4Pt2 rect4ptZ2;
        MMathing2.GetHMulRect(McornerPt22,&rect4ptZ2);

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



}