pythonopencv入门_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)运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值