从对象的边删除关键点

2024-04-19 14:20:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在处理带有对象的图像。我使用canny边缘检测和轮廓来检测和绘制其中对象的边缘。然后我使用SIFT和SURF来检测对象中的关键点。这是我一直在编写的示例代码

import cv2 as cv
import numpy as np 
import matplotlib.pyplot as plt
img = cv.imread(image)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
edges = cv.Canny(gray, 100,200)
image, contours, hierarchy = cv.findContours(edges, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
outimg = cv.drawContours(img, contours, -1, (0,255,0), 3)
sift = cv.xfeatures2d_SIFT.create()
kp, des = sift.detectAndCompute(outimg,None)

有没有办法去除边缘上的关键点?用例子来回答会很有帮助。 谢谢

Original Imageoutput image


Tags: 对象图像imageimportimgascv边缘
2条回答

可以使用pointPolygonTest方法筛选检测到的关键点。使用检测到的轮廓作为边界多边形。您还可以定义所需的边距

Simaple示例(fot 4点轮廓):

def inside_point(self, point, rect):

        # point is a list (x, y)
        # rect is a contour with shape [4, 2]

        rect = rect.reshape([4, 1, 2]).astype(np.int64)

        dist = cv2.pointPolygonTest(rect,(point[0], point[1]),True)

        if dist>=0:
            # print(dist)
            return True
        else:
            return False 

也可以在遮罩图像上绘制轮廓,若要检查点是否在轮廓内,只需使用点坐标检查像素值,若不是0,则该点有效

似乎一切都很好: 我没有xfeatures2d,所以在这里使用了ORB功能

import cv2 as cv
import numpy as np 
import matplotlib.pyplot as plt

img = cv.imread('image.jpg')
#img = cv.resize(img,(512,512))
img = cv.copyMakeBorder(img,20,20,20,20, 0)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

_ , gray = cv.threshold(gray,20,255,cv.THRESH_TOZERO)

gray=cv.erode(gray,np.ones( (5,5), np.int8) )
edges = cv.Canny(gray, 100,200)
contours, hierarchy = cv.findContours(edges, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)

orb = cv.ORB_create(nfeatures=10000)
kp, des = orb.detectAndCompute(gray,None)

outimg = cv.drawContours(img, contours, -1, (0,255,0), 3)

k = []
for cont in contours:   
    for i in kp: 
        (x, y) =i.pt
        dist = cv.pointPolygonTest(cont, (x,y), True)
        if dist>=0:
            k.append(i)

for i in k:
    pt=(int(i.pt[0]),int(i.pt[1]) )
    cv.circle(outimg,pt,3, (255,255,255),-1)

cv.imwrite('result.jpg',outimg)        
cv.imshow('outimg',outimg)
cv.waitKey()

enter image description here

我仍然发现很难从给定的图像中删除关键点。如果关键点不在轮廓中,我尝试将其附加到新列表中,但在使用cv2.drawKeypoints函数时显示错误,因为新列表不是keypoint类型。这就是我到目前为止所做的工作

import cv2 as cv
import numpy as np 
import matplotlib.pyplot as plt
img = cv.imread(image)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
edges = cv.Canny(gray, 100,200)
image, contours, hierarchy = cv.findContours(edges, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
outimg = cv.drawContours(img, contours, -1, (0,255,0), 3)
sift = cv.xfeatures2d_SIFT.create()
kp, des = sift.detectAndCompute(outimg,None)
k = cv.KeyPoint()
for i in kp: 

    (x, y) =i.pt
    dist = cv.pointPolygonTest(contours[0], (x,y), True)
    if dist>=0:
        k1.append(k)

相关问题 更多 >