#include "stdio.h"
#include "cv.h"
#include "highgui.h"
int main()
{
cvNamedWindow("test");
CvCapture* capture=cvCreateCameraCapture(0);
IplImage* frame;
while((frame=cvQueryFrame(capture))!=NULL)
{
//_sleep(100);
cvShowImage("test",frame);
//cvSaveImage("F:\\O\\pic.bmp",frame);
char c=cvWaitKey(40);
if(c==27) break; //按esc键终止程序
}
cvReleaseImage( &frame );
cvDestroyWindow("test");
return 0;
}
#include "stdio.h"
#include "cv.h"
#include "highgui.h"
#include "Math.h"
int main()
{
IplImage *src = cvLoadImage("f:\\p\\zft5.jpg", 0);
IplImage *dsw = cvCreateImage(cvGetSize(src), 8, 1);
IplImage *dst = cvCreateImage(cvGetSize(src), 8, 3);
CvMemStorage *storage = cvCreateMemStorage(0);
CvSeq *first_contour = NULL;
//二值图像
cvThreshold(src, dsw, 100, 255, CV_THRESH_BINARY);
cvFindContours(dsw, storage, &first_contour, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
cvZero(dst);
int cnt = 0;
double area;
for(; first_contour != 0; first_contour = first_contour->h_next)
{
cnt++;
area = fabs(cvContourArea(first_contour, CV_WHOLE_SEQ)); //获取当前轮廓面积像素
if(area>10000&&area<100000) // 设10k<area<100k
{
CvScalar color = CV_RGB(rand()&255, rand()&255, rand()&255);
cvDrawContours(dst, first_contour, color, color, 0, 2, CV_FILLED, cvPoint(0, 0));
CvRect rect = cvBoundingRect(first_contour,0);
cvRectangle(dst, cvPoint(rect.x, rect.y), cvPoint(rect.x + rect.width, rect.y + rect.height),CV_RGB(255, 0, 0), 1, 8, 0);
CvBox2D a = cvMinAreaRect2(first_contour,0);
//printf(" 坐标 X=%d--Y=%d\n",rect.x,rect.y);
printf(" 角度 %f\n",a.angle );
printf(" 面积 %f\n", area);
printf(" 中心 X=%f--Y=%f",a.center.x ,a.center.y );
}
}
cvNamedWindow( "原图", 1 );
cvShowImage( "原图", src );
cvNamedWindow( "轮廓图", 1 );
cvShowImage( "轮廓图", dst );
cvWaitKey(-1);
// 销毁窗口
cvDestroyWindow("原图");
cvDestroyWindow("轮廓图");
// 释放图像
cvReleaseImage(&dsw);
cvReleaseImage(&dst);
cvClearMemStorage(storage);
cvReleaseMemStorage(&storage);
return 0;
}
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。