Ask Your Question

Python Opencv Structured Forests Edge Detection TypeError

asked 2019-03-01 05:26:38 -0600

When I call the OpenCV Structured Forests Edge Detection in Python as shown below, I get an error:

import numpy as np
import cv2

img = cv2.imread('2009_005193.jpg')
gray_img = np.asarray(img.mean(axis=2) / 255, np.float32)
out = cv2.ximgproc_StructuredEdgeDetection.detectEdges(gray_img)

The error I get is:

Traceback (most recent call last):
  File "", line 19, in <module>
    out = cv2.ximgproc_StructuredEdgeDetection.detectEdges(gray_img)
TypeError: descriptor 'detectEdges' requires a 'cv2.ximgproc_StructuredEdgeDetection' object but received a 'numpy.ndarray'

In the documentation (link to documentation), detectEdges() is present under ximgproc_StructuredEdgeDetection.

I am not sure what I am doing wrong.

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted

answered 2019-03-01 06:17:24 -0600

LBerger gravatar image

updated 2019-03-01 06:18:49 -0600

you need a model : see example in c++ and in python

You will need model in :


The structered edge demo requires you to provide a model. This model can be found at the opencv_extra repository on Github on the following link:


edit flag offensive delete link more

answered 2019-03-01 06:47:55 -0600

supra56 gravatar image

Try this:

import cv2
img = cv2.imread('2009_005193.jpg')
edgedetector = cv2.ximgproc.createStructuredEdgeDetection('./model.yml')
src = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
edges = edgedetector.detectEdges(np.float32(src) / 255.0)
cv2.imshow("edges", edges)
edit flag offensive delete link more

answered 2019-03-01 06:18:54 -0600

berak gravatar image

cv2.ximgproc_StructuredEdgeDetection is not a function, but a class, you first have to create an instance of it:

ed = cv2.ximgproc.createStructuredEdgeDetection(model)

which takes the path to the pretrained edge detection model file as argument. (you probably need to download it first)

then you can use it like:

dst = ed.computeEdges(src)

you can also look at the builtin help cmd:

>>> help(cv2.ximgproc_StructuredEdgeDetection)
edit flag offensive delete link more

Question Tools

1 follower


Asked: 2019-03-01 05:26:38 -0600

Seen: 2,189 times

Last updated: Mar 01 '19