3

I'm working currently on a project where I use EmguCV 4.2

I need to use Canny function:

gray=gray.Canny(50, 200);

And it's throwing error:

 _src.depth()==CV_8U

I noticed that exception occurs only if I use CvInvoke.Threshold() on my image earlier

CvInvoke.Threshold(skeleton,img,0,255,Emgu.CV.CvEnum.ThresholdType.Binary);

I'm wondering why is this happening, without the Threshold() function everything works. Is this function changing the depth of my image somehow? How do I convert it back to use the Canny() function without problems?

Thanks in advance.

shefff
  • 117
  • 7
  • is the gray image in your Canny function actually gray? Can you post your full code please? There might be an issue elsewhere in your program. – Aaron Jones Apr 05 '20 at 20:47

1 Answers1

3

From what I can gather from your question, there might be an issue with your conversion of your image to gray scale.

The error code below refers the the depth type of your image, i.e, how many color values can fit inside each individual pixel of your image. In your case, with gray scale images, since you only hold values from black to white with different variants of gray in between, the depth type of the image will be smaller.

_src.depth()==CV_8U

If you just want to pass an image to the Canny function, then you must convert it to gray scale first.

// Read in the image from a file path
Mat img = CvInvoke.Imread(filePath, ImreadModes.AnyColor);

// Convert the image to gray scale
Image<Gray, byte> gray = img.ToImage<Gray, byte>();

// Threshold the image
CvInvoke.Threshold(gray, gray, 0, 100, ThresholdType.Binary);

// Canny the thresholded image
gray = gray.Canny(50, 200);

If you just want to pass an image to the Canny function without passing it through a threshold, then you can use the code below.

// Read in the image from a file path
Mat img = CvInvoke.Imread(filePath, ImreadModes.AnyColor);

// Convert the image to gray scale
Image<Gray, byte> gray = img.ToImage<Gray, byte>();

// Canny the thresholded image
gray = gray.Canny(50, 200);
Aaron Jones
  • 1,140
  • 1
  • 9
  • 26
  • Thanks for your answer. Maybe I've put to little code in my question - sorry for that. I already have the conversion to grayscale in my code: `void GetLines(Image img){ Image gray = img.Convert(); //there goes code for calculating skeleton, then I use the threshold and canny function..` That's not the problem. I know I can pass image to the Canny function without passing it through a threshold - like I wrote above I tried and it's working. But the Threshold is required in my project and that's why I'm asking how to fix this problem. – shefff Apr 05 '20 at 21:51
  • The above methods that I posted have worked for me in the past. What are the pixel dimensions of your image? – Aaron Jones Apr 05 '20 at 21:54
  • Dimensions are not constant. They can be for example 300x300 or even 2500x1400 – shefff Apr 05 '20 at 21:56
  • These methods could work for you, because in Threshold for the InputArray you are using 'gray' and I use skeleton which is calculated using MorphologyEx function, and then it's little edited to eliminate some pixels. – shefff Apr 05 '20 at 21:58
  • Can you edit your original post with more code examples attributing to the problem? – Aaron Jones Apr 05 '20 at 21:59
  • It's most likely the MorphologyEx throwing the error, and when you send the output of that function into the Threshold, it cant compute it and errors out. – Aaron Jones Apr 06 '20 at 00:41
  • 2
    You are right, my output of MorphologyEx function depth is Cv32F. Is it there a method that can convert it to Cv8U which I need? – shefff Apr 06 '20 at 11:20
  • 2
    Ok I figured it out. I did the conversion using this line of code: `img.Mat.ConvertTo(img, Emgu.CV.CvEnum.DepthType.Cv8U);`. Now the exception is not showing, so I assume the problem is solved. Thank you very much for your time! – shefff Apr 06 '20 at 11:30