gpt4 book ai didi

opencv - javacpp-opencv drawContours与python产生不同的结果?

转载 作者:行者123 更新时间:2023-12-02 17:06:35 27 4
gpt4 key购买 nike

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);
}

结果
enter image description here

这是相同的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)

结果 enter image description here

Maven Pom
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.4.3</version>
</dependency>

原始图片 enter image description here

最佳答案

问题是new Scalar(255)创建了具有未定义颜色的Scalar对象数组:

  • http://bytedeco.org/javacpp-presets/opencv/apidocs/org/bytedeco/javacpp/opencv_core.Scalar.html#Scalar-long-

  • 我们需要调用 new Scalar(255.0)使其执行此处所需的操作:
  • http://bytedeco.org/javacpp-presets/opencv/apidocs/org/bytedeco/javacpp/opencv_core.Scalar.html#Scalar-double-
  • 关于opencv - javacpp-opencv drawContours与python产生不同的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53844313/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com