0

javacpp-opencv drawContours produce wrong result than in python.

Here is the code in java to use drawContours function:

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

result enter image description here

Here is identical python code:

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)

result enter image description here

maven pom

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

The original pictureenter image description here

abracadabra
  • 371
  • 2
  • 16
  • 1
    Try: `im = Mat.zeros(im.rows(), im.cols(), CV_8UC1);` – Kinght 金 Dec 19 '18 at 05:23
  • tried that ,still didn't work.@Silencer金 – abracadabra Dec 19 '18 at 05:49
  • It works for me! – Kinght 金 Dec 19 '18 at 05:58
  • ```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); Mat imxx = Mat.zeros(im.rows(), im.cols(), CV_8UC1).asMat(); drawContours(imxx, contours, -1, new Scalar(255), 1, 8, hierarchy, 2, new Point(0,0)); imwrite( "ccc.jpg", imxx);``` Result is a complete black image @Silencer金 – abracadabra Dec 19 '18 at 06:12
  • Are you sure that you are using similar version of OpenCV across Java and Python? – ZdaR Dec 19 '18 at 06:50

2 Answers2

1

I'm using OpenCV 4.0.0 for Python 3.6 | Java 11.

This is my Java test code and result:

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

enter image description here enter image description here

Kinght 金
  • 17,681
  • 4
  • 60
  • 74
1

The issue is that new Scalar(255) creates an array of Scalar objects with undefined color:

We need to call new Scalar(255.0) to have it do what is needed here:

Samuel Audet
  • 4,964
  • 1
  • 26
  • 33