Canny Edge Detection Assertion Failed Error with Single-Channel Image

I’m working on a Java project using OpenCV for image processing. Specifically, I’m trying to perform edge detection on an image using the Canny function. My image is a simple black and white silhouette of three palm trees.

Here’s the relevant part of my code:


Mat src = Imgcodecs.imread("input.png");

Mat gray = new Mat();
Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);

Mat blurred = new Mat();
Imgproc.GaussianBlur(gray, blurred, new Size(5, 5), 0);

// Convert the image to CV_16SC1 type
Mat blurredConverted = new Mat();
blurred.convertTo(blurredConverted, CvType.CV_16SC1);

// Print the number of channels in the image
System.out.println(blurredConverted.channels());

Mat edges = new Mat();
Imgproc.Canny(blurredConverted, edges, 50, 150);

When I run this code, I get the following error:


Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: OpenCV(4.10.0) C:\GHA-OCV-1\_work\ci-gha-workflow\ci-gha-workflow\opencv\modules\imgproc\src\canny.cpp:941: error: (-215:Assertion failed) _dy.type() == _dx.type() in function 'cv::Canny'
]

The print statement confirms that the blurredConverted image is indeed a single-channel image. I’ve also tried different images and updated OpenCV to version 4.10, but the issue persists.

Any help would be greatly appreciated. Thank you!
(Also sorry if the formatting is bad this is my first time using this forum)

please note, that there are also Canny() versions, which take seperate dy, dy input parameters, so i think, you got the wrong one above (also look at c++ error msg !).

however, i got no idea, where it got the signature wrong (and no java to debug here).

maybe add more params like int apertureSize, boolean L2gradient and see, where / if it picks up the correct one ?