python opencv使用BFMatcher knn匹配HOG描述符

时间:2018-06-26 19:19:39

标签: python algorithm opencv image-processing

我正在使用python opencv BFMatcher来匹配同一图像的不同补丁的HOG描述符,以查找复制粘贴伪造。补丁是从scikit库中的SLIC算法获得的。 代码如下,

def matchWithHOGKNN(SegmentValueList: list, segments: numpy.ndarray, imageColor: numpy.ndarray):
'''prepare the HOG descriptor'''

hog = HOGDescriptor((32, 32), (16, 16), (8, 8), (8, 8),9) 

'''iteratively looping over the list of segment values'''
    for segVal in noSIFTkeySegValList:
        rows, cols = numpy.where(segments == segVal)
    '''iterating the HOG sliding window'''
         for y in range(min(rows), max(rows) - detection_window_row, 4):
           for x in range(min(cols), max(cols) - detection_window_col, 4):
             roi = greyImage[y:y + detection_window_row, x:x + detection_window_col]

'''add the descriptor with segment value'''
      dictionary[SegVal(segVal)] = hog.compute(img=roi)

        # '''after iterating the HOG kernel over the segment, add the descriptors to the dictionary'''
        # dictionary[segVal] = temp_h_o_g_descriptors


'''after iterating over all the 
compare between the descriptors of each segment obtained
here in the dictionary key->segment_value | value->HOG descriptor'''

'''prepare a numpy array of HOG descriptors'''
hog_descriptors = numpy.array(list(dictionary.values()))
list_of_keys = numpy.array(list(dictionary.keys()))


'''prepare the BF matcher'''
bf = cv2.BFMatcher()
matches = bf.knnMatch(hog_descriptors, hog_descriptors, k=2)

'''iterating over matches to get the best matches'''
for i, (m, n) in enumerate(matches):
    distance = abs(n.distance- m.distance)

    # if distance < minimum_distance:  # for testing purposes
    #     minimum_distance = distance  # for testing purposes

    if distance < 0.4:
        '''this is taken as a match'''
        '''obtain the segment values for matched descriptors'''
        keyValue1 = list_of_keys[m.queryIdx].segmentValue
        keyValue2 = list_of_keys[n.trainIdx].segmentValue

这里的问题是匹配的补丁不是所需的补丁。感谢您帮助检测该问题。

一些示例图像如下, 从Copy-Move Forgery Detection and Localization的数据集获取的原始图像 enter image description here

克隆图像 cloned the right hand side window

克隆检测到的图像 marked patches are in white color

谢谢。

0 个答案:

没有答案