|
| 1 | +# -*-coding:utf8-*-# |
| 2 | +__author__ = 'play4fun' |
| 3 | +""" |
| 4 | +create time:15-10-29 上午7:47 |
| 5 | +""" |
| 6 | + |
| 7 | +import cv2 |
| 8 | +import numpy as np |
| 9 | + |
| 10 | +filename = '../data/chessboard-2.png' |
| 11 | +img = cv2.imread(filename) |
| 12 | + |
| 13 | +gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) |
| 14 | +# find Harris corners |
| 15 | +gray = np.float32(gray) |
| 16 | +dst = cv2.cornerHarris(gray, 2, 3, 0.04) |
| 17 | +dst = cv2.dilate(dst, None) |
| 18 | +ret, dst = cv2.threshold(dst, 0.01 * dst.max(), 255, 0) |
| 19 | +dst = np.uint8(dst) |
| 20 | + |
| 21 | +# find centroids |
| 22 | +# connectedComponentsWithStats(InputArray image, OutputArray labels, OutputArray stats, |
| 23 | +# OutputArray centroids, int connectivity=8, int ltype=CV_32S) |
| 24 | +ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst) |
| 25 | +# define the criteria to stop and refine the corners |
| 26 | +criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001) |
| 27 | +# Python: cv2.cornerSubPix(image, corners, winSize, zeroZone, criteria) |
| 28 | +# zeroZone – Half of the size of the dead region in the middle of the search zone |
| 29 | +# over which the summation in the formula below is not done. It is used sometimes |
| 30 | +# to avoid possible singularities of the autocorrelation matrix. The value of (-1,-1) |
| 31 | +# indicates that there is no such a size. |
| 32 | +# 返回值由 点坐标组成的一个数组 而 图像 |
| 33 | +corners = cv2.cornerSubPix(gray, np.float32(centroids), (5, 5), (-1, -1), criteria) |
| 34 | +# Now draw them |
| 35 | +res = np.hstack((centroids, corners)) |
| 36 | +# np.int0 可以用来省略小数点后 的数字,非四舍五入 |
| 37 | +res = np.int0(res) |
| 38 | +img[res[:, 1], res[:, 0]] = [0, 0, 255] |
| 39 | +img[res[:, 3], res[:, 2]] = [0, 255, 0] |
| 40 | + |
| 41 | +# cv2.imwrite('subpixel5.png',img) |
| 42 | +cv2.imshow('subpixel5.png', img) |
| 43 | +cv2.waitKey(0) |
0 commit comments