2

I am working on an image processing project.

I am using opencv 2.4.11 + python 2.7

The code is simple as

def preprocessing(src):
    src = cv2.resize(src, (0,0), fx=6, fy=6)
    kernel = np.ones((5,5), np.uint8)

    dilate = cv2.dilate(src, kernel, iterations = 1)
    erode = cv2.erode(dilate, kernel, iterations = 1)

    cv2.imshow("dilate", dilate)
    cv2.imshow("erode", erode)

    th, thresh = cv2.threshold(erode, 200, 255, cv2.THRESH_BINARY)
    cv2.imshow("thresh", thresh)

    contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    cv2.drawContours(src,contours,-1,(0,255,0),3)

    cv2.imshow("src", src)
    cv2.waitKey()


print "==== start image processing ====="
im2 = cv2.imread("training1.png",cv2.CV_LOAD_IMAGE_GRAYSCALE)
preprocessing(im2)

But the findcontours returns assertion failure

The output is

OpenCV Error: Assertion failed (step[dims-1] == (size_t)CV_ELEM_SIZE(flags)) in create, file /tmp/opencv20160107-29960-t5glvv/opencv-2.4.12/modules/core/src/matrix.cpp, line 236
Traceback (most recent call last):
  File "process.py", line 55, in <module>
preprocessing(im2)
  File "process.py", line 44, in preprocessing
contours, hierarchy =  cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.error: /tmp/opencv20160107-29960-t5glvv/opencv-2.4.12/modules/core/src/matrix.cpp:236: error: (-215) step[dims-1] == (size_t)CV_ELEM_SIZE(flags) in function create

Can anyone help? Thank you very much

10
  • @BillalBEGUERADJ Sorry for that. I have finished indented. Commented May 7, 2016 at 5:46
  • (a) The error message does not match the code shown. (b) The code, as shown, works fine for me. Commented May 7, 2016 at 6:21
  • @John1024 sorry, i just pick part of the code here. And what version of python and opencv are you using? I found the same question here stackoverflow.com/questions/32922944/… Commented May 7, 2016 at 6:26
  • Python 2.7.9 and opencv 2.4.9 Commented May 7, 2016 at 6:33
  • @John1024 But how can it output the correct result from previous functions. And if I just remove the png from the directory. The cv2.resize() function will throw the exception. Commented May 7, 2016 at 7:13

3 Answers 3

1

The code works fine. The reason is that the image file is missing.

Sign up to request clarification or add additional context in comments.

1 Comment

But how did I get results from previous imshow() function? Is that a bug for opencv?
1

Solution

  1. Unlink opencv from brew.

  2. Download Anaconda package: http://continuum.io/downloads#all and install.

  3. Install openCV: "conda install opencv" which is version 2.4.8.

If there is anaconda conflict with bottleneck, remove the reference and install it again by conda install command.

I still don't know why opencv 2.4.12 throws such exception, I guess it is because the numpy is conflict against opencv.

Comments

0

You need to convert the image into gray scale and then try contour .It worked for me

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.