Ask Your Question

python beginner mouseclicking problem

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

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

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)

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

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

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

berak gravatar image

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

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


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

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

Question Tools

1 follower


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

Seen: 40 times

Last updated: Nov 15 '20