给棋盘的角点标上红色:
“我是横行无忌的红螃蟹"
给螃蟹的角点标上绿色
源码如下:
# -*- coding: utf-8 -*-
"""
Created on Dec 15 22:19:18 2019
@author: Administrator
"""
import numpy as np
import cv2
#img = cv2.imread("chessboard.jpg")
img = cv2.imread("crab.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
dst = cv2.cornerHarris(gray, blockSize=5, ksize=7, k=0.1)
#第三个参数ksize决定了角点检测的灵敏度,越小越灵敏,其取值必须是3到31之间的奇数
#第二个参数决定了mark点的大小。
print(np.where(dst >0.01* dst.max())) #角点的x坐标和y坐标
img [dst >0.01* dst.max()] = (125,255,0)#给角点标上颜色
#img [dst >0.01* dst.max()] = (0,0,255)
cv2.imshow('corners', img)
'''
cornerHarris(...)
cornerHarris(src, blockSize, ksize, k[, dst[, borderType]]) -> dst
. @brief Harris corner detector.
.
. The function runs the Harris corner detector on the image. Similarly to cornerMinEigenVal and
. cornerEigenValsAndVecs , for each pixel \f$(x, y)\f$ it calculates a \f$2\times2\f$ gradient covariance
. matrix \f$M^{(x,y)}\f$ over a \f$\texttt{blockSize} \times \texttt{blockSize}\f$ neighborhood. Then, it
. computes the following characteristic:
.
. \f[\texttt{dst} (x,y) = \mathrm{det} M^{(x,y)} - k \cdot \left ( \mathrm{tr} M^{(x,y)} \right )^2\f]
.
. Corners in the image can be found as the local maxima of this response map.
.
. @param src Input single-channel 8-bit or floating-point image.
. @param dst Image to store the Harris detector responses. It has the type CV_32FC1 and the same
. size as src .
. @param blockSize Neighborhood size (see the details on #cornerEigenValsAndVecs ).
. @param ksize Aperture parameter for the Sobel operator.
. @param k Harris detector free parameter. See the formula above.
. @param borderType Pixel extrapolation method. See #BorderTypes.
'''
本文分享自 Python可视化编程机器学习OpenCV 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体分享计划 ,欢迎热爱写作的你一起参与!