我正在尝试使用opencv findcontours找到轮廓,但是从一个非常简单的图像中缺少一些轮廓。图像在create_mat()函数中创建,在rbt中使用findcontours。我尝试使用以下方法更改findcontours中的模式:CV_RETR_EXTERNAL,CV_RETR_LIST ...我已尝试了所有这些模式,并更改了方法:CV_CHAIN_APPROX_NONE ....
opencv版本是3.3
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc.hpp"
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
#define SIZE_XX 750
#define SIZE_YY 3000
int im_mat[SIZE_XX][SIZE_YY];
cv::Mat create_mat(){
for(int i=0;i <SIZE_XX;i++){
int index=0;
for(int j=0;j<SIZE_YY;j++){
im_mat[i][j] = 0;
}
}
Mat circle1 = cv::Mat(SIZE_XX,SIZE_YY,CV_8UC1,im_mat);
int thickness = -1;
int lineType = 8;
circle( circle1,Point(10,60),25,Scalar(255),thickness,lineType );
circle( circle1,Point(35,10),25,Scalar(255),thickness,lineType );
rectangle( circle1,Point( 200,400),Point( 500, 600),Scalar( 255 ),-1,8 );
circle( circle1,Point(350,35),25,Scalar(255),thickness,lineType );
rectangle( circle1,Point( 1500,500),Point( 2098, 600),Scalar( 255 ),-1,8 );
circle( circle1,Point(35,160),25,Scalar(255),thickness,lineType );
circle( circle1,Point(2000,160),25,Scalar(255),thickness,lineType );
rectangle( circle1,Point( 1000,0),Point( 1100, 200),Scalar( 255 ),-1,8 );
rectangle( circle1,Point( 1800,25),Point( 1650, 200),Scalar( 255 ),-1,8 );
rectangle( circle1,Point( 200,200),Point( 300, 300),Scalar( 255 ),-1,8 );
rectangle( circle1,Point( 625,625),Point( 825, 750),Scalar( 255 ),-1,8 );
rectangle( circle1,Point( 1300,35),Point( 1450, 400),Scalar( 255 ),-1,8 );
return circle1;
}
void rbt(){
Mat src= create_mat();
Mat oi,oi2,oi3,oi4;
src.copyTo(oi);
cv::resize(oi, oi, cv::Size(), 0.5, 0.5);
cv::imshow("test1",oi);
cv::waitKey(1000);
Mat gray;
threshold(src, gray,0, 255,THRESH_BINARY);
cv::resize(gray, oi2, cv::Size(), 0.5, 0.5);
imshow("grays",oi2);
Rect bounding_rect;
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
Mat cpy;
gray.copyTo(cpy);
findContours( cpy, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE );
RNG rg(12345);
Mat destination(gray.rows, gray.cols, CV_8UC3, Scalar(255, 255, 255));
for( int i = 0; i< contours.size(); i++ )
{
Scalar color = Scalar( rg.uniform(75, 175), rg.uniform(75,175), rg.uniform(75,175) );
drawContours( destination, contours, i, color, -1,8,hierarchy );
cv::resize(destination, oi3, cv::Size(), 0.5, 0.5);
cv::imshow("drawing",oi3);
cv::waitKey(1000);
}
waitKey(0);
}
我还尝试使用canny找到边缘,但结果更糟。
图像是黑色背景,白色物体可以找到轮廓。
我的轮廓图像缺失:圆圈位于(35,10)和(350,35)加上矩形位于(1000,0)。