Ask Your Question
0

python beginner mouseclicking problem

asked 2020-11-15 09:59:55 -0600

updated 2020-11-15 10:03:18 -0600

berak gravatar image

Hello, i'm learning the basics of opencv. I am working with the bit of code below. It generates a black image, i cannot click in some areas of the image without getting an error. Can anyone say why? seems to work ok on images that have the same value for x & y.

import cv2
import numpy as np


def click_event(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        blue = img[x, y, 0]
        green = img[x, y, 1]
        red = img[x, y, 2]
        print(blue, ' ', green, ' ', red)
        print(x, ' ', y)


img = np.zeros((577, 433, 3), np.uint8)

print(img.shape)
cv2.imshow('image', img)
cv2.setMouseCallback('image', click_event)

cv2.waitKey(0)
cv2.destroyAllWindows()
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-11-15 10:07:22 -0600

berak gravatar image

updated 2020-11-15 10:09:09 -0600

welcome to row / col land ;)

both opencv Mat's and numpy arrays are row-major, meaning you have to index like:

img[ y, x, channel ]

seems to work ok on images that have the same value for x & y.

makes a lot of sense now, right ? (just that you have it transposed, then)

edit flag offensive delete link more

Comments

Spectacular. That works a treat. Thank you very much!

thescream gravatar imagethescream ( 2020-11-15 15:00:13 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-11-15 09:51:25 -0600

Seen: 387 times

Last updated: Nov 15 '20