Mouse movement click event -color-:

I want to detect points with different color (each point with a specific color) by clicking the left mouse button. Could you help me please?

For the moment, all detected points are of the same color.

I just started with python and OpenCV.

I thank you in advance.

Below is the resulting image and the piece of code I configured

not “detect”. detection is for things that are already there but you don’t know where.

what you do is “marking” those points.

if you want to use different colors, then use different colors. you see the color argument to putText…
also, putText is for text. if you just want to draw a dot, use with thickness=cv.FILLED argument

Thank you for your comments.
However, to get I tested the 3 combinations of the color argument for the putText and I was able to get other colors for my points, as shown in the mage below.

However, I’m looking for points in the same image, but with different colors (red, blue, orange, yellow, green…etc).

I don’t know if you can understand the principle of what I am looking for?

Thank you!

can it be, you’re looking for the color values of the pixel you clicked ?
(instead of drawing a point of some color)

that would be a simple: print(img[y,x})

1 Like

Thank you for your answer and your proposal.

But I still wonder if there is a possibility to have dots in the same image, with different colors as I already mentioned above the discussion?

Thanks again.

yes sure, that’s very possible. do you want them colored randomly? how do you want to determine each dot’s color?

is this an assignment for a job interview or a class? are they making you write that program without saying why?

(Now playing: imdb:tt0387808)

1 Like

Whether they are randomly colored or not, the main thing is that I manage to have these points of each mouse click with a specific color.

It is a small project of initiation that we realize for a course. Then to work on detector/describer (SIFT and SURF). At first, I want to display the point on which I clicked on the image with its specific color and print its coordinates.

Thank you.

so… you aren’t asking for help, right? just chatting? because you keep not answering how you want to specify the color even though you keep talking about “specific color”

or do you want to sample the color of the picture, instead of drawing a dot? because that’s not what you asked for at any point. besides, your picture is grayscale.

I’m sorry, but I don’t know why you’re telling me this when I’ve answered all your questions!
I told you the result I wanted to obtain (display the point on which I clicked on the image with a specific color and print its coordinates), for the moment I used the function cv.putText(img, ‘.’, (x,y), font, .5 , (0, 0, 255), 4, cv.LINE_AA, False) but I get only one color for all the points.

So what I’m asking you is if there is a function to add to my code or an argument that I need to configure to get the result I want?

-Concerning the images, I work on the images at the gray scale.
-I don’t know how to specify the color of my points, that’s why I ask for your help!
-To sample the color of the image? ! I don’t know. Could you tell me more, please?

-I want to tell you again that I am new in this field!

My best regards.

the (0, 0, 255) argument is the color. that value represents red. the order is (blue, green, red).

if you’re hoping to be handed complete code that does something you can send in to the instructor, that will not happen.

you keep repeating the goal but you keep not addressing my question as to how you think you’d want to specify different colors at runtime. if you change that constant in the code, that’ll only change the color, but for all points you draw. if you wanted multiple points with different colors, you would have to come up with something that lets you set the color at runtime. I already suggested to use random values, which does not require user interaction.

if you’re asking how to let the user enter different color values… lots of options. do you want a color picker dialog? that’ll require either using some real GUI (tkinter?) or opening a second imshow window, with a color palette, and a mouse handler, so you can click there and sample a color from that window.

1 Like

Thank you very much for taking the time to answer.

Could you direct me to a tutorial that I can follow to get more details and better understand your proposal to use random values, to finally test it on my image?


random — Generate pseudo-random numbers — Python 3.10.4 documentation and related functions, depending on what range of values and what distribution you want.

colorsys — Conversions between color systems — Python 3.10.4 documentation if you want colors that aren’t dull, pick a random hue, full saturation, full lightness, and convert from HSV to RGB… and then remember to reorder the values from RGB to BGR because OpenCV wants BGR.

>>> r,g,b = colorsys.hsv_to_rgb(random.random(), 1.0, 255.0); (int(b), int(g), int(r))
(67, 255, 0)
>>> r,g,b = colorsys.hsv_to_rgb(random.random(), 1.0, 255.0); (int(b), int(g), int(r))
(0, 255, 7)
>>> r,g,b = colorsys.hsv_to_rgb(random.random(), 1.0, 255.0); (int(b), int(g), int(r))
(109, 255, 0)
1 Like

Thank you for your help.
I will look at the decomentation.