Ask Your Question
0

[SOLVED] Unable to use rotate_bound image from imutils in matchTemplate

asked 2018-08-14 05:09:17 -0600

marafado88 gravatar image

updated 2018-08-14 06:25:53 -0600

Hello everyone,

I am trying to compare several input images, where those can came in many angles, with a target image for quality control, and I am trying to rotate all input images with imutils.rotate_bound and then compare those with cv2.matchTemplate, but seems like I cannot do that, that way, where I got:

Traceback (most recent call last):
  File "shape_compare.py", line 105, in <module>
    res = cv2.matchTemplate(img_rotated,img_template,cv2.TM_CCORR_NORMED)
cv2.error: OpenCV(3.4.2) C:\projects\opencv-python\opencv\modules\imgproc\src\templmatch.cpp:1102: error: (-215:Assertion failed) (depth == 0 || depth == 5) && type == _templ.type() && _img.dims() <= 2 in function 'cv::matchTemplate'

Do you know a way to fix this or do it another way?

This is the input image:

image description

This is the template image:

image description

This is the full code:

import numpy as np
import cv2
import imutils
from matplotlib import pyplot as plt

######EDIT HERE######
imagem = '20180801_173116_Film2'
template = 'sample_camflash_edited'
#####################

original = cv2.imread(imagem+'.jpg')
original_shape_compare = cv2.imread(imagem+'.jpg')
img_shape_compare = cv2.imread(imagem+'.jpg',0) # 0(zero) for grey
img = cv2.imread(imagem+'.jpg')
img_template = cv2.imread(template+'.jpg',0) # 0(zero) for grey

img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_blur = cv2.GaussianBlur(img_gray,(5,5),0)
img_bgr = cv2.cvtColor(img_blur,cv2.COLOR_GRAY2BGR)
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

#### Target Object Tracking

# define range of color in HSV
lowHue = 0
lowSat = 0
lowVal = 135
highHue = 255
highSat = 55
highVal = 255

# creation of mask
colorLow = np.array([lowHue,lowSat,lowVal])
colorHigh = np.array([highHue,highSat,highVal])
img_mask = cv2.inRange(img_hsv, colorLow, colorHigh)

# find and draw contours
#find
im2, contours, hierarchy = cv2.findContours(~img_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#draw 
cv2.drawContours(img, contours, -1, (0,255,0), 3) # green dots
#find largest contour
try:
    contour_sizes = [(cv2.contourArea(contour), contour) for contour in contours]
    biggest_contour = max(contour_sizes, key=lambda x: x[0])[1]
except:
    print('No object found!')
    x,y,w,h = 0,0,0,0
    cv2.imshow('original',original)
    cv2.imshow('img_template',img_template)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    exit(0)
# bounding rectangle
x,y,w,h = cv2.boundingRect(biggest_contour)
#print ('rectangle size: x=%s y=%s w=%s h=%s' %(x,y,w,h))
cv2.rectangle(img,(x,y),(x+w,y+h),(255,100,0),2) #clear blue box

# bounding rotated rectangle
rect = cv2.minAreaRect(biggest_contour)
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(img,[box],0,(255,200,0),2) #strong blue box

#### Object Handler

print ('rect angle')
print (rect[2])

# ROTATE IMAGE TO STANDARD!
img_rotated = imutils.rotate_bound(img, abs(rect[2])) #angle must be positive
#img_rotated = imutils.rotate(img, rect[2])

#### Object Shape Compare

w, h = img_template.shape[::-1]
res = cv2.matchTemplate(img_shape_compare,img_template,cv2.TM_CCORR_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
top_left = max_loc    
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(original_shape_compare,top_left, bottom_right,(0,255,0), 2) #green box

#### Output

cv2.imshow('img_rotated',img_rotated)

plt.subplot(221),plt.imshow(original)
plt.title('original'), plt.xticks([]), plt.yticks([])
plt.subplot(222),plt.imshow(img_template ...
(more)
edit retag flag offensive close merge delete

Comments

img_rotated.dtype is an uint8.

I tried with,

angle=rect[2]
image_center = tuple(np.array(img.shape[1::-1]) / 2)
rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0)
img_rotated = cv2.warpAffine(img, rot_mat, img.shape[1::-1], flags=cv2.INTER_LINEAR)

and got the same error, also I got the rotated image cutted, it doesnt show the all image, this was a problem that I was having with imutils.rotate, so I switched to imutils.rotate_bound.

marafado88 gravatar imagemarafado88 ( 2018-08-14 05:30:45 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-08-14 05:31:55 -0600

berak gravatar image

your img_template is 1 channel, greyscale, while your img_rotated has 3 channels. so, it should work with:

img_rotated = imutils.rotate(img_gray, rect[2])
edit flag offensive delete link more

Comments

1

It works!, Thanks a lot berak!

marafado88 gravatar imagemarafado88 ( 2018-08-14 05:34:54 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-08-14 05:09:17 -0600

Seen: 1,944 times

Last updated: Aug 14 '18