openCV检测物体是否运动
openCV检测物体是否运动
https://blog.csdn.net/XiBuQiuChong/article/details/135419293
int fmle::capCam() { videoCap.open(0); cv::Mat frame1, frame2; BOOL ifSuccess = videoCap.read(frame1); while (true) { BOOL ifSuccess = videoCap.read(frame2); cv::Mat diff; cv::absdiff(frame1, frame2, diff); // 计算两帧图像的差异 cv::Mat gray; cv::cvtColor(diff, gray, cv::COLOR_BGR2GRAY); // 转换为灰度图像 cv::Mat blurred; cv::GaussianBlur(gray, blurred, cv::Size(5, 5), 0); // 高斯模糊 cv::Mat thresholded; cv::threshold(blurred, thresholded, 20, 255, cv::THRESH_BINARY); // 二值化 cv::Mat dilated; cv::dilate(thresholded, dilated, cv::Mat(), cv::Point(-1, -1), 2); // 膨胀 std::vector<std::vector<cv::Point>> contours; cv::findContours(dilated, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE); // 查找轮廓 bool objectMoved = false; for (const auto& contour : contours) { double area = cv::contourArea(contour); if (area > 1000) // 设置最小轮廓面积阈值 { objectMoved = true; break; } } if (objectMoved) { TRACE("物体移动了\n"); cv::putText(dilated, "Moving... ", cv::Point(0, 40), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(255, 255, 255), 2); } else { cv::putText(dilated, "Stopping... ", cv::Point(0, 40), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(255, 255, 255), 2); TRACE("物体未移动\n"); } frame1 = frame2.clone(); // 更新上一帧图像 mainDlg->drawMatOfPub(dilated); Sleep(40); } videoCap.release(); return 0; }
监控区域变化
https://blog.csdn.net/youth_shouting/article/details/131967141
import cv2 import numpy as np import threading # 初始化pygame库 pygame.init() pygame.mixer.init() # 全局变量,加载MP3文件 def load_alarm_sound(file_path): try: pygame.mixer.music.load(file_path) except pygame.error: print(f"无法加载音频文件:{file_path}") # 封装一个播放MP3警报的函数 def play_alarm(): try: # 播放MP3文件 pygame.mixer.music.play() except pygame.error: print("播放警报音频失败") # 调用load_alarm_sound函数,加载警报音频 load_alarm_sound("警报声.mp3") should_exit = False is_alarm = False prev_frame_image, current_frame_image = None, None def monitor(data_source): global should_exit, is_alarm, prev_frame_image, current_frame_image if data_source == "screenshot": prev_frame = capture_screen() elif data_source == "camera": cap = cv2.VideoCapture(0) prev_frame = capture_video_frame(cap) while not should_exit: current_frame = None if data_source == "screenshot": current_frame = capture_screen() elif data_source == "camera": current_frame = capture_video_frame(cap) if current_frame is not None: prev_gray = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY) current_gray = cv2.cvtColor(current_frame, cv2.COLOR_BGR2GRAY) similarity = ssim(prev_gray, current_gray) threshold = 0.95 if similarity < threshold and not is_alarm: print("检测到较大变化,进行报警!") is_alarm = True play_alarm() # 保存变化前后的照片 prev_frame_image = prev_frame.copy() current_frame_image = current_frame.copy() # 在5秒后重置is_alarm标志 threading.Timer(5, reset_alarm_flag).start() else: print("没有变化") # 更新前一帧图像 prev_frame = current_frame time.sleep(5) # 等待5秒后再获取下一帧 # 释放资源 if data_source == "camera": cap.release() cv2.destroyAllWindows() def start_recognition(): global should_exit should_exit = False print("开始监控") th_monitor = threading.Thread(target=monitor, args=("screenshot",)) th_monitor.daemon = True th_monitor.start() def end_program(): global should_exit should_exit = True print("结束程序") sys.exit(1)
opencv+python判断画面动静
https://zhuanlan.zhihu.com/p/340754133
import cv2 import time import numpy as np video_file = "./videos/video.mp4" cap = cv2.VideoCapture(video_file) #读取视频文件,参数设置为0表示从摄像头获取图像 _, frame1 = cap.read() img1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY) #将图片转为灰度图,第一个返回值表示是否转换成功,第二个返回值就是灰度图了 start = time.time() def moving_detect(frame1, frame2): img1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY) img2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY) grey_diff = cv2.absdiff(img1, img2) #计算两幅图的像素差 change = np.average(grey_diff) if change > 10: #当两幅图的差异大于给定的值后,认为画面有物体在动 cv2.putText(frame2, 'moving', (100, 30), 2, 1,(0,255,0),2,cv2.LINE_AA) else: cv2.putText(frame2, 'quiet', (100, 30), 2, 1, (255, 0, 0), 2, cv2.LINE_AA) cv2.imshow("output", frame2) while True: end = time.time() if (end - start > 2): #每隔2秒拍一幅图,比较前后两幅图的差异 start = time.time() _, frame1 = cap.read() _, frame2 = cap.read() moving_detect(frame1, frame2) if cv2.waitKey(5) & 0xFF == ord('q'): #按下q停止运行程序 break # 最后,关闭所有窗口 cap.release() cv2.destroyAllWindows()
出处:http://www.cnblogs.com/lightsong/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。

浙公网安备 33010602011771号