注意在尋找輪廓時要選擇中尋找外層輪廓
RETR_EXTERNAL
#include "opencv/cv.h"
#include "opencv/highgui.h"
using namespace std;
using namespace cv;
int main()
{
Mat srcimg=imread("./22.jpg");
Mat dst;
cvtColor(srcimg,dst,CV_BGR2GRAY);
threshold(dst,dst,120,255,1);
vector > edgepoint;
vector lclass;
findContours(dst,edgepoint,lclass,RETR_EXTERNAL,CHAIN_APPROX_NONE,Point());
Mat mat[edgepoint.size()];
for(int i=0;i
{
Rect rec=boundingRect(Mat(edgepoint[i]));
mat[i]=dst(rec);
rectangle(dst,rec,Scalar(100,80,90),1,1,0);
drawContours(dst,edgepoint,i,Scalar(200),1,8,lclass);
string str=to_string(i);
imshow(str,mat[i]);
}
imshow("tt",dst);
cout<
waitKey(0);
}
結果如下:
總結
以上就是本文關于c語言通過opencv實作輪廓處理與切割的全部内容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支援!