python - 从python中的像素数据保存图像
问题描述
我正在尝试创建一个程序,该程序将从通过 openCV 精明边缘检测获得的像素数据中保存图像。现在,该程序将一个小图像文件保存在正确的路径中,但该图像文件不包含来自网络摄像头的任何数据。
图像文件中应保存的内容示例: 边缘检测房间的图片
与实际保存的内容相比:只是一个黑色矩形
下面的代码:
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import numpy as np
from numpy import asarray
import PIL
from PIL import Image
import cv2
def LiveCamEdgeDetection_canny(image_color):
threshold_1 = 100 #LINES
threshold_2 = 50 #NOISE
image_gray = cv2.cvtColor(image_color, cv2.COLOR_BGR2GRAY)
canny = cv2.Canny(image_gray, threshold_1, threshold_2)
return canny
# Main calling function to initialize webcam and apply edge detection
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow('Live Edge Detection', LiveCamEdgeDetection_canny(frame))
#cv2.imshow('Webcam Video', frame)
#print(LiveCamEdgeDetection_canny(frame))
# Store pixel data
pixels = [LiveCamEdgeDetection_canny(frame)]
image_todraw = np.array(pixels)
image_todraw = np.reshape(image_todraw, (720, 1280))
image_todraw *= 255
image_tosave = Image.fromarray(image_todraw.astype(np.uint8))
image_tosave.save('/Users/user/Desktop/destinationFolder/RETRY.jpeg', 'JPEG')
#print(image_tosave)
if cv2.waitKey(1) == 'p': #13 Enter Key
break
cap.release() # camera release
cv2.destroyAllWindows()
我很感激你能给我的所有帮助!
解决方案
推荐阅读
- angular - 如何在Angular中将绑定值与不同的变量连接起来?
- variables - Windows 命令行脚本嵌入了空白字符
- c++ - 为什么 gl_FragCoord.z 与 ((pos.z / pos.w) + 1.0) * 0.5 不同?
- email - Yii2-批量发送邮件
- python - Pandas 中的列名 (Python)
- python - FP 增长(频繁模式挖掘)使用 Pyspark 给出错误
- php - 尝试通过 php 和 android 将图像上传到服务器时 size = 0
- java - 无法从 ListView 通过 Intent 正确传递数据
- php - PHP 将查询字符串显示为搜索词
- javascript - 我们怎样才能使情节动画流畅