Untitled

 avatar
unknown
python
5 months ago
1.8 kB
2
Indexable
import numpy as np
import cv2 

img = cv2.imread("images/2.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gauss = cv2.GaussianBlur(gray, (9, 9), 2)
edges = cv2.Canny(gauss, 50, 150)  # Adjust thresholds for edge detection



circles = cv2.HoughCircles(
            gauss,
            cv2.HOUGH_GRADIENT,
            dp=1.2,
            minDist=1000,
            param1=50,
            param2=30,
            minRadius=20,
            maxRadius=200
)

lines = cv2.HoughLines(edges, 1, np.pi / 180, threshold=90)


if circles is not None:
    circles = np.uint16(np.around(circles))
    for i in circles[0, :]:
        center = (i[0], i[1]) 
        radius = i[2]          
        
        # draw circle
        cv2.circle(img, center, radius, (0, 255, 0), 2)
        # draw center
        cv2.circle(img, center, 1, (0, 0, 255), 1)
    
    if lines is not None:
        for line in lines:
            rho, theta = line[0]  # Each line is a single item with [rho, theta]
            
            a = np.cos(theta)
            b = np.sin(theta)
            x0 = a * rho
            y0 = b * rho
            x1 = int(x0 + 1000 * (-b))  # Extend line
            y1 = int(y0 + 1000 * (a))
            x2 = int(x0 - 1000 * (-b))
            y2 = int(y0 - 1000 * (a))
            
            # cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 1)  # Draw base line
            cv2.line(img, center, (x1, y1), (0, 255, 255), 1) # Draw final line 
            
            # Mask the region of interest (ROI) to focus on the gauge
            mask = np.zeros_like(gauss)
            cv2.circle(mask, center, radius, 255, thickness=-1)
            masked = cv2.bitwise_and(img, img, mask=mask)


cv2.imshow("og img", masked)
cv2.waitKey(0)
cv2.destroyAllWindows()
Editor is loading...
Leave a Comment