5

I want to use edge detection algorithms from opencv library. Here is a piece of python code:

from opencv.cv import *
from opencv.highgui import *

img = cvLoadImage ('xxx.jpg')
cvNamedWindow ('img')
cvShowImage ('img', img)
cvWaitKey ()

canny = cvCreateImage (cvSize (img.width, img.height), 8, 3)
cvCanny (img, canny, 50, 200)

harris = cvCreateImage (cvSize (img.width, img.height), 8, 3)
cvCornerHarris (img, harris, 5, 5, 0.1)

Loading and showing image works fine, but canny and harris transformations fail.
cvCanny fails with:

RuntimeError:  openCV Error:
    Status=Unsupported format or combination of formats
    function name=cvCanny
    error message=
    file_name=cv/cvcanny.cpp
    line=72

and cvCornerHarris fails with this error:

RuntimeError:  openCV Error:
    Status=Assertion failed
    function name=cvCornerHarris
    error message=src.size() == dst.size() && dst.type() == CV_32FC1
    file_name=cv/cvcorner.cpp
    line=370

From this messages I can infer that loaded image has invalid format. But I don't understand how to convert it.
Here are values of some img fields:

img.type = 1111638032
img.nChannels = 3
img.depth = 8
2
  • 1
    what version of the API are you using? your code seems to be consistant with the old API and could be confusing for new users: you should use "import cv" and such. I'll be happy to translate your code if you wish Commented Feb 16, 2011 at 14:26
  • @meduz, it is python-opencv_2.0.0-3ubuntu2. I installed it from Ubuntu 10.04 repository. Commented Feb 16, 2011 at 16:05

3 Answers 3

8

For other folks interested in the same type of problem I recommend checking out http://simplecv.org

Here is a bit of code I wrote that does line detection on an image acquired from a webcam. It will even display the image over http. beard detection

import SimpleCV
import time

c = SimpleCV.Camera(1)
js = SimpleCV.JpegStreamer() 

while(1):
  img = c.getImage()
  img = img.smooth()
  lines = img.findLines(threshold=25,minlinelength=20,maxlinegap=20)
  [line.draw(color=(255,0,0)) for line in lines]
  #find the avg length of the lines
  sum = 0
  for line in lines:
      sum = line.length() + sum
  if sum:
      print sum / len(lines)
  else:
      print "No lines found!"
  img.save(js.framebuffer)
  time.sleep(0.1)

Check out the project I made this for at http://labs.radiantmachines.com/beard/ It will detect how long your neck beard is :)

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

1 Comment

This is absolutely awesome. +++1 from me!
6

Here's fixed code. See comments inline. Long story short: your data types were wrong. Read the API.

try:
    from opencv.cv import *
    from opencv.highgui import *
except:
    #
    # Different OpenCV installs name their packages differently.
    #
    from cv import *

if __name__ == '__main__':
    import sys
    #
    # 1 = Force the image to be loaded as RGB
    #
    img = LoadImage (sys.argv[1], 1)
    NamedWindow ('img')
    ShowImage ('img', img)
    WaitKey ()

    #
    # Canny and Harris expect grayscale  (8-bit) input.
    # Convert the image to grayscale.  This is a two-step process:
    #   1.  Convert to 3-channel YCbCr image
    #   2.  Throw away the chroma (Cb, Cr) and keep the luma (Y)
    #
    yuv = CreateImage(GetSize(img), 8, 3)
    gray = CreateImage(GetSize(img), 8, 1)
    CvtColor(img, yuv, CV_BGR2YCrCb)
    Split(yuv, gray, None, None, None)

    canny = CreateImage(GetSize(img), 8, 1)
    Canny(gray, canny, 50, 200)
    NamedWindow ('canny')
    ShowImage ('canny', canny)
    WaitKey()

    #
    # The Harris output must be 32-bit float.
    #
    harris = CreateImage (GetSize(img), IPL_DEPTH_32F, 1)
    CornerHarris(gray, harris, 5, 5, 0.1)

5 Comments

Thanks! I read the API but it is not obvious which image format each transformation expects.
btw, both imports (openhighgu and cv) does not work on my ubuntu 10.04
If they didn't work, how did you run the code? That would generally mean the Python wrappers for OpenCV are not installed.
they work like in my source, i.e. everything is imported with cv prefix
Oh, sorry. That means there is a typo in my code -- I must've changed it when I was trying to make it work on my system. I'll go edit it now.
2

You can convert an image to grayscale in a single step instead of two:

gray = cv.CreateMat(img.height, img.width, cv.CV_8UC1)
cv.CvtColor(img, gray, cv.CV_BGR2GRAY)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.