python Intel Realsense D435 图像自动采集程序 自动剔除模糊图像
生活随笔
收集整理的這篇文章主要介紹了
python Intel Realsense D435 图像自动采集程序 自动剔除模糊图像
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
代碼:
# -*- encoding: utf-8 -*- """ @File : save_image.py @Time : 2019/10/23 14:44 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """ import pyrealsense2 as rs import numpy as np import cv2 import os import time from Judge import judge_the_picture_blur# Configure depth and color streams pipeline = rs.pipeline() # 創建 config 對象: config = rs.config() # config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30) config.enable_stream(rs.stream.color, 1280, 720, rs.format.bgr8, 30)# Start streaming pipeline.start(config)try:while True:# Wait for a coherent pair of frames(一對連貫的幀): depth and colorframes = pipeline.wait_for_frames()# depth_frame = frames.get_depth_frame()color_frame = frames.get_color_frame()if not color_frame:continuecolor_image = np.asanyarray(color_frame.get_data())# Show imagescv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)cv2.imshow('RealSense', color_image)c = cv2.waitKey(1)# 手動采集(空格鍵)# 敲擊空格將圖像保存(空格的ascii碼是32)if c == 32:# 計算文件夾里jpg文件數量,以便于關閉軟件后重新打開采集不會將已有圖片覆蓋count = 0for filename in os.listdir('../imgs/'):if filename.endswith('.jpg'):count += 1# print(count)# 判斷該幀圖片是否模糊,如果模糊就跳過這一幀,循環下一幀image_var = judge_the_picture_blur.getImageNdarrayVar(color_image)if image_var < 200:continue# 保存圖像,保存到上一層的imgs文件夾內,以1、2、3、4...為文件名保存圖像cv2.imwrite('../imgs/{}.jpg'.format(count + 1), color_image)# 自動采集(回車鍵)# 如果按下回車鍵則自動采集(回車鍵ascii碼是13)if c == 13:# 獲取時間以在循環中判斷是否經過了某段時間time0 = time.time()while True:# 之前卡死是因為進了這個循環出不去了了,沒法wait_for_frames同時opencv窗口也沒法waitKey刷新所以就卡死了,所以必須再把wait_for_frames和waitKey加進來# Wait for a coherent pair of frames(一對連貫的幀): depth and colorframes = pipeline.wait_for_frames()# depth_frame = frames.get_depth_frame()color_frame = frames.get_color_frame()if not color_frame:continuecolor_image = np.asanyarray(color_frame.get_data())# Show imagescv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)cv2.imshow('RealSense', color_image)c = cv2.waitKey(1)# 計算文件夾里jpg文件數量,以便于關閉軟件后重新采集不會將已有圖片覆蓋count = 0for filename in os.listdir('../imgs/'):if filename.endswith('.jpg'):count += 1# 設置延時時間(單位:秒)time_delay = 0.2# 使用經過的時間差來對操作進行延時,這樣可避免流停滯,但此方法不完全準確,特別是在我們的操作耗時較長的情況下,延時誤差較大。當我們將操作的時間視為無窮小時,此方法準確if (time.time() - time0) > time_delay:# 可以看到,實際延時微微大于我們設置的延時時間print(time.time() - time0)# 1.0003252029418945# 1.0017242431640625# 1.0030531883239746# 1.004326343536377# 1.0044221878051758# 1.008774995803833# 1.005782127380371# 1.0061774253845215# 1.010535717010498# 只讓操作在某時間段內只執行一次,下次執行需在延時時間time_delay(秒)后time0 += time_delay# 判斷該幀圖片是否模糊,如果模糊就跳過這一幀,循環下一幀image_var = judge_the_picture_blur.getImageNdarrayVar(color_image)if image_var < 200:continue# 保存圖像,保存到上一層的imgs文件夾內,以1、2、3、4...為文件名保存圖像cv2.imwrite('../imgs/{}.jpg'.format(count + 1), color_image)# 延時一下,不然保存太快翻車咋辦# 之前卡頓的原因是用了time.sleep函數,導致wait_for_frames變慢了,不能用sleep函數# time.sleep(1)if c == 27:break# 如果按下ESC則關閉窗口(ESC的ascii碼為27),同時跳出循環if c == 27:cv2.destroyAllWindows()breakfinally:# Stop streamingpipeline.stop() # -*- encoding: utf-8 -*- """ @File : judge_the_picture_blur.py @Time : 2019/10/25 8:52 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """ import cv2 import os# 返回給定圖像(numpy.ndarray類型)的拉普拉斯算子邊緣模糊程度值 def getImageNdarrayVar(image):img2gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)imageVar = cv2.Laplacian(img2gray, cv2.CV_64F).var()return imageVar# 返回指定路徑圖像的拉普拉斯算子邊緣模糊程度值 def getImageVar(img_path):image = cv2.imread(img_path)img2gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)imageVar = cv2.Laplacian(img2gray, cv2.CV_64F).var()return imageVar# 返回給定文件夾下所有圖片的路徑列表 def listFolderImgPath(folder_img_path):img_path_list = []for filename in os.listdir(folder_img_path):filepath = os.path.join(folder_img_path, filename)img_path_list.append(filepath)return img_path_list# 給單張圖片添加文字(圖片路徑,文字) def writeText(img_path, text):# 加載背景圖片# img的類型是np.ndarray數組img = cv2.imread(img_path)# 在圖片上添加文字信息# 顏色參數值可用顏色拾取器獲取((255,255,255)為純白色)# 最后一個參數bottomLeftOrigin如果設置為True,那么添加的文字是上下顛倒的composite_img = cv2.putText(img, text, (100, 680), cv2.FONT_HERSHEY_SIMPLEX,2.0, (255, 255, 255), 5, cv2.LINE_AA, False)cv2.imwrite(img_path, composite_img)# 如果不是被導包使用 if __name__ == '__main__':# 文件夾路徑folder_img_path = '../../imgs/'# 圖片路徑img_path = '../../imgs/f_cotton-g_top (813).jpg'# print(getImageVar(img_path))# print(listFolderImgPath(folder_img_path))# 獲取圖片路徑列表img_path_list = listFolderImgPath(folder_img_path)# 循環處理每張圖片for img_path in img_path_list:# 獲取該張圖片模糊值imageVar = getImageVar(img_path)# 創建需寫入文字信息text = 'The fuzzy is: {:.2f}'.format(imageVar)# 將文字寫入圖片writeText(img_path, text)# img = cv2.imread(img_path)# cv2.namedWindow('image', cv2.WINDOW_AUTOSIZE)# cv2.imshow('image', img)# cv2.waitKey(1)總結
以上是生活随笔為你收集整理的python Intel Realsense D435 图像自动采集程序 自动剔除模糊图像的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSDN markdown 如何更改文字
- 下一篇: websocket python爬虫_p