Ask Your Question

Trouble in OPENCV trackbar.

asked 2013-08-04 11:07:01 -0500

lenteken gravatar image

updated 2013-08-05 00:18:12 -0500

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.


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

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

void main()

//Camera Version

IplImage* PatchImg;
PatchImg = cvLoadImage("D:/id2.jpg", CV_LOAD_IMAGE_GRAYSCALE);

IplImage* PatchImg2;
PatchImg2 = cvLoadImage("D:/id.jpg", CV_LOAD_IMAGE_GRAYSCALE);

//Create Matching class
CMareMatchingClass MMathing;    
//Input PatchImg

//Create Matching class
CMareMatchingClass2 MMathing2;  
//Input PatchImg

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

    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.LeftTop.x, rect4pt.LeftTop.y), cvScalar(255,0,0), 2);

        Rect4Pt rect4ptZ;



    c = cvWaitKey(10);
    if ...
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-08-04 17:32:19 -0500

updated 2013-08-04 21:40:57 -0500

You don't load your image in the loop, therefor, when you change the value with your trackbar, it doesn't load a new image. I suggest to load both image, into two different Mat. Compute all the descriptors for the both images. After that, you use the trackbar has a boolean to point to the right image.

//Create Matching class
CMareMatchingClass MColorMatch;
CMareMatchingClass MBnWMatch;
//Input PatchImg
    if( colorInt == 1 )
        res = MMColorMatch.GetObjectRectAndBestH(BackGroundImg, &rect4pt);
        res = MMBnWMatch.GetObjectRectAndBestH(BackGroundImg, &rect4pt);
    if( res == 0 )

[EDIT] You have to:

  1. Load the both images
  2. Compute the descriptors for the both images
  3. compare (with GetObjectRectAndBestH I assume) the current image with:
    • the color descriptor if the trackbar is on color (colorInt == 1)
    • the black and white image if the trackbar is on black and white (colorInt == 0)
edit flag offensive delete link more


Sorry, but I can't understand your point . Can you please edit the whole code to be clearly for me? Thanks.

lenteken gravatar imagelenteken ( 2013-08-04 20:57:19 -0500 )edit

I'm done in load the both images and compute the descriptors for the both images. What is the continuation of the code you said in if( res == 0 )? I edit the code above.

lenteken gravatar imagelenteken ( 2013-08-05 00:15:16 -0500 )edit

Draw your line (cvLine(...)). This line has to replace each of the if(MMathing2.GetObjectRectAndBestH2(BackGroundImg, &rect4pt2) != 0) tests (H and H2).

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2013-08-05 01:53:20 -0500 )edit

Question Tools


Asked: 2013-08-04 11:07:01 -0500

Seen: 187 times

Last updated: Aug 05 '13