-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect_logs.py
More file actions
67 lines (49 loc) · 2.17 KB
/
Copy pathdetect_logs.py
File metadata and controls
67 lines (49 loc) · 2.17 KB
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
import cv2
import numpy as np
def detect_logs(image_path):
# Read the image
image = cv2.imread(image_path)
# Check if the image is loaded successfully
if image is None:
print(f"Error: Unable to read the image at '{image_path}'")
return []
# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply Gaussian blur to reduce noise
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
# Apply adaptive thresholding
_, thresholded = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# Apply morphological operations to remove small noise and fill gaps
kernel = np.ones((5, 5), np.uint8)
morphed = cv2.morphologyEx(thresholded, cv2.MORPH_CLOSE, kernel)
morphed = cv2.morphologyEx(morphed, cv2.MORPH_OPEN, kernel)
# Find contours in the processed image
contours, _ = cv2.findContours(morphed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
log_dimensions = []
for contour in contours:
# Filter contours based on area to remove small details
area = cv2.contourArea(contour)
if area < 100:
continue
# Approximate the contour to a polygon
epsilon = 0.02 * cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, epsilon, True)
# Calculate the bounding box for the contour
x, y, w, h = cv2.boundingRect(approx)
# Draw the bounding box on the original image (for visualization)
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
# Calculate the diameter of the log (assuming circular shape)
diameter = min(w, h)
# Append the diameter to the list
log_dimensions.append(diameter)
# Display the measurement near the rectangle
cv2.putText(image, f"{diameter} pixels", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
# Display the image with rectangles and measurements
cv2.imshow("Detected Logs", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
return log_dimensions
# Example usage
image_path = "rear-view-truck.jpeg"
log_dimensions = detect_logs(image_path)
print("Log Dimensions:", log_dimensions)