Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- import math
- def draw_line_P(img, x1, y1, x2, y2, color = (0, 0, 255), thickness = 2,
- lineType=cv2.LINE_AA):
- cv2.line(img, (x1, y1), (x2, y2), color, thickness, lineType)
- def draw_circle(img, center, radius, color=(0, 0, 255), thickness = 2,
- lineType = cv2.LINE_AA):
- cv2.circle(img, center, radius, color, thickness, lineType)
- img = cv2.imread("C:\Program Files (x86)\Photos for TZ\6_1.png",cv2.IMREAD_COLOR)
- allLines = np.copy(img)
- longestLine = np.copy(img)
- allCircles = np.copy(img)
- biggestCircle = np.copy(img)
- img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- img_grayinv = cv2.bitwise_not(img_gray)
- lines = cv2.HoughLinesP(img_grayinv, rho = 1, theta = np.pi/720, threshold = 230,
- minLineLength=150, maxLineGap = 15)
- maxLen = 0
- x1P, y1P, x2P, y2P = 0, 0, 0, 0
- for line in lines:
- x1, y1, x2, y2 = line[0]
- currentLen = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
- if currentLen >= maxLen:
- maxLen = currentLen
- x1P, y1P, x2P, y2P = x1, y1, x2, y2
- draw_line_P(allLines, x1, y1, x2, y2)
- draw_line_P(longestLine, x1P, y1P, x2P, y2P)
- edges = cv2.Canny(img_gray, 0, 255)
- circles = cv2.HoughCircles(edges, cv2.HOUGH_GRADIENT, dp = 1, minDist = 250,
- param1 = 255, param2 = 97, minRadius = 0, maxRadius = 0)
- xmax, ymax, rmax = 0, 0, 0
- for circle in circles[0]:
- x0, y0, r = circle
- if r > rmax:
- xmax, ymax, rmax = x0, y0, r
- draw_circle(allCircles, (x0, y0), r)
- draw_circle(biggestCircle, (xmax, ymax), rmax)
- cv2.imwrite("C:\Program Files (x86)\Photos for TZ\allLines.png", allLines)
- cv2.imwrite("C:\Program Files (x86)\Photos for TZ\longestLine.png", longestLine)
- cv2.imwrite("C:\Program Files (x86)\Photos for TZ\allCircles.png", allCircles)
- cv2.imwrite("C:\Program Files (x86)\Photos for TZ\biggestCircle.png", biggestCircle)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement