0
我在使用opencv中的trackbar時遇到了麻煩。我希望它用於改變SURF中的參考圖像以進行物體識別,但問題是在我的程序中僅檢測到一個參考圖像。OPENCV trackbar中的問題。
我知道這是非常基本的,但我是新手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);
}
我試過了你的建議,但是我得到一個錯誤「變量'PatchImg'正在被使用而未被初始化。」 lastColorInt的值是多少?以及如何更改狀態變化的變化?謝謝 – user2529081
指針必須初始化,所以你必須指定:PatchImg = NULL;在一開始的時候。 lastColorInt是與colorInt相同的類型,並通過與colorInt相同的值進行初始化。狀態轉換捕獲已經在上面的代碼中實現。 –