Ask Your Question

Canny (OCL, 3-beta) can not detect connected contour of black square on a white background.

asked 2014-12-30 13:09:17 -0500

updated 2015-01-12 15:04:10 -0500

Simple code for simple image result is not connected contour as it is expected. Top or bottom edge missed. Result printscreen is square-bug.png . The picture itself is square.png. Code is:

   void test(){
        UMat img = imread("./square.png", IMREAD_COLOR).getUMat(ACCESS_READ);
        UMat imgGray, imgGrayEdges3, imgGrayEdges5;
        cvtColor(img, imgGray, CV_BGR2GRAY);
        Canny(imgGray, imgGrayEdges3, 50, 150, 3);
        Canny(imgGray, imgGrayEdges5, 300, 2000, 5); // why so large thresholds?..
        cout << imgGray.getMat(ACCESS_READ) << "\n" << imgGrayEdges3.getMat(ACCESS_READ) << "\n" << imgGrayEdges5.getMat(ACCESS_READ) ;
        imshow("img", img);
        imshow("canny3", imgGrayEdges3);
        imshow("canny5", imgGrayEdges5);

Am I doing something wrong?

I use OpenCV3-beta in Eclipse under Win8.1 and have to build source from git. Source updated and compiled today (last canny.cpp change was 7 weeks ago). I do have openCL so I guess UMat Canny function uses OCL_RUN version of function

UPDATE: device and system info

  • Driver Packaging
  • Version 14.501.1003-141120a-178000C
  • Catalyst Version 14.12 AMD Catalyst
  • Omega Software 2D Driver
  • Version Direct3D
  • Version OpenGL
  • Version Mantle Driver
  • Version 9.1.

    • Graphics Chipset AMD Radeon HD 7900 Series
    • BIOS Version
    • BIOS Part Number 113-C3860100-100
    • BIOS Date 2011/12/07
edit retag flag offensive close merge delete


Well.. no ideas. Bug reported:

Vit gravatar imageVit ( 2015-01-07 04:19:50 -0500 )edit

Can you share on which hardware and driver this happen? Did you test with OpenCV 2.4? The code would be only slightly different, example:

    #include "opencv2/ocl/ocl.hpp"
    cv::Mat input = cv::imread(filename,0 ); //directly grayscale
    cv::ocl::oclMat imgGray(input);
    cv::ocl::oclMat imgGrayEdges3, imgGrayEdges5;

    cv::ocl::Canny(imgGray, imgGrayEdges3, 50, 150, 3);
    cv::ocl::Canny(imgGray, imgGrayEdges5, 300, 2000, 5);

    cv::imshow("img", (cv::Mat) imgGray);
    cv::imshow("canny3", (cv::Mat) imgGrayEdges3);
    cv::imshow("canny5", (cv::Mat) imgGrayEdges5);

Which works correctly for me by the way

Antonio gravatar imageAntonio ( 2015-01-12 09:06:47 -0500 )edit

BTW: The answer to: "Why so large threshold" is that when using 5 as kernel size of Canny, at an intermediate stage you get a Sobel image computed with the mask 5 parameters, which has much larger multipliers, and of course convolves a larger number of pixels.

Antonio gravatar imageAntonio ( 2015-01-12 09:55:36 -0500 )edit

@Antonio, thanks for your answer. I tested Canny and it is ok in 2.4 with this test picture, your code works perfect. The problem is actual for OpenCV3-beta only. Post updated by soft- and hardware info.

As for threshold, from my point of view it is more convenient to have normalized output i.e. threshold should not be a function of another parameters, this increases the code hidden-errors possibility.

Vit gravatar imageVit ( 2015-01-12 15:09:56 -0500 )edit

@Vit I confirm 100% your findings: OpenCV3 OpenCL implementation of Canny it's buggy. (Although is 4 times faster)

Antonio gravatar imageAntonio ( 2015-01-13 06:19:47 -0500 )edit

BTW, did you build 32bit or 64bit? In my case, 32bit

Antonio gravatar imageAntonio ( 2015-01-13 06:42:14 -0500 )edit

@Antonio, thanks for you informative report on OpenCV bug-tracker. I hope this will activate works on that issue. According to attached "cmake-gui-out.txt" file there I have 32-bit version.

Vit gravatar imageVit ( 2015-01-13 08:32:59 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2014-12-30 20:55:15 -0500

I just ran it through the sample code in this link and it worked fine for me.

They used: Canny(imgGray, imgGrayEdges, lowThreshold, lowThreshold*3, 3); where lowThreshold is a value between 0 to 100.

edit flag offensive delete link more



How this this related to my question? It is not just Canny(..) function call, it is a bunch of different functions call, including blur(..) which affects to the input data for Canny. The most important this is not OpenCL UMat (which is the most attractive thing of OpenCV3) version of this code. I should emphasize on OCL aspect...

Vit gravatar imageVit ( 2014-12-31 03:09:10 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2014-12-30 13:09:17 -0500

Seen: 612 times

Last updated: Jan 12 '15