Jen*_*ang 4 java android opencv convex-hull
请帮我,
Android上的Convex Hull有问题.我使用Java和OpenCV 2.3.
在我使用Java之前,我使用Visual Studio 2008在C++上创建它.
此代码可以在C++上成功运行.
现在,我想在Android上将它从C++转换为Java.当我在SDK Android模拟器上运行它时,我发现了像"强制关闭"这样的错误.
这是我在C++上的代码:
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
drawing = Mat::zeros( canny_output.size(), CV_64F );
/// Find the convex hull object for each contour
vector<vector<Point> > hull ( contours.size() );
for( int i = 0; i < contours.size(); i++ )
{ convexHull( Mat(contours[i]), hull[i], false );
}
for(size_t i = 0; i < contours.size(); i++){
drawContours( drawing, hull, i, Scalar(255, 255, 255), CV_FILLED ); // FILL WHITE COLOR
}
Run Code Online (Sandbox Code Playgroud)
这是我在Android上的代码:
Mat hierarchy = new Mat(img_canny.rows(),img_canny.cols(),CvType.CV_8UC1,new Scalar(0));
List<Mat> contours =new ArrayList<Mat>();
List<Mat> hull = new ArrayList<Mat>(contours.size());
drawing = Mat.zeros(img_canny.size(), im_gray);
Imgproc.findContours(img_dilasi, contours, hierarchy,Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0));
for(int i=0; i<contours.size(); i++){
Imgproc.convexHull(contours.get(i), hull.get(i), false);
}
for(int i=0; i<contours.size(); i++){
Imgproc.drawContours(drawing, hull, i, new Scalar(255.0, 255.0, 255.0), 5);
}
Run Code Online (Sandbox Code Playgroud)
对于您的信息,我在我的代码中对Convex Hull进行了一些修改.我在轮廓内填充颜色.
有人可以帮我解决我的问题吗?
我非常感谢你的帮助.
med*_*loh 10
没有代表添加评论,只是想说上面的两个答案帮助我得到Imgproc.convexHull()为我的用例工作这样的事情(2.4.8):
MatOfPoint mopIn = ...
MatOfInt hull = new MatOfInt();
Imgproc.convexHull(mopIn, hull, false);
MatOfPoint mopOut = new MatOfPoint();
mopOut.create((int)hull.size().height,1,CvType.CV_32SC2);
for(int i = 0; i < hull.size().height ; i++)
{
int index = (int)hull.get(i, 0)[0];
double[] point = new double[] {
mopIn.get(index, 0)[0], mopIn.get(index, 0)[1]
};
mopOut.put(i, 0, point);
}
// do something interesting with mopOut
Run Code Online (Sandbox Code Playgroud)
findContours()查看and的文档convexHull(),您似乎错误地声明了变量contours和hull。
尝试将声明更改为:
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
List<MatOfInt> hull = new ArrayList<MatOfInt>();
Run Code Online (Sandbox Code Playgroud)
然后,在调用 后convexHull(),hull包含构成凸包的点的索引。contours为了用 绘制点drawContours(),您需要填充一个MatOfPoint仅包含凸包上的点的新点,并将其传递给drawContours()。我把这个留给你作为练习。
| 归档时间: |
|
| 查看次数: |
8945 次 |
| 最近记录: |