Witam,
mam problem z porównywaniem obrazów z wykorzystaniem pakietu cv w pythonie. Problem jest w tym, że chce sprawdzić skale podobieństwa pomiędzy dwoma obrazami, wykorzystuje tutaj metodę threshold. Problem w tym, że uzyskuje bardzo małą miarę podobieństwa pomiędzy obrazem temp a obrazem z kamery. W momencie, kiedy obraz z kamery przedstawia białą kartkę i miara podobieństwa jest znacznie większa niż jak mam obiekt porównywany.
Tutaj jest kod.
def f_TM_CCOEFF():
# intialize the webcam and pass a constant which is 0
cam = cv2.VideoCapture(0)
# title of the app
cv2.namedWindow('python webcam screenshot app')
# let's assume the number of images gotten is 0
img_counter = 0
# while loop
while True:
# intializing the frame, ret
ret, frame = cam.read()
# if statement
if not ret:
print('failed to grab frame')
break
# the frame will show with the title of test
cv2.imshow('test', frame)
#to get continuous live video feed from my laptops webcam
k = cv2.waitKey(1)
# if the escape key is been pressed, the app will stop
if k%256 == 27:
print('escape hit, closing the app')
break
# if the spacebar key is been pressed
# screenshots will be taken
elif k%256 == 32:
# the format for storing the images scrreenshotted
img_name = "ORGINAL/opencv_frame_{}.png".format(img_counter)
# saves the image as a png file
cv2.imwrite(img_name, frame)
print('screenshot taken')
# the number of images automaticallly increases by 1
img_counter += 1
# release the camera
cam.release()
#img = cv2.imread(img_name,0)
#sql = "SELECT photo (photo, userid) FROM photos VALUES (%s, (SELECT Uid FROM `users` WHERE `username` = '"+ username_entry.get().strip() + "'))"
#sql = "SELECT photo, userid FROM photos VALUES (%s, (SELECT Uid FROM `users` WHERE `username` = '"+ username_entry.get().strip() + "'))"
sql = "SELECT photo FROM photos WHERE userid = (SELECT Uid FROM users WHERE username = '"+ username_entry.get().strip() + "')"
cursor= db.cursor(buffered=True)
cursor.execute(sql, )
data2 = cursor.fetchall()
db.commit()
# data2 wyciaga nam z bazy danych sciezke (b'PHOTO/opencv_frame_109.png',) a nie sam obrazek
# 1. ze sciezki wyciagnac plik
# 2. inaczej zapisac w bazie danych obrazek
template = cv2.imread(sql)
print(data2)
w, h = template.shape[:-1]
method = cv2.TM_CCOEFF
# img = img2.copy()
# Apply template Matching
res = cv2.matchTemplate(img,template,method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
# If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle (img, top_left, bottom_right, 255, 2)
#bottom_right = (top_left[0] + w, top_left[1] + h)
#cv.rectangle(img,top_left, bottom_right, 255, 2)
plt.subplot(121),plt.imshow(res,cmap = 'gray')
plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(img,cmap = 'gray')
plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
plt.show()
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
#frame.close()
#cap.release()
cv2.destroyAllWindows()