概述
1、读入图像、显示图像、保存图像
下面的程序将会加载一个灰度图, 显示图片,按下’s’键保存后退出,或者
按下 ESC 键退出不保存;学习怎样用 Matplotib 显示图像
import numpy as np
import cv2
from matplotlib import pyplot as plt
if __name__ == '__main__':
cv2.namedWindow('image',cv2.WINDOW_NORMAL)
#load an image in grayscale
img = cv2.imread('image\2.jpg',0)
cv2.imshow('image', img)
k = cv2.waitKey(0)&0xFF
if k == 27:
cv2.destroyAllWindows()
elif k == ord('s'):
cv2.imwrite('lena.jpg', img)
cv2.destroyAllWindows()
#用 Matplotib 显示图像
img1 = cv2.imread('image\4.jpg',0)
plt.imshow(img1,cmap='gray',interpolation = 'bicubic')
plt.xticks([]),plt.yticks([])
plt.show()
- 1
注:彩色图像使用 OpenCV 加载时是 BGR 模式。但是 Matplotib 是 RGB
模式
2、OpenCV 中的绘图函数
使用 OpenCV 绘制不同几何图形,学习这些函数:cv2.line(), cv2.circle(), cv2.rectangle(),cv2.ellipse(), cv2.putText()
上面所有的这些绘图函数需要设置下面这些参数:
• img:你想要绘制图形的那幅图像。
• color:形状的颜色。以 RGB 为例,需要传入一个元组,例如: (255,0,0)代表蓝色。对于灰度图只需要传入灰度值。
• thickness:线条的粗细。如果给一个闭合图形设置为 -1,那么这个图形
就会被填充。默认值是 1.
• linetype:线条的类型, 8 连接,抗锯齿等。默认情况是 8 连接。 cv2.LINE_AA为抗锯齿,这样看起来会非常平滑
import numpy as np
import cv2
from matplotlib import pyplot as plt
if __name__ == '__main__':
img = np.zeros((512,512,3),np.uint8)
cv2.line(img, (0,0), (511,511), (255,0,0),5)
cv2.rectangle(img, (384,0), (510,128), (0,255,0),3)
cv2.circle(img, (447,63), 63, (0,0,255),-1)
cv2.ellipse(img, (256,256), (100,50), 0, 0, 180, 255,-1)
pts = np.array([[10,5],[20,30],[70,20],[50,10]],np.int32)
pts = pts.reshape((-1,1,2))
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, 'OPENCV', (10,500), font, 4, (255,255,255),2)
winname = 'example'
cv2.namedWindow(winname)
cv2.imshow(winname, img)
cv2.waitKey(0)
cv2.destroyWindow(winname)
- 1
结果图:
3、把鼠标当画笔
学习使用 OpenCV 处理鼠标事件,学习的函数是: cv2.setMouseCallback()
import cv2
import numpy as np
drawing = False
mode = True
ix,iy = -1,-1
def draw_circle(event,x,y,flags,param):
global ix,iy,drawing,mode
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
ix,iy = x,y
elif event == cv2.EVENT_MOUSEMOVE and flags == cv2.EVENT_FLAG_LBUTTON:
if drawing == True:
if mode == True:
cv2.rectangle(img, (ix,iy), (x,y), (0,255,0),-1)
else:
cv2.circle(img, (x,y), 3, (0,0,255),-1)
elif event == cv2.EVENT_LBUTTONUP:
drawing == False
if __name__ == '__main__':
img = np.zeros((512,512,3),np.uint8)
cv2.namedWindow('image')
cv2.setMouseCallback('image', draw_circle)
while(1):
cv2.imshow('image', img)
k = cv2.waitKey(20)&0xFF
if k == ord('m'):
mode = not mode
elif k == 27:
break
- 1
回调函数包含两部分,一部分画矩形,一部分画圆圈。这是一个典型的
例子他可以帮助我们更好理解与构建人机交互式程序,比如物体跟踪,图像分
割等。
4、用滑动条做调色板
学会把滑动条绑定到 OpenCV 的窗口,学习函数:cv2.getTrackbarPos(), cv2.creatTrackbar()等
结合上一节的知识,创建一个画板,可以自选各种颜色的画笔绘画各种图
形
import cv2
import numpy as np
def nothing(x):
pass
#while(1):
# cv2.imshow('image', img)
# k = cv2.waitKey(1)&0xFF
# if k == 27:
# break
# r = cv2.getTrackbarPos('R', 'image')
# g = cv2.getTrackbarPos('G', 'image')
# b = cv2.getTrackbarPos('B', 'image')
# s = cv2.getTrackbarPos(switch, 'image')
# if s == 0:
# img[:] = 0
# else:
# img[:] = [b,g,r]
#cv2.destroyAllWindows()
drawing = False
mode = True
ix,iy = -1,-1
def draw_circle(event,x,y,flags,param):
r = cv2.getTrackbarPos('R', 'image')
g = cv2.getTrackbarPos('G', 'image')
b = cv2.getTrackbarPos('B', 'image')
color = (b,g,r)
global ix,iy,drawing,mode
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
ix,iy = x,y
elif event == cv2.EVENT_MOUSEMOVE and flags == cv2.EVENT_FLAG_LBUTTON:
if drawing == True:
if mode == True:
cv2.rectangle(img, (ix,iy), (x,y), color,-1)
else:
cv2.circle(img, (x,y), 3, color,-1)
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
if __name__ == '__main__':
img = np.zeros((512,512,3),np.uint8)
cv2.namedWindow('image')
cv2.createTrackbar('R', 'image', 0, 255, nothing)
cv2.createTrackbar('G', 'image', 0, 255, nothing)
cv2.createTrackbar('B', 'image', 0, 255, nothing)
cv2.setMouseCallback('image', draw_circle)
while(1):
cv2.imshow('image', img)
k = cv2.waitKey(1)&0xFF
if k == ord('m'):
mode = not mode
elif k == 27:
break
- 1
参考OpenCV-Python 中文教程 OpenCV官方教程中文版(For Python)
最后
以上就是乐观毛豆为你收集整理的opencv图像基础操作的全部内容,希望文章能够帮你解决opencv图像基础操作所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复