Assignment 5: Hough transform Line and Circle

Image Link: https://drive.google.com/drive/folders/1USa7N0JCIeGGhZhkKP18-QuziOzh8riX?usp=share_link

Code Line: 

import cv2

import numpy as np


img = cv2.imread('sudoku.png')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

edges = cv2.Canny(gray, 50, 150, apertureSize=3)

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


for line in lines:

    rho,theta = line[0]

    a = np.cos(theta)

    b = np.sin(theta)

    x0 = a * rho

    y0 = b * rho

    # x1 stores the rounded off value of (r * cos(theta) - 1000 * sin(theta))

    x1 = int(x0 + 1000 * (-b))

    # y1 stores the rounded off value of (r * sin(theta)+ 1000 * cos(theta))

    y1 = int(y0 + 1000 * (a))

    # x2 stores the rounded off value of (r * cos(theta)+ 1000 * sin(theta))

    x2 = int(x0 - 1000 * (-b))

    # y2 stores the rounded off value of (r * sin(theta)- 1000 * cos(theta))

    y2 = int(y0 - 1000 * (a))

    cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)


cv2.imshow('image', img)

k = cv2.waitKey(0)

cv2.destroyAllWindows()

img = cv2.imread('road.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)

lines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength=100,maxLineGap=10)
for line in lines:
     x1,y1,x2,y2 = line[0]
    cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)
cv2.imshow('image', img)
k = cv2.waitKey(0)
cv2.destroyAllWindows()


img = cv.imread('shape.png')
output = img.copy()
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
gray = cv.medianBlur(gray, 5)
circles = cv.HoughCircles(gray, cv.HOUGH_GRADIENT, 1, 20,
param1=50, param2=30, minRadius=0, maxRadius=0)
detected_circles = np.uint16(np.around(circles))
for (x, y ,r) in detected_circles[0, :]:
    cv.circle(output, (x, y), r, (0, 0, 0), 3)
    cv.circle(output, (x, y), 2, (0, 255, 255), 3)
cv.imshow('output',output)
cv.waitKey(0)
cv.destroyAllWindows()

Comments

Popular posts from this blog

Assignment 1.2 - Transformation

Assignment 4: Canny Edge Detection

Assignment 1.1: Image Transformation in Python