Python——opencv的图像显示、视频采集、图像处理
2018-12-08 17:31
357 查看
操作界面:
打开摄像头
二值化处理
此demo的缺点是只能通过单步打开关闭摄像头,而不能进行实时的刷新处理。ESC关闭,space保存截图图片。
代码如下:
import cv2 import tkinter as tk from tkinter import filedialog#文件控件 from PIL import Image, ImageDraw, ImageFont,ImageTk#图像控件 import threading#多线程 #---------------创建窗口 window = tk.Tk() window.title('摄像头') sw = window.winfo_screenwidth()#获取屏幕宽 sh = window.winfo_screenheight()#获取屏幕高 wx = 500 wh = 400 window.geometry("%dx%d+%d+%d" %(wx,wh,(sw-wx)/2,(sh-wh)/2-100))#窗口至指定位置 canvas = tk.Canvas(window,bg='#c4c2c2',height=wh,width=wx)#绘制画布 canvas.pack() #---------------打开摄像头获取图片 def binaryThreshold(image, type, thres_Value, a, c): # 创建空图 # grey = np.zeros(image.shape, np.unit8) # 转换为灰度图 grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 二值化, 返回值,第一个参数为阀值,第二个参数为二值化图像 if (type == 1): # 固定阀值 ret, thresh1 = cv2.threshold(grey, thres_Value, 255, cv2.THRESH_BINARY) # 黑白二值 # ret,thresh2 = cv2.threshold(grey,thres_Value,255,cv2.THRESH_BINARY_INV) #黑白二值反转 # ret,thresh3 = cv2.threshold(grey,thres_Value,255,cv2.THRESH_TRUNC) #多像素图像 # ret,thresh4 = cv2.threshold(grey,thres_Value,255,cv2.THRESH_TOZERO) # ret,thresh5 = cv2.threshold(grey,thres_Value,255,cv2.THRESH_TOZERO_INV) if (type == 2): # 自动阀值,自动取灰度直方图两个峰值之间的值 ret, thresh1 = cv2.threshold(grey, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) if (type == 3): # 自适应阀值:获取区域为a*a的像素均值 减去 c 作为这个区域的阀值 thresh1 = cv2.adaptiveThreshold(grey, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, a, c) return thresh1 def video_demo1(): if __name__ == '__main__': # 创建窗口 cv2.namedWindow("camera", 1) # 开启摄像头 capture = cv2.VideoCapture(0) num = 0 area = 7 cc = 8 while (1): # 获取图像 ret, img = capture.read() # 获取图片尺寸 (height, width, channels) = img.shape # 二值化 img = binaryThreshold(img, 3, 127, area, cc) # 写文本到图像上,字体大小:1.2,粗细:2 # pil_img = Image.fromarray(img) # draw = ImageDraw.Draw(pil_img) # font = ImageFont.truetype('simhei.ttf',20,0,encoding='utf-8') # draw.text((50,50),'1.按ESC退出',(0,0,255),font=font) # draw.text((50,80),'2.按空格保存图片',(0,0,255),font=font) # img = np.array(pil_img) # 显示图像 cv2.imshow("camera", img) # 按键事件 key = cv2.waitKey(10) if key == 27: # ord('q'): #画面窗口按ES退出 print("esc break...") break if key == ord(' '): num = num + 1 filename = "img_%s.jpg" % num cv2.imwrite(filename, img) if key == ord('d'): str = input("领域尺寸 area=") area = int(str) str = input("衰减常数 C=") cc = int(str) def video(): if __name__ == '__main__': # 创建窗口 cv2.namedWindow("camera", 1) # 开启摄像头 capture = cv2.VideoCapture(0) while (1): # 获取图像 ret, img = capture.read() # 显示图像 cv2.imshow("camera", img) # 按键事件 key = cv2.waitKey(10) if key == 27: # ord('q'): #画面窗口按ES退出 print("esc break...") break if key == ord(' '): num = num + 1 filename = "img_%s.jpg" % num cv2.imwrite(filename, img) if key == ord('d'): str = input("领域尺寸 area=") area = int(str) str = input("衰减常数 C=") cc = int(str) bt_start = tk.Button(window,text='打开摄像头',height=2,width=15,command=video) bt_start.place(x=200,y=250) bt_start = tk.Button(window, text='二值化处理', height=2, width=15, command=video_demo1) bt_start.place(x=200, y=300) window.mainloop()
相关文章推荐
- opencv进行视频读取和调用摄像头以及对采集到的图像进行闭环处理
- OpenCV-Python 图像与视频处理
- Opencv3.0-python的那些事儿:(一)、Opencv的图像和视频处理基本用法
- Python-OpenCV 图像与视频处理
- Python-OpenCV 图像与视频处理
- Python-OpenCV 图像与视频处理
- 利用Python+opencv进行视频文件的读取和保存,打开笔记本摄像头拍照保存、图像在窗口显示等操作
- python opencv入门(读 / 写 显示以及转换图像 和 视频读入)
- 5-python图像处理opencv(1.读图,显示,转换,存储)
- Python图像处理(3):视频显示
- Python-OpenCV 图像与视频处理
- 利用FPGA和USB总线的视频图像的采集与处理系统设计
- 用python简单处理图片(1):打开\显示\保存图像
- Python3与OpenCV3.3 图像处理(二十六)--分水岭算法(纯代码)
- opencv python 显示图像
- Python3与OpenCV3.3 图像处理(二十五)--开闭操作(补充)
- python for OpenCV图像处理之模板匹配以及分水岭算法
- Python OpenCV处理图像之滤镜和图像运算
- 【Python】opencv显示图像
- Python下使用OpenCV——图像显示与保存