File size: 1,955 Bytes
50d6ddc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b86ebfa
50d6ddc
 
 
 
 
 
 
 
 
 
b86ebfa
 
50d6ddc
 
 
 
 
 
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
import cv2
import numpy as np
import math

class ContourAnalyzer:
    def __init__(self, min_area_threshold=5):
        self.min_area_threshold = min_area_threshold

    def find_thickest_contour(self, contours, binary_image):
        max_width = 0
        thickest_section = None
        thickest_points = None
        distance_transforms = []

        for contour in contours:
            if cv2.contourArea(contour) > self.min_area_threshold:

                mask = np.zeros_like(binary_image)
                cv2.drawContours(mask, [contour], 0, 255, thickness=cv2.FILLED)

                distance_transform = cv2.distanceTransform(mask, cv2.DIST_L2, cv2.DIST_MASK_PRECISE)
                distance_transforms.append(distance_transform)

                _, _, _, max_loc = cv2.minMaxLoc(distance_transform)
                width = 2 * distance_transform[max_loc[1], max_loc[0]]

                if width > max_width:
                    max_width = width
                    thickest_section = contour
                    thickest_points = max_loc

        return max_width, thickest_section, thickest_points, distance_transforms

    def find_contours(self, binary_image):
        contours, _ = cv2.findContours(binary_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

        # for debugging
        print("Number of contours:", len(contours))

        max_width, thickest_section, thickest_points, distance_transforms = self.find_thickest_contour(contours, binary_image)

        return max_width, thickest_section, thickest_points, distance_transforms

    @staticmethod
    def calculate_width(y, x, pixel_width, calibration_factor, distance):
        # angle = math.atan2(y, x)
        angle=1
        width = angle * pixel_width * distance * calibration_factor
        return width
    
    def draw_circle_on_image(self, image, center, radius, color=(57, 255, 20), thickness=-1):
        cv2.circle(image, center, radius, color, thickness)