Home > Blockchain >  I cant see cv2.imshow result in for loop
I cant see cv2.imshow result in for loop

Time:11-01

I am new for coding. I am trying to make little project for myself. OpenCV showing template matching result but its seems like i cant crop it or imshow not giving me crop result here is my codes;

for myfile in files1:
    image = cv.imread(myfile,0)
    template_data.append(image)



for tmp in template_data:
    w, h = tmp.shape[::-1]
    result = cv.matchTemplate(img_gray,myfile,cv.TM_CCOEFF_NORMED)
    _, _, _, maxLoc=cv.minMaxLoc(result)
    topLeft = maxLoc
    bottom_right = (topLeft[0]   w , topLeft[1]   h)
    cv.rectangle(img_rgb,topLeft, bottom_right,255, 2)
    crop_img = img_rgb[maxLoc[1]:maxLoc[1] w, maxLoc[0]:maxLoc[0] h, :]
    cv.imshow("cropped", *crop_img)
    cv.waitKey()

Thanks for helping

CodePudding user response:

remove the * in imshow , you must pass the image matrix in one argument

cv.imshow("cropped", crop_img)

What does ** (double star/asterisk) and * (star/asterisk) do for parameters?

CodePudding user response:

Hey man I think i see your error. Instead of cv2.waitKey() replace it with.

cv2.waitKey(0) & 0xFF== ord(‘q’)

You gotta have a number in the waitKey because thats how long it waits until it runs the next tmp. Also if you add the ord(‘q’) part the code will wait till you press q before it moves onto the next step. you can also add

cv2.destroyAllWindows()

This will close the window when you hit the q key. One last thing to note is that the waitKey is waiting in milliseconds or 1/1000 of a second. So if you put in 500 it will wait half a second before continuing. It doesnt wait for the q key to be pressed if you do it that way.

Hope this was helpful!

  • Related