您的位置:首页 > 编程语言 > Python开发

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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: