|
楼主 |
发表于 2023-5-30 19:26:24
|
显示全部楼层
谢谢大佬,可以查看照片了,但现在又有一个问题,我该怎么对图片进行处理。我写的程序是这样的
import cv2
def video_demo():
print('开始')
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW) # 电脑自身摄像头
i = 0 # 定时装置初始值
photoname = 1 # 文件名序号初始值
while True:
i = i + 1
reg, frame = cap.read()
frame = cv2.flip(frame, 1) # 图片左右调换
cv2.imshow('window', frame)
if i == 50: # 定时装置,定时截屏,可以修改。
filename = str(photoname) + '.png' # filename为图像名字,将photoname作为编号命名保存的截图
filepath = 'E:/asdfghj' + '\\' + filename
cv2.imwrite('E:/asdfghj' + '\\' + filename, frame) # 截图 前面为放在桌面的路径 frame为此时的图像
print(filename + '保存成功') # 打印保存成功
i = 0 # 清零
#读取图片
img = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
#变HSV
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
c_low1 = (85,150,150)
c_high1 = (95,255,255)
#大津法二值化
dst = cv2.inRange(hsv,c_low1,c_high1)
cv2.imshow('Read Image', img)
cv2.waitKey(0)
photoname = photoname + 1
if photoname >= 2: # 最多截图20张 然后退出(如果调用photoname = 1 不用break为不断覆盖图片)
# photoname = 1
break
if cv2.waitKey(1) & 0xff == ord('q'):
break
# 释放资源
cap.release()
video_demo()
cv2.destroyAllWindows()
然后这个照片也显示不出来了,显示的结果如下
Traceback (most recent call last):
File "D:\pythonProject\tuxiangduqushiyantwo\main.py", line 42, in <module>
video_demo()
File "D:\pythonProject\tuxiangduqushiyantwo\main.py", line 25, in video_demo
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
cv2.error: OpenCV(4.7.0) d:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.simd_helpers.hpp:92: error: (-2:Unspecified error) in function '__cdecl cv::impl::`anonymous-namespace'::CvtHelper<struct cv::impl::`anonymous namespace'::Set<3,4,-1>,struct cv::impl::A0x416a7d27::Set<3,-1,-1>,struct cv::impl::A0x416a7d27::Set<0,5,-1>,2>::CvtHelper(const class cv::_InputArray &,const class cv::_OutputArray &,int)'
> Invalid number of channels in input image:
> 'VScn::contains(scn)'
> where
> 'scn' is 1
请问这个该怎么办呢 |
|