La *_*bla 11 java android opencv javacv
我需要提取图像的最大轮廓.这是我目前正在使用的代码.在线收集了几个片段
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(outerBox, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
double maxArea = -1;
int maxAreaIdx = -1;
for (int idx = 0; idx < contours.size(); idx++) {
Mat contour = contours.get(idx);
double contourarea = Imgproc.contourArea(contour);
if (contourarea > maxArea) {
maxArea = contourarea;
maxAreaIdx = idx;
}
}
Run Code Online (Sandbox Code Playgroud)
它似乎工作.但是,我不太确定如何从这里开始.我尝试过使用Imgproc.floodFill
,但我不太清楚如何使用.此功能需要Mat
与原始Mat
+2水平和+2垂直相同大小的桅杆.当我在轮廓上运行它时contours.get(maxAreaIdx)
,它给了我一个错误.代码:
Mat mask = Mat.zeros(contour.rows() + 2, contour.cols() + 2, CvType.CV_8UC1);
int area = Imgproc.floodFill(contour, mask, new Point(0,0), new Scalar(255, 255, 255));
Run Code Online (Sandbox Code Playgroud)
错误:
11-18 19:07:49.406: E/cv::error()(3117): OpenCV Error: Unsupported format or combination of formats () in void cvFloodFill(CvArr*, CvPoint, CvScalar, CvScalar, CvScalar, CvConnectedComp*, int, CvArr*), file /home/oleg/sources/opencv/modules/imgproc/src/floodfill.cpp, line 621
Run Code Online (Sandbox Code Playgroud)
所以基本上我的问题是,在找到面积最大的轮廓后,我怎样才能"突出"它呢?我希望其他一切都是黑色的,轮廓是白色的
谢谢!
alv*_*tya 10
您可以在OpenCV中使用DrawContours函数:http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=drawcontours#drawcontours
或者你可以在C++中使用这个实现(你可以在OpenCV doc中找到Java中的等价物,只需在google上键入OpenCV +函数的名称)
Mat src = imread("your image"); int row = src.rows; int col = src.cols;
//Create contour
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
Mat src_copy = src.clone();
findContours( src_copy, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
// Create Mask
Mat_<uchar> mask(row,col);
for (int j=0; j<row; j++)
for (int i=0; i<col; i++)
{
if ( pointPolygonTest( contours[0], Point2f(i,j),false) =0)
{mask(j,i)=255;}
else
{mask(j,i)=0;}
};
Run Code Online (Sandbox Code Playgroud)
尝试轮廓[1],轮廓[2] ...找到最大的一个
这是为了显示你的轮廓:
namedWindow("Contour",CV_WINDOW_AUTOSIZE);
imshow("Contour", mask);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
21255 次 |
最近记录: |