|
- import cv2
- import numpy as np
-
- image=cv2.imread('10076.jpg')
- image = cv2.resize(image, (600, 600))
-
- '''
- imgray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
- ret, thresh = cv2.threshold(imgray, 127, 255, 0)
- contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
- '''
-
- #(3, 3)表示高斯矩阵的长与宽都是3,标准差取1
- InputArray = image.copy()
- cv2.GaussianBlur(InputArray, (3, 3), 1, InputArray)
- edgeImage = cv2.Canny(InputArray, 20, 80)
- cv2.imshow('GaussianBlur', edgeImage)
-
- ret, thresh = cv2.threshold(edgeImage, 127, 255, 0)
- contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
-
- '''
- blur = cv2.bilateralFilter(edgeImage,9,75,75)
- cv2.imshow('blur', blur)
- '''
-
- kernel = np.ones((5,5),np.uint8)
- edgeImage = cv2.morphologyEx(edgeImage, cv2.MORPH_CLOSE, kernel)
- cv2.imshow('morphologyEx', edgeImage)
-
- '''
- kernel = np.ones((5,5),np.uint8)
- edgeImage = cv2.dilate(edgeImage,kernel,iterations = 1)
- cv2.imshow('dilate', edgeImage)
- '''
-
- laplacian = cv2.Laplacian(edgeImage,cv2.CV_8U)
- sobelx = cv2.Sobel(edgeImage,cv2.CV_8U,1,0,ksize=5)
- sobely = cv2.Sobel(edgeImage,cv2.CV_8U,0,1,ksize=5)
- # laplacian = cv2.Laplacian(edgeImage,cv2.CV_64F)
- # sobelx = cv2.Sobel(edgeImage,cv2.CV_64F,1,0,ksize=5)
- # sobely = cv2.Sobel(edgeImage,cv2.CV_64F,0,1,ksize=5)
- cv2.imshow('laplacian', laplacian)
- cv2.imshow('sobelx', sobelx)
- cv2.imshow('sobely', sobely)
- edgeImage = sobely
-
- def cnt_area(cnt):
- area = cv2.contourArea(cnt)
- return area
- contours.sort(key = cnt_area, reverse=True)
- print(len(contours))
- for i in range(0, 10):
- cnt = contours[i]
- img = image.copy()
- cv2.drawContours(img, [cnt], 0, (0,255,0), 3)
- cv2.imshow('drawContours_' + str(i), img)
- cv2.drawContours(image, contours, -1, (0,255,0), 1)
- cv2.imshow('drawContours', image)
-
- houghLinesImage = image.copy()
- lines = cv2.HoughLines(edgeImage,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 = int(x0 + 1000*(-b))
- y1 = int(y0 + 1000*(a))
- x2 = int(x0 - 1000*(-b))
- y2 = int(y0 - 1000*(a))
- if (x2-x1) == 0 :
- xielv = (y2-y1)/0.00000001
- else:
- xielv = (y2-y1)/(x2-x1)
- if (xielv > -0.08 and xielv < 0.08) or xielv > 8.0 or xielv < -8.0 :
- # print(xielv)
- cv2.line(houghLinesImage,(x1,y1),(x2,y2),(0,0,255),2)
- cv2.imshow('houghLinesImage', houghLinesImage)
-
- houghLinesPImage = image.copy()
- thresh_min = min(edgeImage.shape)
- lines = cv2.HoughLinesP(edgeImage, 1.2, np.pi / 180, 160, minLineLength=int(edgeImage.shape[0] * 0.7),maxLineGap=int(thresh_min * 0.5))
- print(len(lines))
- for line in lines:
- x1,y1,x2,y2 = line[0]
- xielv = (y2-y1)/(x2-x1)
- if (xielv > -0.08 and xielv < 0.08) or xielv > 8.0 or xielv < -8.0 :
- # print(xielv)
- cv2.line(houghLinesPImage,(x1,y1),(x2,y2),(255,0,0),2)
- cv2.imshow('HoughLinesP', houghLinesPImage)
-
- '''
- circles = cv2.HoughCircles(edgeImage,cv2.HOUGH_GRADIENT,1,20,param1=50,param2=30,minRadius=0,maxRadius=0)
- circles = np.uint16(np.around(circles))
- for i in circles[0,:]:
- # 绘制外圆
- cv2.circle(image,(i[0],i[1]),i[2],(0,255,0),2)
- # 绘制圆心
- cv2.circle(image,(i[0],i[1]),2,(0,0,255),3)
- '''
-
- pts1 = np.float32([[56,65],[368,52],[28,387],[389,390]])
- pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])
- M = cv2.getPerspectiveTransform(pts1, pts2)
- image = cv2.warpPerspective(image, M, (600, 600))
-
- cv2.imshow('show', image)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
|