import cv2# 打开图片并显示img = cv2.imread("resources/lena.png")cv2.imshow("Lena", img)cv2.waitKey(0)# 打开视频并显示cap = cv2.VIDeoCapture("resources/test_vIDeo.mp4")while True: success, img = cap.read() if not success: break cv2.imshow("vIDeo", img) if cv2.waitKey(20) & 0xFF == ord('q'): # 关于waitKey:https://blog.csdn.net/weixin_44049693/article/details/106271643 break# 打开摄像头并显示cap = cv2.VIDeoCapture(0) # 0对应笔记本摄像头while True: success, img = cap.read() if not success: break cv2.imshow("vIDeo", img) if cv2.waitKey(20) & 0xFF == ord('q'): break# 打开摄像头plus版本frameWIDth = 640frameHeight = 480cap = cv2.VIDeoCapture(0, cv2.CAP_DSHOW) # 第二个参数可以避免在退出时报错,默认为cv2.CAP_ANY# https://docs.opencv.org/3.4/d0/da7/vIDeoio_overvIEw.HTML# https://www.cnblogs.com/cyssmile/p/12611843.HTMLcap.set(3, frameWIDth) # 3 4 10没有实际意义,类似于功能号cap.set(4, frameHeight)cap.set(10, 150) # 设置亮度while True: success, img = cap.read() cv2.imshow("Result", img) if cv2.waitKey(1) & 0xFF == ord('q'): breakcap.release() # 释放摄像头cv2.destroyAllwindows()
几个注意点:
waitKe(): https://blog.csdn.net/weixin_44049693/article/details/106271643摄像头关闭时要释放,同时打开摄像头时,使用接口cv2.CAP_DSHOW,经测试cv2.CAP_ANY, cv2.CAP_MSMF, cv2.CAP_V4L都会有报错。目前不知道具体的原因。二、彩图、灰度图、二值图1、彩图与灰度图import cv2import numpy as npimport timeimg = cv2.imread("resources/LenaPlus.png")img = cv2.resize(img, (500, 250))height, wIDth, channel = img.shape# img[i][j] i为行,j为列img_gray = np.zeros([height, wIDth])# 这里可以直接生成默认的全零矩阵,但是在显示图像之前需要把数据类型转换为 uint8img_red = np.zeros([height, wIDth], np.uint8)# 也可以直接生成 uint8 的全零矩阵,后期就不需要再次变换img_green = np.zeros([height, wIDth], np.uint8)img_blue = np.zeros([height, wIDth], np.uint8)i = 0for row in img: j = 0 for pixel in row: # print(pixel) # 某个打印值:[127 117 195],对应BGR三个通道 gray = int((int(pixel[0]) + int(pixel[1]) + int(pixel[2]))/3) img_gray[i][j] = gray blue, green, red = pixel img_red[i][j] = red img_green[i][j] = green img_blue[i][j] = blue j += 1 i += 1img_gray = np.uint8(img_gray)cv2.imshow("Lena gray", img_gray)cv2.imshow("Lena original", img)cv2.imshow("Lena red", img_red)cv2.imshow("Lena green", img_green)cv2.imshow("Lena blue", img_blue)cv2.waitKey(0)cv2.destroyAllwindows()
打开的图像 img 可以像处理多维列表一样处理,通过对每一个像素的遍历,再对三个色彩通道的值取平均产生了一张灰度图,同时得到了三个颜色通道对应的图像,如下图:
2、灰度图二值化(比较了三个方法的运行时间)import cv2import numpy as npimport time# 灰度图简单二值化img = cv2.imread("resources/LenaPlus.png")img = cv2.resize(img, (500, 250)) # 修改图像大小height, wIDth, channel = img.shapeimg_gray = cv2.cvtcolor(img, cv2.color_BGR2GRAY)# 遍历方法print("二值化,遍历方法:")img_binary = np.zeros([height, wIDth], np.uint8)start = time.time()for i in range(height): for j in range(wIDth): img_binary[i][j] = 255 if img_gray[i][j] > 125 else 0cv2.imshow("Lena binary", img_binary)end = time.time()print("Running time: %s Seconds" % (end-start))# 矩阵方法print("二值化,矩阵方法:")start = time.time()img_bool = img_gray > 125img_binary2 = img_bool * 255cv2.imshow("Lena binary2", np.uint8(img_binary2))end = time.time()print("Running time: %s Seconds" % (end-start))# 库函数方法print("二值化,库函数方法:")start = time.time()ret, img_binary3 = cv2.threshold(img_gray, 125, 255, cv2.THRESH_BINARY)# https://blog.csdn.net/zj360202/article/details/79165796cv2.imshow("Lena binary3", np.uint8(img_binary3))end = time.time()print("Running time: %s Seconds" % (end-start))cv2.waitKey(0)cv2.destroyAllwindows()
遍历方法: 遍历每一个像素,大于125输出255,否则输出0
矩阵方法:
使用 img_gray > 125 进行比较,输出一个 bool 类型的矩阵。在交互窗口进行了上图测试。所以将输出的矩阵直接*255,得到二值图。
库函数方法: 参考了https://blog.csdn.net/zj360202/article/details/79165796
实验结果与各个方法运行时间见下图(图像较大时,方法2可能比方法3快):
总结以上是内存溢出为你收集整理的数字图像处理笔记(一)(基于python与OpenCV)全部内容,希望文章能够帮你解决数字图像处理笔记(一)(基于python与OpenCV)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)