opencv - javacpp-opencv drawContours 产生与 python 不同的结果?
问题描述
javacpp-opencv drawContours 产生的结果比在 python 中错误。
这是java中使用drawContours
函数的代码:
public static void main(String[] args){
Mat im = imread("7KXY.png");
cvtColor(im, im, CV_BGR2GRAY);
threshold(im,im, 230, 255, THRESH_BINARY_INV);
MatVector contours = new MatVector();
Mat hierarchy = new Mat();
findContours(im, contours,hierarchy,RETR_TREE ,CHAIN_APPROX_SIMPLE);
im = new Mat(im.rows(),im.cols(),CV_8UC1);
drawContours(im, contours, -1, new Scalar(255), 1, 8, hierarchy, 2, new Point(0,0));
imwrite( "ccc.jpg", im);
}
这是相同的python代码:
im = cv2.imread(r'7KXY.png')
im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
thresh,im = cv2.threshold(im, 230, 255, cv2.THRESH_BINARY_INV)
im2, contours, hierarchy = cv2.findContours(im, cv2.RETR_TREE , cv2.CHAIN_APPROX_SIMPLE)
im = np.zeros(im.shape).astype(dtype='uint8')
cv2.drawContours(im, contours, -1, (255), 1,8, hierarchy, 2,(0,0))
cv2.imwrite(r"asd.jpg",im)
maven pom
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.4.3</version>
</dependency>
解决方案
我将 OpenCV 4.0.0 用于 Python 3.6 | 爪哇 11。
这是我的 Java 测试代码和结果:
public static void main(String[] args){
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat im = Imgcodecs.imread("7KXY.jpg");
Imgproc.cvtColor(im, im, Imgproc.COLOR_BGR2GRAY);
Imgproc.threshold(im,im, 230, 255, Imgproc.THRESH_BINARY_INV);
List<MatOfPoint> contours = new ArrayList<>();
Mat hierarchy = new Mat();
Imgproc.findContours(im, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
im = Mat.zeros(im.rows(), im.cols(), CvType.CV_8UC1);
Imgproc.drawContours(im, contours, -1, new Scalar(255), 1, 8, hierarchy, 2, new Point(0,0));
Imgcodecs.imwrite( "dst.jpg", im);
}
推荐阅读
- asp.net - 用于连接 SQL Server 并以 JSON 格式返回响应的 Web API
- bash - 一行中的多个 awks
- arrays - 使用多字符分隔符将字符串拆分为数组
- r - 在 r 中的值内过滤 n char
- node.js - apollo-angular error // 角度项目无法编译
- php - 在php中的提交按钮后打印账单发票
- python - 用一个字典/多个字典创建一个类
- vue.js - VUE 使用 axios- 从服务器接收但未显示的数据
- css - 闪亮的模态:宽度自动调整到里面的内容
- python - Python可以使用不同的格式化程序将日志记录到同一个文件中吗