OPENCV轨道栏中的问题。

时间:2013-08-04 15:59:05

标签: opencv computer-vision

我在opencv中使用轨道栏时遇到了麻烦。我希望它能更改SURF中的参考图像以进行物体识别,但问题是我的程序中只检测到一个参考图像。

我知道这是非常基本的,但我是opencv的新手。我希望有人给我解决方案。

感谢。

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



}

1 个答案:

答案 0 :(得分:0)

我认为你必须移动代码

if(colorInt!=lastColorInt)
{
    if( colorInt == 0)
    {
            PatchImg = cvLoadImage("D:/id2.jpg", CV_LOAD_IMAGE_GRAYSCALE);
    }
    else
    {
            PatchImg = cvLoadImage("D:/id.jpg", CV_LOAD_IMAGE_GRAYSCALE);
    }
lastColorInt=colorInt;
}

在while循环中并更改它以查找colorInt变量的更改状态(例如,我更改它)。不要忘记发布分配的图像。