有用过findcontours找轮廓的吗,contour里面存储的是点向量
发布网友
发布时间:2022-09-25 11:29
我来回答
共1个回答
热心网友
时间:2023-09-17 11:00
#include "cv.h"
#include "highgui.h"
using namespace cv;
int main( int argc, char** argv )
{
Mat src;
// the first command-line parameter must be a filename of the binary
// (black-n-white) image
if( argc != 2 || !(src=imread(argv[1], 0)).data)
return -1;
Mat dst = Mat::zeros(src.rows, src.cols, CV_8UC3);
src = src > 1;
namedWindow( "Source", 1 );
imshow( "Source", src );
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours( src, contours, hierarchy,
CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
// iterate through all the top-level contours,
// draw each connected component with its own random color
int idx = 0;
for( ; idx >= 0; idx = hierarchy[idx][0] )
{
Scalar color( rand()&255, rand()&255, rand()&255 );
drawContours( dst, contours, idx, color, CV_FILLED, 8, hierarchy );
}
namedWindow( "Components", 1 );
imshow( "Components", dst );
waitKey(0);
}
注意基本学习方法,查看官方文档,不会的方法查看官方文档和相关参数方法
ht t p ://docs.opencv.org/2.4/moles/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours#void findContours(InputOutputArray image, OutputArrayOfArrays contours, int mode, int method, Point offset)
也可以采用如下方式
Mat g_grayImage,g_cannayMat_output;
int g_nThresh = 80;
RNG g_rng(12345);
// canny算子检测边缘
Canny(g_grayImage,g_cannyMat_output,g_nThresh,g_nThresh*2,3);
//寻找轮廓
findContours(g_cannyMat_output,contours,hierarchy,RETR_TREE,
CHAIN_APPROX_SIMPLE,Point(0,0));
Mat drawing = Mat::zero(g_cannyMat_output.size(),CV_8UC3);
//绘制轮廓
for(int i = 0;i<contours.size();++i)
{
Scalar color = Scalar(g_rng.uniform(0,255),g_rng.uniform(0,255),
g_rng.uniform(0,255)); //任意值
drawContours(drawing,contours,i,color,2,8,hierarchy,0,Point());
}
imshow(WINDOW_NAME,drawing);
热心网友
时间:2023-09-17 11:00
#include "cv.h"
#include "highgui.h"
using namespace cv;
int main( int argc, char** argv )
{
Mat src;
// the first command-line parameter must be a filename of the binary
// (black-n-white) image
if( argc != 2 || !(src=imread(argv[1], 0)).data)
return -1;
Mat dst = Mat::zeros(src.rows, src.cols, CV_8UC3);
src = src > 1;
namedWindow( "Source", 1 );
imshow( "Source", src );
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours( src, contours, hierarchy,
CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
// iterate through all the top-level contours,
// draw each connected component with its own random color
int idx = 0;
for( ; idx >= 0; idx = hierarchy[idx][0] )
{
Scalar color( rand()&255, rand()&255, rand()&255 );
drawContours( dst, contours, idx, color, CV_FILLED, 8, hierarchy );
}
namedWindow( "Components", 1 );
imshow( "Components", dst );
waitKey(0);
}
注意基本学习方法,查看官方文档,不会的方法查看官方文档和相关参数方法
ht t p ://docs.opencv.org/2.4/moles/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours#void findContours(InputOutputArray image, OutputArrayOfArrays contours, int mode, int method, Point offset)
也可以采用如下方式
Mat g_grayImage,g_cannayMat_output;
int g_nThresh = 80;
RNG g_rng(12345);
// canny算子检测边缘
Canny(g_grayImage,g_cannyMat_output,g_nThresh,g_nThresh*2,3);
//寻找轮廓
findContours(g_cannyMat_output,contours,hierarchy,RETR_TREE,
CHAIN_APPROX_SIMPLE,Point(0,0));
Mat drawing = Mat::zero(g_cannyMat_output.size(),CV_8UC3);
//绘制轮廓
for(int i = 0;i<contours.size();++i)
{
Scalar color = Scalar(g_rng.uniform(0,255),g_rng.uniform(0,255),
g_rng.uniform(0,255)); //任意值
drawContours(drawing,contours,i,color,2,8,hierarchy,0,Point());
}
imshow(WINDOW_NAME,drawing);