1
votes

I'm facing some general problems regarding the edge detection in an image (the image should be irrelevant for my question).

  1. I want the canny edge detector to ignore a certain pixel value. For example: It should only look for edges if the gray value is not 0. Otherwise there will be "false edges" detected.

    I usually use the cv2.canny function which works quite fast and well. Problem is, it is not customizable. So I took this code of a custom canny edge detector (https://rosettacode.org/wiki/Canny_edge_detector#Python) in order to customize it. It works but it's calculating the edges way too slow (It takes several minutes, whereas the cv2.canny function takes a fraction of a second). This is my first problem.

    Is there another way to make the cv2.canny function "ignore" pixels of a certein value. Imagine somewhere in the picture is a area filled with black (soo the image below). I don't want the edge detector to detect the edge of this black area.

enter image description here

  1. Once I have some clear edges detected in my image, I want to create masks based on those edges. I couldn't find any examples for this online. So if anyone knows where to find a good tutorial on how to create masks from edges it would be great if you could help me out.

Thanks in advance

1
You will likely get a better answer if you provide a sensible image - I can't tell you how annoying it is to spend half an hour answering somebody and then they tell you their image is nothing like what you were imagining because their description is so poor... or their image is not really greyscale but colour... or their image is not a nice PNG but a noisy, colour-deficient, low-quality JPEG... or their image is half-transparent... or their image is 32-bit floats not 8-bit integers... or they work on Windows and don't have any tools available... or libraries...Mark Setchell
Thanks for the comment! No problem. I just edited the post so you get a better idea what I mean.Luca Rüffert
Requests for tutorials and asking multiple questions in one post tend to work badly on stack overflow. Cutting out the 2nd question could help make this more specific.Will
Have a look at stackoverflow.com/help/minimal-reproducible-example. Providing a toy sample and your code would be very helpful.Nikolas Rieble

1 Answers

3
votes

Here's an approach:

  • Calculate your Canny as usual using the fast OpenCV function.

  • Now locate all the black pixels in the image - you can do that with _,thr = cv2.threshold(im,1,255,cv2.THRESH_BINARY) and dilate those areas by 1 pixel with morphology to allow edges to be offset a little as they often are.

  • Multiply the normal Canny image with the mask you created so that anything it found in the black areas gets multiplied by zero, i.e. lost.