OpenCV implements StructuredEdgeDetection based on the random forest based approach outlined in "Structured Forests for Fast Edge Detection" (2013) by P. Dollár and C. Zitnick. The authors have published an implementation for Matlab and there's also one for Python, both of which also contain a pretrained model based on the BSDS500 dataset.
The OpenCV implementation seems to be lacking a pretrained model and I'm also unable to uncover what format the only constructor it offers expects:
Ptr<cv::StructuredEdgeDetection> createStructuredEdgeDetection(String model)
The only available constructor
Parameters: model – model file name
The documentation also doesn't outline how to train the OpenCV implementation, so I'm left quite in the dark.
To recap, how to use the OpenCV implementation? Is a trained model available? If not, how to train one using OpenCV?
Canny Edge detection is an Algorithm consisting of 4 major steps: Reduce Noise using Gaussian Smoothing. Compute image gradient using Sobel filter. Apply Non-Max Suppression or NMS to just jeep the local maxima.
How are Edges Detected? Edges are characterized by sudden changes in pixel intensity. To detect edges, we need to go looking for such changes in the neighboring pixels. Come, let's explore the use of two important edge-detection algorithms available in OpenCV: Sobel Edge Detection and Canny Edge Detection.
It is a multi-stage algorithm and we will go through each stages. Noise Reduction. Since edge detection is susceptible to noise in the image, first step is to remove the noise in the image with a 5x5 Gaussian filter. We have already seen this in previous chapters. Finding Intensity Gradient of the Image.
You can use this model from opencv_extra
ximgproc
test data.
If you want to train your own model, you can follow instructions on OpenCV tutorials.
Image:
Edges:
Code:
#include <opencv2\opencv.hpp>
#include <opencv2\ximgproc.hpp>
using namespace cv;
using namespace cv::ximgproc;
int main()
{
Ptr<StructuredEdgeDetection> pDollar = createStructuredEdgeDetection("path_to_model.yml.gz");
Mat3b src = imread("path_to_image");
Mat3f fsrc;
src.convertTo(fsrc, CV_32F, 1.0 / 255.0);
Mat1f edges;
pDollar->detectEdges(fsrc, edges);
imshow("Image", src);
imshow("Edges", edges);
waitKey();
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With