# Mouse movement click event -color-:

**URL:** https://forum.opencv.org/t/mouse-movement-click-event-color/8996
**Category:** Python
**Tags:** programming
**Created:** [June 1, 2022, 2:16pm UTC](https://forum.opencv.org/t/mouse-movement-click-event-color/8996 "2022-06-01T14:16:32Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Ramh](https://avatars.discourse-cdn.com/v4/letter/r/8e8cbc/32.png) [@Ramh](https://forum.opencv.org/u/Ramh)
#### Post date: [June 1, 2022, 2:16pm UTC](https://forum.opencv.org/t/mouse-movement-click-event-color/8996/1 "2022-06-01T14:16:32Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/5/5a6a81a46e9d4d6edcf66eae5cca5e69586e25a8.jpeg)

---

<div class="post-metadata">

### Author: ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)
#### Post date: [June 1, 2022, 2:50pm UTC](https://forum.opencv.org/t/mouse-movement-click-event-color/8996/2 "2022-06-01T14:50:17Z")

</div>

> [@Ramh](#):
>
> 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?

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 `cv.circle()` with `thickness=cv.FILLED` argument

---

<div class="post-metadata">

### Author: ![Ramh](https://avatars.discourse-cdn.com/v4/letter/r/8e8cbc/32.png) [@Ramh](https://forum.opencv.org/u/Ramh)
#### Post date: [June 1, 2022, 3:40pm UTC](https://forum.opencv.org/t/mouse-movement-click-event-color/8996/3 "2022-06-01T15:40:43Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/c/c84597de7b42cd639efe67a2786d32b01b3e652a.jpeg)

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!

---

<div class="post-metadata">

### Author: ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)
#### Post date: [June 1, 2022, 6:05pm UTC](https://forum.opencv.org/t/mouse-movement-click-event-color/8996/4 "2022-06-01T18:05:45Z")

</div>

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})`

---

<div class="post-metadata">

### Author: ![Ramh](https://avatars.discourse-cdn.com/v4/letter/r/8e8cbc/32.png) [@Ramh](https://forum.opencv.org/u/Ramh)
#### Post date: [June 1, 2022, 7:09pm UTC](https://forum.opencv.org/t/mouse-movement-click-event-color/8996/5 "2022-06-01T19:09:09Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)
#### Post date: [June 1, 2022, 7:26pm UTC](https://forum.opencv.org/t/mouse-movement-click-event-color/8996/6 "2022-06-01T19:26:13Z")

</div>

> [@Ramh](#):
>
> have dots in the same image, with different colors as I already mentioned above the discussion?

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)_

---

<div class="post-metadata">

### Author: ![Ramh](https://avatars.discourse-cdn.com/v4/letter/r/8e8cbc/32.png) [@Ramh](https://forum.opencv.org/u/Ramh)
#### Post date: [June 1, 2022, 8:08pm UTC](https://forum.opencv.org/t/mouse-movement-click-event-color/8996/7 "2022-06-01T20:08:27Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)
#### Post date: [June 1, 2022, 10:10pm UTC](https://forum.opencv.org/t/mouse-movement-click-event-color/8996/8 "2022-06-01T22:10:37Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Ramh](https://avatars.discourse-cdn.com/v4/letter/r/8e8cbc/32.png) [@Ramh](https://forum.opencv.org/u/Ramh)
#### Post date: [June 2, 2022, 7:35am UTC](https://forum.opencv.org/t/mouse-movement-click-event-color/8996/9 "2022-06-02T07:35:22Z")

</div>

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?

N.B:  
-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.

---

<div class="post-metadata">

### Author: ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)
#### Post date: [June 2, 2022, 8:50am UTC](https://forum.opencv.org/t/mouse-movement-click-event-color/8996/10 "2022-06-02T08:50:30Z")

</div>

> [@Ramh](#):
>
> cv.putText(img, ‘.’, (x,y), font, .5 , (0, 0, 255), 4, cv.LINE\_AA, False)

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.

---

<div class="post-metadata">

### Author: ![Ramh](https://avatars.discourse-cdn.com/v4/letter/r/8e8cbc/32.png) [@Ramh](https://forum.opencv.org/u/Ramh)
#### Post date: [June 2, 2022, 9:09am UTC](https://forum.opencv.org/t/mouse-movement-click-event-color/8996/11 "2022-06-02T09:09:31Z")

</div>

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?

Sincerely.

---

<div class="post-metadata">

### Author: ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)
#### Post date: [June 2, 2022, 9:57am UTC](https://forum.opencv.org/t/mouse-movement-click-event-color/8996/12 "2022-06-02T09:57:23Z")

</div>

[random — Generate pseudo-random numbers — Python 3.10.4 documentation](https://docs.python.org/3/library/random.html#random.random) and related functions, depending on what range of values and what distribution you want.

[colorsys — Conversions between color systems — Python 3.10.4 documentation](https://docs.python.org/3/library/colorsys.html?highlight=color#colorsys.hsv_to_rgb) 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.

```auto
>>> 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)

```

---

<div class="post-metadata">

### Author: ![Ramh](https://avatars.discourse-cdn.com/v4/letter/r/8e8cbc/32.png) [@Ramh](https://forum.opencv.org/u/Ramh)
#### Post date: [June 2, 2022, 10:08am UTC](https://forum.opencv.org/t/mouse-movement-click-event-color/8996/13 "2022-06-02T10:08:20Z")

</div>

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