opencv入门--opencv2
读取图片
import cv2 as cv
src=cv.imread("./3.jpg",cv.IMREAD_GRAYSCALE)
cv.namedWindow("input",cv.WINDOW_AUTOSIZE)
cv.imshow("input",src)
cv.waitKey(0)
cv.destroyAllWindows()
几种创建初始图像的方法
import cv2 as cv
import numpy as np
def read_write_image():
src = cv.imread("./3.jpg", cv.IMREAD_ANYCOLOR)
cv.imshow("imnput", src)
cv.imwrite("./4.png", src)
print(src)
def creation_image():
src = cv.imread("./3.jpg", cv.IMREAD_ANYCOLOR)
cv.imshow("input", src)
dst = np.copy(src)
dst.fill(127)
cv.imshow("det", dst)
#直接创建空白图片
blank=np.zeros([400,400],dtype=np.uint8)
cv.imshow("blankl",blank)
#第三种方法创建
t3=np.zeros([4000],dtype=np.uint8)
t3.reshape([200,20])
cv.imshow("t3",t3)
#希望创建的图片大小和输入图片大小保持一致
clone=np.zeros(src.shape,src.dtype)
cv.imshow("clone",clone)
cv.imwrite("./clone.png",clone)
#随机生成图像
t5=np.random.random_sample([400,400])*255
cv.imshow("t5",t5)
t6=np.uint8(t5)
cv.imshow("t6",t6)
if __name__ == '__main__':
creation_image()
cv.waitKey(0)
cv.destroyAllWindows()
简单的图形绘制
import cv2 as cv
import numpy as np
def draw_graphics_demo():
src = np.zeros([400, 400, 3], dtype=np.uint8)
# cv.imshow("原图", src)
# 绘制直线
cv.line(src, (10, 10), (400,400),(255, 0, 0), 4, cv.LINE_8, 0)#哪一个图像,开始点,结束点
cv.line(src, (400, 10), (10, 400), (0, 255, 255), 4, cv.LINE_8, 0)
#绘制方框
cv.rectangle(src,(100,100),(300,300),(0,255,0),4,cv.LINE_8,0)
#绘制圆
cv.circle(src,(250,250),150,(255,255,0),4,cv.LINE_8,0)
#绘制椭圆
cv.ellipse(src,(250,250),(150,50),0,300,300,(255,0,255),4,cv.LINE_8,0)
#绘制文本
cv.putText(src,"hello cv",(50,50),cv.FONT_HERSHEY_PLAIN,1.2,(0,0,155),0)
cv.imshow("opencv", src)
if __name__ == '__main__':
draw_graphics_demo()
cv.waitKey(0)
cv.destroyAllWindows()
#运行结果
多边形图像绘制,随机绘制彩色直线
import cv2 as cv
import numpy as np
def draw_graphics_demo():
src = np.zeros([400, 400, 3], dtype=np.uint8)
# cv.imshow("原图", src)
# 绘制直线
cv.line(src, (10, 10), (400, 400), (255, 0, 0), 4, cv.LINE_8, 0) # 哪一个图像,开始点,结束点
cv.line(src, (400, 10), (10, 400), (0, 255, 255), 4, cv.LINE_8, 0)
# 绘制方框
cv.rectangle(src, (100, 100), (300, 300), (0, 255, 0), 4, cv.LINE_8, 0)
# 绘制圆
cv.circle(src, (250, 250), 150, (255, 255, 0), 4, cv.LINE_8, 0)
# 绘制椭圆
cv.ellipse(src, (250, 250), (150, 50), 0, 300, 300, (255, 0, 255), 4, cv.LINE_8, 0)
# 绘制文本
cv.putText(src, "hello cv", (50, 50), cv.FONT_HERSHEY_PLAIN, 1.2, (0, 0, 155), 0)
cv.imshow("opencv", src)
# 绘制多边形
points = []
src2 = np.zeros([400, 400, 3], dtype=np.uint8)
points.append((100, 100))
points.append((100, 50))
points.append((200, 100))
points.append((200, 300))
points.append((100, 300))
index = 0 # 循环里面要用到的索引
for point in points:
cv.line(src2, point, points[(index + 1) % 5], (0, 0, 255), 4, cv.LINE_8, 0)
index += 1
# 填充
cv.rectangle(src2, (100, 100), (400, 400), (0, 255, 0), -1, cv.LINE_8, 0)
cv.imshow("src2", src2)
src3 = np.zeros([600, 600, 3], dtype=np.uint8)
for i in range(1000): # 生成随机颜色随机位置的直线
x1 = np.int(np.random.rand() * 600)
y1 = np.int(np.random.rand() * 600)
x2 = np.int(np.random.rand() * 600)
y2 = np.int(np.random.rand() * 600)
b = np.random.randint(0, 255)
g = np.random.randint(0, 255)
r = np.random.randint(0, 255)
cv.line(src3, (x1, y1), (x2, y2), (b, g, r), 4, cv.LINE_8, 0)
cv.imshow("random",src3)
c = cv.waitKey(20)
if c == 27: # 退出
break
if __name__ == '__main__':
draw_graphics_demo()
cv.waitKey(0)
cv.destroyAllWindows()
运行
相应鼠标事件
import cv2 as cv
import numpy as np
def my_mouse_callback(event, x, y, flag, params):
"""
事件处理函数
:param event: 事件
:param x: x轴
:param y: y轴
:param flag:
:param params:用户数据
:return:
"""
if event == cv.EVENT_LBUTTONDBLCLK: # 如果用户双击就绘制一个圆
b = np.random.randint(0, 255)
g = np.random.randint(0, 255)
r = np.random.randint(0, 255)
cv.circle(params, (x, y), 50, (r, b, g), 2, cv.LINE_8, 0)
if event==cv.EVENT_LBUTTONDOWN:#鼠标是一直按着的画 x1 = np.int(np.random.rand() * 600)
y1 = np.int(np.random.rand() * 600)
x2 = np.int(np.random.rand() * 600)
y2 = np.int(np.random.rand() * 600)
x1 = np.int(np.random.rand() * 600)
b = np.random.randint(0, 255)
g = np.random.randint(0, 255)
r = np.random.randint(0, 255)
cv.line(params, (x1, y1), (x2, y2), (b, g, r), 4, cv.LINE_8, 0)
def mouse_demo():
src = np.zeros((512, 512, 3), dtype=np.uint8)
cv.namedWindow("mouse_demo", cv.WINDOW_AUTOSIZE) # 给窗口添加一个名称
cv.setMouseCallback("mouse_demo", my_mouse_callback, src)
while True:
cv.imshow("mouse_demo", src)
c = cv.waitKey(20)
if c == 27:
break
if __name__ == '__main__':
mouse_demo()
cv.waitKey(0)
cv.destroyAllWindows()
这里的用户数据其实也就是初始化的图像
滑块
createTrackbar 滑块创建
getTrackbarPos 响应函数
import numpy as np
import cv2 as cv
def do_nothing():
pass
def track_bar_demo():
src = np.zeros((512, 512, 3), dtype=np.uint8)
cv.namedWindow("tb_demo", cv.WINDOW_AUTOSIZE)
cv.createTrackbar("B", "tb_demo", 0, 255, do_nothing)
cv.createTrackbar("G", "tb_demo", 0, 255, do_nothing)
cv.createTrackbar("R", "tb_demo", 0, 255, do_nothing)
while True:
b = cv.getTrackbarPos("B", "tb_demo")
g = cv.getTrackbarPos("G", "tb_demo")
r = cv.getTrackbarPos("R", "tb_demo")
src[:] = [b, g, r]
cv.imshow("tb_demo", src)
c = cv.waitKey(15)
if c == 17:
break
if __name__ == '__main__':
track_bar_demo()
这个还是蛮重要的,以后调试参数的时候就可用这个滑块
调节RGB数值改变颜色
像素操作
import cv2 as cv
import numpy as np
def pixel_demo():
src=cv.imread("./3.jpg")
print(src.shape)
h,w,ch=src.shape
cv.imshow("input",src)
for row in range(h):
for cow in range(w):
b,g,r=src[row,cow]
b=255-b
g=255-g
r=255-r
src[row,cow]=[b,b,b]
cv.imshow("output",src)
if __name__ == '__main__':
pixel_demo()
cv.waitKey(0)
cv.destroyAllWindows()
对像素值取反