开发者社区> 问答> 正文

OpenCV in Python 入门:报错 

OpenCV是Intel®开源计算机视觉库。它由一系列 C 函数和少量 C++ 类构成,实现了图像处理和计算机视觉方面的很多通用算法。 在这篇文章(译自 http://glowingpython.blogspot.com/2011/10/beginning-with-opencv-in-python.html) 中将介绍如何使用 Python 版的 OpenCV。 下面的代码打开磁盘中的图片,打印一些图片属性,并在一个窗口中显示这个图片:

# load and show an image in gray scale
image = cv.LoadImage('ariellek.jpg',cv.CV_LOAD_IMAGE_GRAYSCALE)

# print some image properties
print 'Depth:',image.depth,'# Channels:',image.nChannels
print 'Size:',image.width,image.height
print 'Pixel values average',cv.Avg(image)

# create the window
cv.NamedWindow('my window', cv.CV_WINDOW_AUTOSIZE)
cv.ShowImage('my window', image) # show the image
cv.WaitKey() # the window will be closed with a (any)key press
我使用的是下面这张图片
在控制台中显示的内容:
Depth: 8 # Channels: 1
Size: 366 550
Pixel values average (80.46735717834079, 0.0, 0.0, 0.0)
现在可对图片更改其大小
# resize the image
dst = cv.CreateImage((150,150), 8, 1)
cv.Resize(image,dst,interpolation=cv.CV_INTER_LINEAR)
cv.ShowImage('my window', dst)
cv.WaitKey()
cv.SaveImage('image2.jpg', dst) # save the image
结果是:
A Sobel operator can be applied as follow:
# Sobel operator
dstSobel = cv.CreateMat(image.height, image.width, cv.CV_32FC1)
cv.Sobel(image,dstSobel,1,1,3)
cv.ShowImage('my window', dstSobel)
cv.WaitKey()
cv.SaveImage('imageSobel.jpg', dstSobel)
运行结果:
最后的例子使用两个操作,平滑过滤和截取操作:
# image smoothing and subtraction
imageBlur = cv.CreateImage(cv.GetSize(image), image.depth, image.nChannels)
# filering the original image
cv.Smooth(image, imageBlur, cv.CV_BLUR, 15, 15)
diff = cv.CreateImage(cv.GetSize(image), image.depth, image.nChannels)
# subtraction (original - filtered)
cv.AbsDiff(image,imageBlur,diff)
cv.ShowImage('my window', diff)
cv.WaitKey()
cv.SaveImage('imageDiff.jpg', diff)
运行结果:

展开
收起
kun坤 2020-06-08 12:19:33 573 0
1 条回答
写回答
取消 提交回答
  • 那我也贴一个用wxwidgets使用opencv的小demo

    http://larryo.org/work/information/wxopencv/index.html

    ######一个小提示,在python里用OpenCV提供的Python模块的时候,需要先安装Numerical Python,否则可能会发生import错误,错误内容一般是:numpy.core.multiarray failed to import.

    2020-06-08 12:19:39
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载