OpenCV實現(xiàn)最小外接正矩形
更新時間:2020年07月21日 11:38:48 作者:fancy_MSF
這篇文章主要為大家詳細介紹了OpenCV實現(xiàn)最小外接正矩形,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了OpenCV實現(xiàn)最小外接正矩形的具體代碼,供大家參考,具體內(nèi)容如下
#include "stdafx.h" #include "cv.h" #include "highgui.h" #include "cxcore.h" #include "math.h" #include <iostream.h> int main(int argc, char* argv[]) { IplImage *src; IplImage *dst; IplImage *ROI; CvMemStorage* storage=cvCreateMemStorage(0); CvSeq* contour=0; src=cvLoadImage("I:\\test.jpg",0); cvNamedWindow("image0",1); cvShowImage("image0",src); int hei=src->height; int wid=src->width; uchar *data; data=(uchar*)src->imageData; int widstep=src->widthStep; int channel=src->nChannels; dst=cvCreateImage(cvSize(wid,hei),IPL_DEPTH_8U,3); ROI=cvCreateImage(cvSize(wid,hei),IPL_DEPTH_8U,3); for (int i=0;i<hei;i++) { for(int j=0;j<wid;j++) { if (data[i*widstep+j*channel]>120) { data[i*widstep+j*channel]=0; } else { data[i*widstep+j*channel]=255; } } } cvNamedWindow("image",0); cvShowImage("image",src); printf("圖像的高為:%d,寬為:%d\n\n",hei,wid); cvCvtColor(src, dst, CV_GRAY2BGR);; cvFindContours(src,storage,&contour,sizeof(CvContour),CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE); for(;contour!=0;contour=contour->h_next) { double length =cvArcLength(contour); double area =fabs(cvContourArea(contour)); CvRect rect = cvBoundingRect(contour,1); cout<<"Length="<<length<<" Area="<<area<<endl; CvPoint p1; CvPoint p2; p1.x=rect.x; p1.y=rect.y; p2.x=rect.x+rect.width; p2.y=rect.y+rect.height; cout<<"p1=("<<p1.x<<","<<p1.y<<")"; cout<<"p2=("<<p2.x<<","<<p2.y<<")"<<endl; cvRectangle(dst,p1,p2,CV_RGB(255,0,0),1,8,0); } cvNamedWindow("dst",1); cvShowImage("dst",dst); cvWaitKey(0); return 0; }
原圖:
二值化反色圖:
最小正矩形圖:
最小正矩形信息:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言計算連續(xù)無序數(shù)組中缺省數(shù)字方法詳解
這篇文章主要介紹了C語言計算連續(xù)無序數(shù)組中缺省數(shù)字方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2023-02-02C語言實現(xiàn)學生信息管理系統(tǒng)(多文件)
這篇文章主要為大家詳細介紹了C語言實現(xiàn)學生信息管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-12-12