[問題] opencv convexhull

看板C_and_CPP (C/C++)作者 (why so serious)時間16年前 (2009/04/12 21:08), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串1/1
最近要弄一個手勢辨識的作業 所以想說用opencv的convexhull來試試看 但是結果都會怪怪的cvConvexHull2出來的結果都會有 (-858993460,-858993460)這個點 因為會抓到這個點,所以會有一些其他的點抓不到 這是在測試一些簡單contour的情況下 若是直接丟幾個點進去跑得結果倒是會正確 囧 #include "stdafx.h" #include "cv.h" #include "highgui.h" #include "cxcore.h" #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { IplImage *pImg = cvLoadImage("多邊形1.bmp", 1); IplImage *pTmp = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1); IplImage *pContourImg = cvCreateImage(cvGetSize(pImg), IPL_DEPTH_8U, 1); CvMemStorage * storage = cvCreateMemStorage(0); CvSeq *contour = cvCreateSeq(CV_SEQ_KIND_GENERIC|CV_32SC2, sizeof(CvContour), sizeof(CvPoint), storage); // CvSeq *contour2 = cvCreateSeq(CV_SEQ_KIND_GENERIC|CV_32SC2, sizeof(CvContour), sizeof(CvPoint), storage); CvPoint pt; cvCvtColor( pImg, pTmp, CV_BGR2GRAY); cvCanny(pTmp, pContourImg, 50, 150, 3); // cvFindContours(pContourImg, storage, &contour, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); cvNamedWindow("AAA:", 1); cvShowImage("AAA:", pContourImg); int count = 0; for(int i = 0; i < pContourImg->width; i++) { for(int j = 0; j < pContourImg->height; j++) { int itensity; itensity = ((uchar*)(pContourImg->imageData + pContourImg->widthStep * i))[j]; if(itensity == 255 && j < pContourImg->width && i< pContourImg->height) { pt.x = j; pt.y = i; cout<<"("<<pt.x<<","<<pt.y<<")"; // count = count + 1; } cvSeqPush(contour, &pt); } } /* CvPoint pt1,pt2,pt3,pt4,pt5,pt6,pt7; pt1.x = 300; pt1.y = 150; cvSeqPush(contour, &pt1); pt2.x = 240; pt2.y = 240; cvSeqPush(contour, &pt2); pt3.x = 360; pt3.y = 240; cvSeqPush(contour, &pt3); pt4.x = 300; pt4.y = 200; cvSeqPush(contour, &pt4); pt5.x = 386; pt5.y = 153; cvSeqPush(contour, &pt5); pt6.x = 440; pt6.y = 110; cvSeqPush(contour, &pt6); pt7.x = 280; pt7.y = 280; cvSeqPush(contour, &pt7);*/ CvSeq * seq = cvConvexHull2(contour, 0, CV_CLOCKWISE, 0); // CvSeq * seq2 = cvConvexityDefects(contour, seq1, NULL); cout<<seq->total; for(int i = 0; i < seq->total; i++) { CvPoint pt1 = **CV_GET_SEQ_ELEM(CvPoint*, seq, i); CvPoint pt2 = **CV_GET_SEQ_ELEM(CvPoint*, seq, i+1); cvLine(pContourImg, pt1, pt2, CV_RGB(255, 255, 255)); // pt = pt1; cout<<"("<<pt1.x<<","<<pt1.y<<")"<<endl; // cout<<"("<<pt0.x<<","<<pt0.y<<")"<<endl; } cvNamedWindow("source:", 1); cvNamedWindow("contour:", 1); // cvDrawContours(pContourImg, contour, CV_RGB(0,0,255), CV_RGB(255, 255, 255), 1, 1, 8); cvShowImage("source:", pImg); cvShowImage("contour:", pContourImg); cvWaitKey(0); cvDestroyWindow("source:"); cvDestroyWindow("contour:"); cvReleaseImage(&pImg); cvReleaseImage(&pContourImg); cvReleaseMemStorage(&storage); system("pause"); return -1; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 134.208.3.134

04/13 17:53, , 1F
將所有CvPoint資料結構的配置改成用cvPoint()初始化看看
04/13 17:53, 1F
文章代碼(AID): #19uUUmNo (C_and_CPP)
文章代碼(AID): #19uUUmNo (C_and_CPP)