《OpenCV入门指南》系列文章地址:http://blog.csdn.net/morewindows/article/category/1291764
安装好 OpenCV 之后,首先尝试加载一张最简单的图片并显示出来,代码示例:
第一种方式使用cv2.cv的LoadImage、ShowImage和SaveImage函数
importcv2.cvascv
# 读图片
image=cv.LoadImage('img/image.png', cv.CV_LOAD_IMAGE_COLOR)#Load the image
#Or just: image=cv.LoadImage('img/image.png')
cv.NamedWindow('a_window', cv.CV_WINDOW_AUTOSIZE)#Facultative
cv.ShowImage('a_window', image)#Show the image
# 写图片
cv.SaveImage("thumb.png", thumb)
cv.WaitKey(0)#Wait for user input and quit
也可以直接使用cv2的imread、imwrite和imshow函数
importnumpyasnp
importcv2
img = cv2.imread('messi5.jpg',0)
cv2.imshow('image',img)
k = cv2.waitKey(0)
ifk ==27:# wait for ESC key to exit
cv2.destroyAllWindows()
elifk == ord('s'):# wait for 's' key to save and exit
cv2.imwrite('messigray.png',img)
cv2.destroyAllWindows()
imread函数还可以定义加载的mode,默认是以RGB模式处理图片:
importcv2
grayImage = cv2.imread('MyPic.png', cv2.CV_LOAD_IMAGE_GRAYSCALE)
# 可选参数CV_LOAD_IMAGE_COLOR (BGR), CV_LOAD_IMAGE_GRAYSCALE (grayscale), CV_LOAD_IMAGE_UNCHANGED(neither)
cv2.imwrite('MyPicGray.png', grayImage)
importcv2
img = cv2.imread('img/image.png')
printimg.shape
# (640, 640, 3)
printimg.size
# 1228800
printimg.dtype
# uint8
# 在debug的时候,dtype很重要
在处理图片的时候,我们经常会需要把一些信息直接以文字的形式输出在图片上,下面的代码将实现这个效果:
importcv2.cvascv
image=cv.LoadImage('img/lena.jpg', cv.CV_LOAD_IMAGE_COLOR)#Load the image
font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX,1,1,0,3,8)#Creates a font
y = image.height /2# y position of the text
x = image.width /4# x position of the text
cv.PutText(image,"Hello World !", (x,y),font, cv.RGB(255,255,255))#Draw the text
cv.ShowImage('Hello World', image)#Show the image
cv.WaitKey(0)
cv2:
cv2.putText(frame,'Hello World', (300,100),0,0.5, (0,0,255),2)
下面的例子将实现缩放图片并保存,这个在使用 OpenCV 做图像处理的时候都是很常用的操作:
importcv2.cvascv
im = cv.LoadImage("img/alkaline.jpg")#get the image
thumb = cv.CreateImage((im.width /2, im.height /2),8,3)#Create an image that is twice smaller than the original
cv.Resize(im, thumb)#resize the original image into thumb
#cv.PyrDown(im, thumb)
cv.SaveImage("thumb.png", thumb)# save the thumb image
cv2:
importcv2
importnumpyasnp
img = cv2.imread('messi5.jpg')
res = cv2.resize(img,None,fx=2, fy=2, interpolation = cv2.INTER_CUBIC)
#OR
height, width = img.shape[:2]
res = cv2.resize(img,(2*width,2*height), interpolation = cv2.INTER_CUBIC)
importcv2
importnumpyasnp
img = cv2.imread('messi5.jpg',0)
rows,cols = img.shape
M = np.float32([[1,0,100],[0,1,50]])
dst = cv2.warpAffine(img,M,(cols,rows))
cv2.imshow('img',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread('messi5.jpg',0)
rows,cols = img.shape
M = cv2.getRotationMatrix2D((cols/2,rows/2),90,1)
dst = cv2.warpAffine(img,M,(cols,rows))
importcv2
importnumpyasnp
img = cv2.imread('mao.jpg')
rows,cols,ch = img.shape
pts1 = np.float32([[50,50],[200,50],[50,200]])
pts2 = np.float32([[10,100],[200,50],[100,250]])
M = cv2.getAffineTransform(pts1,pts2)
dst = cv2.warpAffine(img,M,(cols,rows))
cv2.imshow('image',dst)
cv2.waitKey(0)
实际使用过程中,我们经常也需要对一些图片的颜色空间做一些改变之类的:
c2.cv:
importcv2.cvascv
im=cv.LoadImage('img/fruits.jpg',cv.CV_LOAD_IMAGE_COLOR)
res = cv.CreateImage(cv.GetSize(im), cv.CV_8UC2,3)#cv.CV_32F, cv.IPL_DEPTH_16S, ...
cv.Convert(im, res)
cv.ShowImage("Converted",res)
res2 = cv.CreateImage(cv.GetSize(im), cv.CV_8UC2,3)
cv.CvtColor(im, res2, cv.CV_RGB2BGR)# HLS, HSV, YCrCb, ....
cv.ShowImage("CvtColor", res2)
cv.WaitKey(0)
cv.Convert():将图片从一个颜色空间转到另一个颜色空间
cv.CvtColor(src, dst, code):
cv2:
cv2.cvtColor(input_image, flag)函数实现图片颜色空间的转换,flag 参数决定变换类型。如 BGR->Gray flag 就可以设置为 cv2.COLOR_BGR2GRAY 。
一个简单的例子,下面的代码实现识别摄像视频中蓝色的部分:
importcv2
importnumpyasnp
cap = cv2.VideoCapture(0)
while(1):
# 读取视频的每一帧
_, frame = cap.read()
# 将图片从 BGR 空间转换到 HSV 空间
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# 定义在HSV空间中蓝色的范围
lower_blue = np.array([110,50,50])
upper_blue = np.array([130,255,255])
# 根据以上定义的蓝色的阈值得到蓝色的部分
mask = cv2.inRange(hsv, lower_blue, upper_blue)
res = cv2.bitwise_and(frame,frame, mask= mask)
cv2.imshow('frame',frame)
cv2.imshow('mask',mask)
cv2.imshow('res',res)
k = cv2.waitKey(5) &0xFF
ifk ==27:
break
cv2.destroyAllWindows()
以上的代码给出了视频中获取兴趣对象的基本思想。
对于一张图片的 R、G、B 通道,我们可以很方便的使用 OpenCV 获取并分离或者合并:
(这是将图像灰度化处理的一种方式)
cv2.cv
importcv2.cvascv
orig = cv.LoadImage('img/fruits.jpg')
b = cv.CreateImage(cv.GetSize(orig), orig.depth,1)
g = cv.CloneImage(b)
r = cv.CloneImage(b)
cv.Split(orig, b, g, r,None)
merged = cv.CreateImage(cv.GetSize(orig),8,3)
cv.Merge(g, b, r,None, merged)
cv.ShowImage("Image", orig)
cv.ShowImage("Blue", b)
cv.ShowImage("Green", g)
cv.ShowImage("Red", r)
cv.ShowImage("Merged", merged)
cv.WaitKey(0)
cv2
importcv2
img = cv2.imread('img/image.png')
b,g,r = cv2.split(img)
img = cv2.merge((b,g,r))
cv2.copyMakeBorder函数
importcv2
importnumpyasnp
BLUE = [255,0,0]
img1 = cv2.imread('opencv_logo.png')
replicate = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REPLICATE)
reflect = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT)
reflect101 = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT_101)
wrap = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_WRAP)
constant= cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_CONSTANT,value=BLUE)