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

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

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

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

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

原始图片opencv - javacpp-opencv drawContours与python产生不同的结果?-LMLPHP

最佳答案

问题是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/

    10-09 01:07