这是一次数学建模用到的图像识别代码,是关于python的opencv库的使用,在使用之前请安装opencv库。
下面是代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| def modify_picture1(filename,n): image = cv2.imread(filename) image = image[111:326,45:314] gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gradX = cv2.Sobel(gray, ddepth=cv2.CV_32F,dx=1,dy=0,ksize=-1) gradY = cv2.Sobel(gray, ddepth=cv2.CV_32F, dx=0, dy=1, ksize=-1) gradient = cv2.subtract(gradX, gradY) gradient = cv2.convertScaleAbs(gradient) blurred = cv2.blur(gradient,(9,9)) (_, thresh) = cv2.threshold(blurred, 80, 255, cv2.THRESH_BINARY) closed = cv2.erode(thresh, None, iterations=3) closed = cv2.dilate(closed, None, iterations=3) z = (n-55)*4.25 for i in range (215): for j in range(269): if closed[i,j]==255: f.writelines('%i,%i\n' % (i+111, j+45))
|