OpenCv 新兵 求教
opencv吧
全部回复
仅看楼主
level 2
Hello_海锅 楼主
新人求教!
小弟编写一个简单的程序,程序功能是画出轮廓图并显示轮廓数量。
自己简单做了一张有10个轮廓的图做测试,结果画出是个轮廓,
1.当采用 CV_RETR_TREE模式时,画出10个轮廓显示轮廓数量为14;
2.采用CV_RETR_EXTERNAL模式时,只画出最外轮廓显示轮廓数量为8;
3.采用CV_RETR_LIST和 CV_RETR_CCOMP模式时,画出10个轮廓,显示轮廓数量为8;
求教各位大神我是哪里出了问题,怎么修改代码可以成功画出全部轮廓并正确显示轮廓数量!
代码及截图见二楼,三楼
2016年08月03日 01点08分 1
level 2
Hello_海锅 楼主
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <ctime>
using namespace cv;
CvSeq *contourSeq = NULL;
IplImage* ImageThreshold(IplImage* src)
{
IplImage *gray,*binaryim;
int height,width;
gray=cvCreateImage(cvGetSize(src),src->depth,1);
cvCvtColor(src,gray,CV_BGR2GRAY);
height=gray->height;
width=gray->width;
printf("The canvas'width is :%d, height is :%d\n\n",width,height);
binaryim=cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1);
cvThreshold(gray,binaryim,128,255,CV_THRESH_BINARY_INV);
return binaryim;
}
IplImage* printlunkuo(IplImage *binaryim)
{
CvMemStorage *contourStorage=cvCreateMemStorage();
cvFindContours(binaryim, contourStorage, &contourSeq, sizeof(CvContour),CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
int contourcount=contourSeq->total;
printf("contour count is %d",contourcount);
IplImage *pOutlineImage = cvCreateImage(cvGetSize(binaryim), IPL_DEPTH_8U, 3);
int nLevels = 5;
cvRectangle(pOutlineImage, cvPoint(0, 0), cvPoint(pOutlineImage->width, pOutlineImage->height), CV_RGB(0,0,0), CV_FILLED);
cvDrawContours(pOutlineImage, contourSeq, CV_RGB(255,0,0), CV_RGB(0,255,0), nLevels,0.5);
return pOutlineImage;
}
int main(int argc,char** argv)
{
IplImage *src,*binaryim,*contourim;
src=cvLoadImage("gtest2.bmp",1);
binaryim=ImageThreshold(src);
contourim = printlunkuo(binaryim);
cvNamedWindow("Binary_image",0);
cvShowImage("Binary_image",binaryim);
cvNamedWindow("Contour_image",0);
cvShowImage("Contour_image",contourim);
cvWaitKey(0);
cvDestroyWindow("Binary_image");
cvReleaseImage(&binaryim);
cvDestroyWindow("Contours_image");
cvReleaseImage(&contourim);
}
2016年08月03日 01点08分 2
level 2
Hello_海锅 楼主
原图
轮廓图
轮廓数量
2016年08月03日 01点08分 3
1