When I trying to run the code below i am getting error
i am using python 2.7 and opencv 2.4.13. The code is
# -*- coding: cp1254 -*-
import cv2
import numpy as np
def sift_detector(new_image, template):
image1=cv2.cvtColor(new_image,cv2.COLOR_BGR2GRAY)
image2=template
sift=cv2.SIFT()
keypoints1, descriptors1=sift.detectAndCompute(image1,None)
keypoints2, descriptors2=sift.detectAndCompute(image2,None)
FLANN_INDEX_KDTREE=0
index_param=dict(algorithm=FLANN_INDEX_KDTREE,trees=3)
search_params=dict(checks=100)
flann=cv2.FlannBasedMatcher(index_params,search_params)
matches=flann.knnMatch(descriptors1,descriptors2,k=2)
good_matches=[]
for m,n in matches:
if m.distance < 0.7 * n.distance:
good_matches.append(m)
return len(good_matches)
cap =cv2.VideoCapture(0)
template=cv2.imread("ben.jpg",0)
while True:
ret,frame=cap.read()
height, width= frame.shape[:2]
top_left_x=width/3
top_left_y=(height/2) + (height/4)
bottom_right_x= (width/3)*2
bottom_right_y= (height/2) - (height/4)
cv2.rectangle(frame, (top_left_x,top_left_y), (bottom_right_x,bottom_right_y),(255,0,0),3)
cropped=frame[bottom_right_y:top_left_y,bottom_right_x: top_left_x]
frame=cv2.flip(frame,1)
matches=sift_detector(cropped,template)
cv2.putText(frame,str(matches),(450,450),cv2.FONT_HERSHEY_COMPLEX,2,(0,255,0),3)
threshold=15
if matches > threshold:
cv2.rectangle(frame, (top_left_x,top_left_y), (bottom_right_x,bottom_right_y),(255,125,0),3)
cv2.putText(frame,"object found", (50,50), cv2.FONT_HERSHEY_COMPLEX,2,(255,255,0),2)
cv2.imshow("after process",frame)
if cv2.waitKey(1)==13:
break
cap.release()
cv2.destroyAllWindows()
And the error is
Traceback (most recent call last):
File "C:\Users\Ahmet\Desktop\artificial intelligence\opencv çalışmaları\mini project 5.py", line 41, in <module>
matches=sift_detector(cropped,template)
File "C:\Users\Ahmet\Desktop\artificial intelligence\opencv çalışmaları\mini project 5.py", line 10, in sift_detector
keypoints1, descriptors1=sift.detectAndCompute(image1,None)
error: C:\build\2_4_winpack-bindings-win64-vc14-static\opencv\modules\nonfree\src\sift.cpp:724: error: (-5) image is empty or has incorrect depth (!=CV_8U) in function cv::SIFT::operator ()