import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
def photo_save():
capture = cv.VideoCapture(0, cv.CAP_DSHOW)
while True:
ret, frame = capture.read() #ret=return,frame是视频的每一帧
frame = cv.flip(frame, 1) #镜像倒转 1是正像-1是反像
if ret:
cv.imshow("video", frame) # 显示图像的每一帧
if cv.waitKey(1) & 0xFF == ord('q'):
cv.imwrite("D:/summer/test.png", frame)
file_name = "xieyang.jpg"
cv.imwrite(file_name, frame)
break
def binary(image): #对图像进行二值化处理
gray = cv.cvtColor(image, cv.COLOR_RGB2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU) # 进行图像二值化
cv.imshow("binary", binary)
def image_hist(image): #展示图像的直方图和灰度图
image = cv.imread("C:/Users/ZMZ/Desktop/aidu.jpg")
gray = cv.cvtColor(image, cv.COLOR_RGB2GRAY)
plt.subplots(311), plt.imshow(gray, "gray")
plt.title("input image"), plt.xticks([]), plt.yticks([])
ret1, th1 = cv.threshold(gray, 0, 255, cv.THRESH_OTSU)
plt.subplot(312), plt.hist(gray.ravel(), 256)
plt.axvline(x=ret1, color='red', label='otsu')
plt.legend(loc='upper right')
plt.title('Histogram'), plt.xticks([]), plt.yticks([])
plt.subplot(313),plt.imshow(th1, "gray")
plt.title("output image"), plt.xticks([]), plt.yticks([])
plt.show()
def dilate(image): #对图像进行膨胀化处理
print(image.shape)
gray = cv.cvtColor(image, cv.COLOR_RGB2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU) #进行图像二值化
cv.imshow("binary", binary)
kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3)) #图像的结构元素
dst = cv.dilate(binary, kernel) #图像膨胀
cv.imshow("erode_demo", dst)
photo_save()
src = cv.imread("D:/summer/test.png")
cv.namedWindow('input_image', cv.WINDOW_AUTOSIZE)
cv.imshow('input_image', src)
dilate(src)
cv.waitKey(0)
cv.destroyAllWindows()
|