Alex Von D
Alex Von D

Reputation: 23

How can I save circular Hough transform into a .npy file

I'm trying to find the contours of an image of this type: Input image

I have use the circular Hough transform but the circle don't match the shape, I also tried the find contours but the image is the same

imagen = y[0]
# imagen = Image.fromarray(imagen) 
cimg = cv.cvtColor(imagen,cv.COLOR_GRAY2BGR)

# Find contours
contours,_ = cv.findContours(imagen, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)

# Draw all detected contours on image in green with a thickness of 1 pixel
cv.drawContours(cimg, contours, -1, (0,255,0), 3)

cv.imshow('detected circles',cimg)
cv.waitKey(0)

But it only shows a rectangle. Also I was trying to automate this process and doit for a series of images but with no avail, this is the function that I'm using, the idea is to save the image with the circle of contours so I can train a machine learning model

def hug_tranasform(img, dp=1,minDist=80,param1=30,param2=100,minRadius=10,maxRadius=650):
    cht = []

    for i in range(len(img)):
        current_img = img[i]
        cimg = cv.cvtColor(current_img, cv.COLOR_GRAY2BGR)
        # Detección de círculos usando la transformada circular de Hough
        circles = cv.HoughCircles(
            current_img,
            cv.HOUGH_GRADIENT,
            dp,
            minDist,
            param1=param1,
            param2=param2,
            minRadius=minRadius,
            maxRadius=maxRadius
        )

        # Verificar si se detectaron círculos y si tienen la forma esperada
        if circles is not None and len(circles.shape) == 3:
            circles = np.uint16(np.around(circles))
            for j in circles[0, :]:
                # Dibujar el círculo exterior
                cv.circle(cimg, (j[0], j[1]), j[2], (0, 255, 0), 2)
                # Dibujar el centro del círculo
                cv.circle(cimg, (j[0], j[1]), 2, (0, 0, 255), 3)

        cht.append((cimg))
    np.save('train_data_cht.npy', np.array(cht))
    return

Upvotes: 0

Views: 48

Answers (0)

Related Questions