【问题标题】:how to process image with opencv in python?如何在python中使用opencv处理图像?
【发布时间】:2011-02-15 11:16:05
【问题描述】:

我想使用 opencv 库中的边缘检测算法。 这是一段python代码:

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)

加载和显示图像工作正常,但 canny 和 harris 转换失败。
cvCanny 失败并显示:

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

并且cvCornerHarris 失败并出现此错误:

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

从这条消息中,我可以推断出加载的图像格式无效。但我不明白如何转换它。
以下是一些img 字段的值:

img.type = 1111638032
img.nChannels = 3
img.depth = 8

【问题讨论】:

  • 您使用的是什么版本的 API?您的代码似乎与旧 API 一致,可能会让新用户感到困惑:您应该使用“import cv”等。如果您愿意,我很乐意翻译您的代码
  • @meduz,它是 python-opencv_2.0.0-3ubuntu2。我是从 Ubuntu 10.04 存储库安装的。

标签: python image-processing opencv


【解决方案1】:

对于其他对相同类型问题感兴趣的人,我建议查看http://simplecv.org

这是我编写的一段代码,用于对从网络摄像头获取的图像进行线检测。它甚至会通过 http 显示图像。

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)

查看我在http://labs.radiantmachines.com/beard/ 制作的项目,它会检测你脖子上的胡须有多长:)

【讨论】:

  • 这真是太棒了。 +++1 来自我!
【解决方案2】:

这是固定代码。见内联 cmets。长话短说:您的数据类型错误。阅读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)

【讨论】:

  • 谢谢!我阅读了 API,但不清楚每个转换需要哪种图像格式。
  • 顺便说一句,两个导入(openhighgucv)在我的 ubuntu 10.04 上都不起作用
  • 如果它们不起作用,您是如何运行代码的?这通常意味着未安装 OpenCV 的 Python 包装器。
  • 它们的工作方式与我的源代码一样,即所有内容都以 cv 前缀导入
  • 哦,对不起。这意味着我的代码中有一个错字——当我试图让它在我的系统上工作时,我一定已经更改了它。我现在去编辑它。
【解决方案3】:

您可以通过一个步骤而不是两个步骤将图像转换为灰度:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多