Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2.cv2 as cv2
- import numpy
- import math as m
- 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(center, radius,img, color=(0, 255, 0),thickness=2,lineType=cv2.LINE_AA):
- cv2.circle(img, center,radius, color,thickness, lineType)
- foto = cv2.imread('D:/pitonlaba/laba6/6_1_1.png',cv2.IMREAD_GRAYSCALE)
- tofo = cv2.imread('D:/pitonlaba/laba6/6_1_1.png',cv2.IMREAD_COLOR)
- tofo1 = cv2.imread('D:/pitonlaba/laba6/6_1_1.png',cv2.IMREAD_COLOR)
- tofo2 = cv2.imread('D:/pitonlaba/laba6/6_1_1.png',cv2.IMREAD_COLOR)
- ing , img = cv2.threshold(foto, 122, 255 , cv2.THRESH_BINARY_INV)
- lines = cv2.HoughLinesP( image = img, rho = 1, theta = numpy.pi/720, threshold = 255, maxLineGap = 15)
- OTVET = 0
- (a, b , c ) = numpy.shape(lines)
- (x1 , y1 , x2 , y2) = lines[0][0]
- max = (((x1-x2)**2)+((y1-y2)**2))
- for H in range(1,a):
- (x1 , y1 , x2 , y2) = lines[H][0]
- if (((x1-x2)**2)+((y1-y2)**2)) > max:
- max = (((x1-x2)**2)+((y1-y2)**2))
- OTVET = H
- (x1 , y1 , x2 , y2) = lines[OTVET][0]
- draw_line_P(tofo, x1 , y1 ,x2, y2)
- draw_line_P(tofo2, x1 , y1 ,x2, y2)
- rez1Canny = cv2.GaussianBlur(foto,(9,9),0) #кени
- threshold1 = 100 #кени
- threshold2 = 200 #кени
- rez1Canny = cv2.Canny(foto, threshold1, threshold2, apertureSize= 7 , L2gradient= True)
- Circles = cv2.HoughCircles( rez1Canny , cv2.HOUGH_GRADIENT, dp = 2,minDist = 1)
- OTVET = 0
- (a, b , c ) = numpy.shape(Circles)
- (x , y , r) = Circles[0][0]
- max = m.pi *r*r
- for H in range(1,a):
- (x , y ,r) = Circles[0][H]
- print(m.pi *r*r )
- if m.pi *r*r > max:
- max = m.pi *r*r
- OTVET = H
- (x , y , r) = Circles[0][OTVET]
- draw_circle((x , y) , r, tofo1)
- draw_circle((x , y) , r, tofo2)
- cv2.imshow('isxodnik', foto)
- cv2.imshow('rez line', tofo)
- cv2.imshow('rez circles', tofo1)
- cv2.imshow('rez line and circles', tofo2)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement