我想将每个点向量放入一个矩阵中,例如:
std::vector<std::vector<cv::Point>> vec;
......................
for (int i ; i < vec.size();i++){
imshow("stuff", cv::Mat(vec[i]); /// this crashes !!!
}
知道怎么做吗?提前致谢
imshow 寻找完整的图片。
通过将轮廓投射到垫子上,您将没有图片。你可以做的是:(src是你的图片)
src = imread( argv[1], 1 );
/// Convert image to gray and blur it
cvtColor( src, src_gray, CV_BGR2GRAY );
blur( src_gray, src_gray, Size(3,3) );
Mat canny_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
/// Detect edges using canny
Canny( src_gray, canny_output, thresh, thresh*2, 3 );
/// Find contours
findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
/// Draw contours
for( int i = 0; i< contours.size(); i++ )
{
Mat test_image = Mat::zeros( canny_output.size(), CV_8UC3 );
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
drawContours( test_image, contours, i, color, 2, 8, hierarchy, 0, Point() );
imshow("test",test_image);
waitKey();
}
你可以这样做:
void draw_contour(cv::Mat &dst_img, const std::vector<cv::Point> &contour, const cv::Scalar &color)
{
for (auto &point: contour)
{
dst_img.at<unsigned char>(point) = color;
}
}
或者,如果您使用轮廓近似:
void draw_contour(cv::Mat &dst_img, const std::vector<cv::Point> &contour, const cv::Scalar &color)
{
for (unsigned i = 0; i < contour.size(); ++i)
{
cv::line(dst_img, contour[i], contour[(i + 1) % contour.size(), color);
}
}