Ask Your Question

nqchanh's profile - activity

2019-04-22 02:21:04 -0600 received badge  Popular Question (source)
2018-01-23 05:02:43 -0600 received badge  Notable Question (source)
2016-11-25 23:11:44 -0600 received badge  Popular Question (source)
2013-05-20 11:26:05 -0600 asked a question findHomography problems

i've tried to detect an object from a picture using FeatureDetection interface, but i've got problems with my input images. Here is my code:

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"

using namespace cv;

void readme();

/** @function main */
int main( int argc, char** argv )
{

  //Mat img_object = imread( "facerot.png", CV_LOAD_IMAGE_COLOR );
  //Mat img_scene = imread( "BKDEC.png", CV_LOAD_IMAGE_COLOR );

Mat img_object = imread("img_2.png", CV_LOAD_IMAGE_COLOR  );
Mat img_scene = imread( "img_1.png", CV_LOAD_IMAGE_COLOR  );

//Mat img_object = imread( "black white image 2.jpg", CV_LOAD_IMAGE_COLOR);
//Mat img_scene = imread( "black white image 1.jpg", CV_LOAD_IMAGE_COLOR);

  if( !img_object.data || !img_scene.data )
  { std::cout<< " --(!) Error reading images " << std::endl; return -1; }

  //-- Step 1: Detect the keypoints using SURF Detector
  int minHessian = 400;

  SurfFeatureDetector detector( minHessian );

  std::vector<KeyPoint> keypoints_object, keypoints_scene;

  detector.detect( img_object, keypoints_object );
  detector.detect( img_scene, keypoints_scene );

  //-- Step 2: Calculate descriptors (feature vectors)
  SurfDescriptorExtractor extractor;

  Mat descriptors_object, descriptors_scene;

  extractor.compute( img_object, keypoints_object, descriptors_object );
  extractor.compute( img_scene, keypoints_scene, descriptors_scene );

  //-- Step 3: Matching descriptor vectors using FLANN matcher
  FlannBasedMatcher matcher;
  std::vector< DMatch > matches;
  matcher.match( descriptors_object, descriptors_scene, matches );

  double max_dist = 0; double min_dist = 100;

  //-- Quick calculation of max and min distances between keypoints
  for( int i = 0; i < descriptors_object.rows; i++ )
  { double dist = matches[i].distance;
    if( dist < min_dist ) min_dist = dist;
    if( dist > max_dist ) max_dist = dist;
  }

  printf("-- Max dist : %f \n", max_dist );
  printf("-- Min dist : %f \n", min_dist );

  //-- Draw only "good" matches (i.e. whose distance is less than 3*min_dist )
  std::vector< DMatch > good_matches;

  for( int i = 0; i < descriptors_object.rows; i++ )
  { if( matches[i].distance < 3*min_dist )
     { good_matches.push_back( matches[i]); }
  }

  Mat img_matches;
  drawMatches( img_object, keypoints_object, img_scene, keypoints_scene,
           good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
           vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );

  //-- Localize the object
  std::vector<Point2f> obj;
  std::vector<Point2f> scene;

  for( int i = 0; i < good_matches.size(); i++ )
  {
    //-- Get the keypoints from the good matches
    obj.push_back( keypoints_object[ good_matches[i].queryIdx ].pt );
    scene.push_back( keypoints_scene[ good_matches[i].trainIdx ].pt );
  }

  Mat H = findHomography( obj, scene, CV_RANSAC );

  //-- Get the corners from the image_1 ( the object to be "detected" )
  std::vector<Point2f> obj_corners(4);
  obj_corners[0] = cvPoint(0,0); obj_corners[1] = cvPoint( img_object.cols, 0 );
  obj_corners[2] = cvPoint( img_object.cols, img_object.rows ); obj_corners[3] = cvPoint( 0, img_object.rows );
  std::vector<Point2f> scene_corners(4);

  perspectiveTransform( obj_corners, scene_corners, H);

  //-- Draw lines between the corners (the mapped object in the scene - image_2 )
  line( img_matches, scene_corners[0] + Point2f( img_object.cols, 0), scene_corners[1] + Point2f( img_object.cols, 0), Scalar(0, 255, 0), 4 );
  line( img_matches, scene_corners[1] + Point2f( img_object.cols, 0), scene_corners[2] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
  line( img_matches, scene_corners[2] + Point2f( img_object.cols, 0), scene_corners[3] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
  line( img_matches, scene_corners[3] + Point2f( img_object.cols, 0), scene_corners[0] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );

  //-- Show detected matches
  imshow( "Good Matches & Object detection", img_matches );

  waitKey(0);
  return 0;
  }

  /** @function readme */
  void readme()
  { std ...
(more)
2013-04-29 19:57:53 -0600 commented question cvCalcEigenObjects in opencv?

sorry, but what u mean, Steven Puttemans? i've posted the full code! could u tell me more clearly what u need ? ^~^

2013-04-29 05:51:58 -0600 commented question cvCalcEigenObjects in opencv?

i think the problem here is the function "cvCalcEigenObjects", but i don't know what wrong with it ?

2013-04-29 05:50:32 -0600 asked a question cvCalcEigenObjects in opencv?

Here's my code:

#include "StdAfx.h"
#include <stdio.h>
#include "cv.h"
#include "highgui.h"
#include "cvaux.h"
#include "legacy.h"
/*************************************************/
//Global variables
int nTrainFaces = 0;//number of training images
int nEigens = 0;//number of eigenvalues
IplImage** faceImgArr = 0;//array of face images
IplImage** eigenVectArr = 0;//eigenvectors
CvMat* personNumTruthMat;
CvMat* projectedTrainFaceMat;//projected training faces
CvMat* eigenValMat;//eigenvalues
IplImage* pAvgTrainImg = 0;
/*************************************************/
//Define methods
void learn();
int loadFaceImgArray(char* filename);
void doPCA();
void storeTrainingData();
void recognize();
int loadTrainingData(CvMat** pTrainPersonNumMat);
int findNearestNeighbor(float* projectedTestFace);
/*************************************************/
//Content of methods
void learn()
{
int i;
//load training data
nTrainFaces = loadFaceImgArray("training_images.txt");
//do PCA on the training faces
doPCA();
//project the training images onto the PCA subspace
projectedTrainFaceMat = cvCreateMat(nTrainFaces,nEigens,CV_32FC1);
for(i = 0; i < nTrainFaces; i++)
{
    cvEigenDecomposite(
        faceImgArr[i],
        nEigens,
        eigenVectArr,
        0,NULL,
        pAvgTrainImg,
        projectedTrainFaceMat->data.fl + i*nEigens
        );
}
//store the recognition data as an .xml file
storeTrainingData();
}
int loadFaceImgArray( char* filename)
{
FILE* imgListFile = 0;
char imgFilename[512];
int iFace,nFaces = 0;
//Open the input file
imgListFile = fopen(filename,"r");
//count number of faces
while(fgets(imgFilename,512,imgListFile))
    ++nFaces;
rewind(imgListFile);
//allocate the face-image array and person number matrix
faceImgArr = (IplImage**)cvAlloc(nFaces*sizeof(IplImage*));
personNumTruthMat = cvCreateMat(1,nFaces,CV_32SC1);
//Store the face images in an array
for(iFace = 0; iFace < nFaces; iFace++)
{
    //read person number and name of image file
    fscanf(
        imgListFile,
        "%d %s",
        personNumTruthMat->data.i + iFace,
        imgFilename
            );
    //load the face image
    faceImgArr[iFace] = cvLoadImage(imgFilename,CV_LOAD_IMAGE_GRAYSCALE);
}
fclose(imgListFile);
return nFaces;
}
void doPCA()
{
int i;
CvTermCriteria calcLimit;
CvSize faceImgSize;
//set the number of eigenvalues to use
nEigens = nTrainFaces - 1;
//allocate the eigenvector images
//faceImgSize.width = faceImgArr[0]->width;
//faceImgSize.height = faceImgArr[0]->height;
faceImgSize.width = 170;
faceImgSize.height = 180;
eigenVectArr = (IplImage**)cvAlloc(nEigens*sizeof(IplImage));
for(i = 0; i < nEigens; i++)
    eigenVectArr[i] = cvCreateImage(faceImgSize,IPL_DEPTH_32F,1);
//allocate the eigenvalue array
eigenValMat = cvCreateMat(1,nEigens,CV_32FC1);
//allocate the averaged image
pAvgTrainImg = cvCreateImage(faceImgSize,IPL_DEPTH_32F,1);
//set the PCA termination criterion
calcLimit = cvTermCriteria(CV_TERMCRIT_ITER,nEigens,1);
//compute average image, eigenvalues, and eugenvectors
cvCalcEigenObjects(
    nTrainFaces,
    (void*)faceImgArr,
    (void*)eigenVectArr,
    CV_EIGOBJ_NO_CALLBACK,
    0,0,
    &calcLimit,
    pAvgTrainImg,
    eigenValMat->data.fl
    );
}
void storeTrainingData()
{
CvFileStorage* fileStorage;
int i;
//create a file-storage interface
fileStorage = cvOpenFileStorage("facedata.xml",0,CV_STORAGE_WRITE);
//store all the data
cvWriteInt(fileStorage,"nEigens",nEigens);
cvWriteInt(fileStorage,"nTrainFaces",nTrainFaces);
cvWrite(fileStorage,"trainPersonNumMat",personNumTruthMat,cvAttrList(0,0));
cvWrite(fileStorage,"eigenValMat",eigenValMat,cvAttrList(0,0));
cvWrite(fileStorage,"projectedTrainFaceMat",projectedTrainFaceMat,cvAttrList(0,0));
cvWrite(fileStorage,"avgTrainImg",pAvgTrainImg,cvAttrList(0,0));
for(i = 0; i < nEigens; i++)
{
    char varname[200];
    sprintf(varname,"eigenVect_%d",i);
    cvWrite(fileStorage,varname, eigenVectArr[i],cvAttrList(0,0));
}
//release the file-storage interface
cvReleaseFileStorage(&fileStorage);
}
void recognize(IplImage* TestImg)
{
CvMat* trainPersonNumMat = 0;
float* projectedTestFace = 0;
//load the saved training data
if(!loadTrainingData(&trainPersonNumMat))
    return;

projectedTestFace = (float*)cvAlloc(nEigens*sizeof(float));

int iNearest,nearest;
//project the test images onto the PCA subspace
cvEigenDecomposite(
    TestImg,
    nEigens,
    eigenVectArr,
    0,0,
    pAvgTrainImg,
    projectedTestFace
    );
iNearest = findNearestNeighbor(projectedTestFace);
nearest = trainPersonNumMat->data.i[iNearest ];
printf("nearest = %d",nearest);
}
int loadTrainingData(CvMat** pTrainPersonNumMat)
{
CvFileStorage* fileStorage;
int i;
//create a file-storage interface
fileStorage = cvOpenFileStorage("facedata ...
(more)
2013-02-24 06:55:54 -0600 asked a question line detection ?

I've got an assignment as follow: "Using cvFilter functiom with the suitable kernels in order to scan a picture and then only hold lines that are +- 45 degree and +- 60 degree".

i hope someone can give me some clues, especially how to calculate the kernels ! ^~^

2013-02-19 09:32:21 -0600 commented question capturing camera ?

Yes, i think the problem here is configuration for property! but i do not know which error i've got!

2013-02-19 01:57:36 -0600 asked a question capturing camera ?

i'm using VS2010 conbined with OpenCV 2.2 ! i've tryied programming to capturing camera but the result as the picture below: image description i've configed property as follow:

*VC++ Directories:
 +Include Directories... add: 'C:\OpenCV2.1\include\opencv;'
 +Library Directories... add: 'C:\OpenCV2.1\lib;'
 +Source Directories... add folders in path "...\\include\\Opencv2"
*C/C++ =>General=>add “C:\OpenCV2.2\include”.
*Linker:
 +General=>Additional Library Directories => add “C:\OpenCV2.2\lib”.
*Input=>Additional Dependencies:
 opencv_imgproc220d.lib
 opencv_core220d.lib
 opencv_highgui220d.lib
 opencv_ml220d.lib
 opencv_video220d.lib
 opencv_features2d220d.lib
 opencv_calib3d220d.lib
 opencv_objdetect220d.lib
 opencv_contrib220d.lib
 opencv_legacy220d.lib
 opencv_flann220d.lib

And here is the code:

#include "stdafx.h"
#include "highgui.h"

int _tmain(int argc, _TCHAR* argv[])
{
CvCapture* capture = cvCaptureFromCAM(0);
IplImage* src = NULL;
cvNamedWindow("Webcam",0);
while(1)
{
    src = cvQueryFrame(capture);
    if(!src) break;
    char c = cvWaitKey(30);
    if(c == 27) break;
    cvShowImage("Webcam",src);
}
cvReleaseImage(&src);
cvDestroyWindow("Webcam");
return 0;
}

hope for help! ^~^ !

2013-02-14 07:40:53 -0600 asked a question video processing ?

I've got an assingment as follow: programming C++ by using VS2010 combined with OpenCV to run a AVI file, use a cursor to choose an area an the window displaying AVI file, after choose, this area is converted into Gray code, while the other areas still have their original colors! But i haven't got any idea abot how to do this ! i mean which OpenCV functions i can use ! hope someone can give me some clues! ^~^

2013-01-30 04:44:39 -0600 received badge  Student (source)
2013-01-30 03:26:21 -0600 received badge  Scholar (source)
2013-01-30 03:26:05 -0600 commented answer display image prob ?

thanks Martin Peris! it works well ! but how can i solve the last warning ?

2013-01-30 03:25:18 -0600 commented answer display image prob ?

thanks Mostafa Sataki! it works well ! but how can i solve the last warning ?

2013-01-30 02:09:05 -0600 received badge  Editor (source)
2013-01-30 02:06:26 -0600 asked a question display image prob ?

I’ve try to create a C++ program using OpenCV to open an image in my laptop, but it doesn’t work ! Here is my code:

#include "stdafx.h"
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"

int _tmain(int argc, _TCHAR* argv[])
{
IplImage* img =cvLoadImage("D:\Pictures\love.jpg");
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage( "Example1", img );
cvWaitKey();
cvReleaseImage(&img );
cvDestroyWindow( "Example1" );
return 0;
}

After build & debug, a window popped up but had no image on it. And there are some warnings ad follow:

Warning 3 warning C4129: 'l' : unrecognized character escape sequence d:\document\study\university of technology\semester_8\computer vision\c++ projects\30-1 at home\30-1 at home\30-1 at home.cpp 11 1 30-1 at home

Warning 2 warning C4129: 'P' : unrecognized character escape sequence d:\document\study\university of technology\semester_8\computer vision\c++ projects\30-1 at home\30-1 at home\30-1 at home.cpp 11 1 30-1 at home

Warning 1 warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. c:\opencv2.2\include\opencv2\flann\logger.h 66 1 30-1 at home

I hope someone can help me solve this problem! Thanks !

[EDIT 1} here is my code after being changed:

#include "stdafx.h"
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

int _tmain(int argc, _TCHAR* argv[])
{
IplImage* img =cvLoadImage("D:/Pictures/love.jpg",CV_LOAD_IMAGE_UNCHANGED);
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage( "Example1", img );
cvWaitKey();
cvReleaseImage(&img );
cvDestroyWindow( "Example1" );


return 0;
}

but that warning still occured !