【问题标题】:How to implement counter in Harris Corner Detection algorithm?如何在哈里斯角检测算法中实现计数器?
【发布时间】:2020-10-14 18:35:28
【问题描述】:
import numpy as np
import cv2 as cv
img = cv.imread('maple.jpg')
half = cv.resize(img, (1200, 800), fx=0.1, fy=0.1)
gray = cv.cvtColor(half, cv.COLOR_BGR2GRAY)
gray = np.float32(gray)
dst = cv.cornerHarris(gray, 2, 3, 0.08)
dst = cv.dilate(dst, None)
half[dst > 0.01 * dst.max()] = [0, 0, 255]
cv.imshow('dst', half)
cv.waitKey(0)
cv.destroyAllWindows()

我需要计算这段代码检测到的特征数量。这些特征在图像中显示为红点。我对 python 和 OpenCV 非常陌生。我不知道如何在这段代码中实现计数器。

【问题讨论】:

  • 嘿。您可能想看看 'approxPolyDP' 函数。
  • 所以我不会直接在程序中实现一个计数器变量,而是使用这个函数来计算结果图像中的红点,对吧?
  • 有多种方法可以解决问题。您可以实现自己的计数 sn-p 但通常最好使用提供功能的内置工具和库,而不是自己重新发明轮子。这是一个安全的选择,因为内部工具更加优化。
  • 我还是个初学者,所以使用这个功能对我来说有点困难。我不知道从哪里开始。
  • 如果您认为可以解决您的疑问,不妨接受答案。

标签: python opencv feature-extraction feature-detection


【解决方案1】:

这里有可能对你有帮助的 sn-p。

# Python code to detect an arrow (seven-sided shape) from an image. 
import numpy as np 
import cv2 
   
# Reading image 
img2 = cv2.imread('arrow.jpg', cv2.IMREAD_COLOR) 
   
# Reading same image in another variable and  
# converting to gray scale. 
img = cv2.imread('arrow.jpg', cv2.IMREAD_GRAYSCALE) 
   
# Converting image to a binary image  
# (black and white only image). 
_,threshold = cv2.threshold(img, 110, 255,  
                            cv2.THRESH_BINARY) 
   
# Detecting shapes in image by selecting region  
# with same colors or intensity. 
contours,_=cv2.findContours(threshold, cv2.RETR_TREE, 
                            cv2.CHAIN_APPROX_SIMPLE) 
   
# Searching through every region selected to  
# find the required polygon. 
for cnt in contours : 
    area = cv2.contourArea(cnt) 
   
    # Shortlisting the regions based on there area. 
    if area > 400:  
        approx = cv2.approxPolyDP(cnt,  
                                  0.009 * cv2.arcLength(cnt, True), True) 
   
        # Checking if the no. of sides of the selected region is 7. 
        if(len(approx) == 7):  
            cv2.drawContours(img2, [approx], 0, (0, 0, 255), 5) 
   
# Showing the image along with outlined arrow. 
cv2.imshow('image2', img2)  
   
# Exiting the window if 'q' is pressed on the keyboard. 
if cv2.waitKey(0) & 0xFF == ord('q'):  
    cv2.destroyAllWindows()

这是sn-p中使用的图片:

这里是reference link

P.S - 如果有帮助,请为答案投票。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 2011-09-08
    • 2014-12-14
    • 2016-12-20
    • 2016-03-16
    • 2020-04-15
    • 1970-01-01
    相关资源
    最近更新 更多