opencv / opencv_contrib

Repository for OpenCV's extra modules
Apache License 2.0
9.32k stars 5.74k forks source link

linemod can not found features with mask by Python #3372

Open Yakuho opened 1 year ago

Yakuho commented 1 year ago
System information (version)
Detailed description

OpenCV Contrib linemod code When I use opencv-python linemod to run a detector demo, I meet some template and mask trouble!!

import numpy as np
import cv2

detector = cv2.linemod.getDefaultLINE()
colorGradient = detector.getModalities()[0]
template = cv2.imread("template.png", 1)

def show(paddedEnable=0, maskEnable=0):
    global template
    mask = np.ones(template.shape[:2], dtype=np.uint8) * 255
    if paddedEnable:
        template = cv2.copyMakeBorder(template, 100, 100, 100, 100, cv2.BORDER_CONSTANT, 0)
        mask = cv2.copyMakeBorder(mask, 100, 100, 100, 100, cv2.BORDER_CONSTANT, 0)
    if maskEnable:
        quantizedPyramid = colorGradient.process(template, mask)
    else:
        quantizedPyramid = colorGradient.process(template, None)
    retval, templ = quantizedPyramid.extractTemplate()
    for feature in templ.features:
        cv2.circle(template, (feature.x, feature.y), 2, (0, 0, 255), 2)
    cv2.imshow('w1', template)
    cv2.imshow('w2', mask)
    cv2.waitKey(0)

if __name__ == '__main__':
    show(paddedEnable=0, maskEnable=0)
Steps to reproduce

As I know, mask image should be pixel value is 0 or 255, mask image type is uint8 in python, uint8 equal CV_8UC1.

  1. when set maskEnable false, paddedEnable false: Found features perfectly!
  2. when set maskEnable false, paddedEnable true: It is also good.
  3. when set maskEnable true, paddedEnable false: It can not found features???? :(
  4. when set maskEnable false, paddedEnable false: It is also not found :(

In my code, mask was set the full image (all pixel is 255). Normally detector should match the whole image and extract feature, but it didn't. Maybe my understanding of the mask input is wrong?

Issue submission checklist
Yakuho commented 1 year ago

template.png