File size: 2,674 Bytes
014de59 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Aug 4 16:08:30 2024
@author: ysnrfd
"""
import cv2
import numpy as np
def detect_anomalies(frame1, frame2, min_contour_area=1, threshold_value=7):
"""
Detects anomalies between two frames and highlights them.
Parameters:
- frame1: The previous frame.
- frame2: The current frame.
- min_contour_area: Minimum area for a contour to be considered an anomaly.
- threshold_value: Threshold value for binary conversion.
Returns:
- The frame with anomalies highlighted.
"""
# Convert images to grayscale
gray1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
# Compute the absolute difference between the two images
diff = cv2.absdiff(gray1, gray2)
# Apply GaussianBlur to reduce noise and improve thresholding
blurred = cv2.GaussianBlur(diff, (5, 5), 0)
# Threshold the difference to get a binary image
_, thresh = cv2.threshold(blurred, threshold_value, 255, cv2.THRESH_BINARY)
# Find contours of the anomalies
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Draw contours on the original frame
for contour in contours:
if cv2.contourArea(contour) > min_contour_area: # Filter small contours
x, y, w, h = cv2.boundingRect(contour)
cv2.rectangle(frame1, (x, y), (x+w, y+h), (0, 255, 0), 1)
return frame1
def main():
# Initialize video capture (0 is usually the default camera)
cap = cv2.VideoCapture(0)
# Check if the camera opened successfully
if not cap.isOpened():
print("Error: Could not open video capture.")
return
# Read the first frame to initialize the previous frame
ret, prev_frame = cap.read()
if not ret:
print("Error: Could not read initial frame.")
cap.release()
return
while True:
# Read the current frame
ret, curr_frame = cap.read()
if not ret:
print("Error: Could not read frame.")
break
# Detect anomalies between previous and current frame
result_frame = detect_anomalies(prev_frame, curr_frame)
# Display the result
cv2.imshow('Anomalies Detected', result_frame)
# Update previous frame
prev_frame = curr_frame
# Exit on 'q' key press
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release video capture and close windows
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()
|