1

I'm using normalization as a preprocessing method with Template Matching. However, I faced an error when I run the code

Error: error: OpenCV(3.4.2) /opt/concourse/worker/volumes/live/9523d527-1b9e-48e0-7ed0-a36adde286f0/volume/opencv-suite_1535558719691/work/modules/imgproc/src/templmatch.cpp:1102: error: (-215:Assertion failed) (depth == 0 || depth == 5) && type == _templ.type() && _img.dims() <= 2 in function 'matchTemplate'

This my preprocessing method:

def Image_Preprocessing (image):
    Gray_image = cv2.cvtColor(image , cv2.COLOR_BGR2GRAY)  # converting the image to grayscale image
    resized_image = cv2.resize(Gray_image, (width, height))  # Resize the image 
    mean, stdDev = cv2.meanStdDev(resized_image)  #Get Mean and Standard-deviation
    Normalized_image = (resized_image-mean)/stdDev  #Normalize the image  
    # Scale the normalized values to integer range
    Normalized_image -= Normalized_image.min() 
    Normalized_image /= Normalized_image.max()
    Normalized_image *= 255 # [0, 255] range

    return  Normalized_image

How I can solve this problem?

Sara
  • 169
  • 2
  • 9
  • Your `Normalized_Image` is of type `float64`. Did you verify, (a) that the template matching in general works with `float64`, and if so (b) your template is also of type `float64`? – HansHirse Nov 25 '19 at 12:03

1 Answers1

0

In any case, you should verify @HansHirse 's answer, if the problem even is your preprocessing, you can try this:

def Image_Preprocessing (image):
    Gray_image = cv2.cvtColor(image , cv2.COLOR_BGR2GRAY)  # converting the image to grayscale image
    resized_image = cv2.resize(Gray_image, (width, height))  # Resize the image
    Normalized_image = np.array(np.divide(resized_image, np.amax(resized_image)), dtype=np.float64) # Normalizes to float 0 - 1, ensure float
    # Scale the normalized values to integer range
    Normalized_image *= 255 # [0, 255] range
    Normalized_image = np.uint8(Normalized_image)

    return  Normalized_image

This returns a uint8 image, if your template is also uint8, there shouldn't be an issue.

code-lukas
  • 1,586
  • 9
  • 19
  • thank you so much, the problem was solved by adding this line Normalized_image = np.uint8(Normalized_image) – Sara Nov 25 '19 at 13:43