

{"id":120184,"date":"2024-04-06T18:00:41","date_gmt":"2024-04-06T12:30:41","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=120184"},"modified":"2024-04-06T18:33:12","modified_gmt":"2024-04-06T13:03:12","slug":"opencv-mouse-events","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/opencv-mouse-events\/","title":{"rendered":"OpenCV Mouse Events"},"content":{"rendered":"<p>Unlock the power of interaction in your image processing endeavours with OpenCV&#8217;s captivating mouse events! In the digital realm, where pixels come to life, learn how to harness the click, drag, and magic of the mouse to transform static images into dynamic canvases.<\/p>\n<p>Join us as we delve into the world of OpenCV&#8217;s mouse events, where every cursor movement unveils new possibilities.<\/p>\n<h2>Let\u2019s Know About Mouse events in OpenCV<\/h2>\n<h3>Need for Mouse Events:<\/h3>\n<p>Mouse events in OpenCV are essential for creating interactive applications that involve user interactions with images and videos. These events allow you to capture different actions performed by the user, such as clicking, moving, and releasing mouse buttons. By responding to these events, you can enable dynamic behaviours like drawing, selecting regions of interest, annotating images, and more. Mouse events enhance user engagement, making applications more intuitive and user-friendly.<\/p>\n<p><strong>List of Possible Mouse Events in OpenCV:<\/strong><\/p>\n<p><strong>1. cv2.EVENT_MOUSEMOVE:<\/strong> Triggered when the mouse cursor moves over the image or window. Useful for tracking cursor movement and updating visual feedback.<\/p>\n<p><strong>2. cv2.EVENT_LBUTTONDOWN:<\/strong> This occurs when the left mouse button is pressed down. It is commonly used to start actions like drawing or selecting.<\/p>\n<p><strong>3. cv2.EVENT_LBUTTONUP:<\/strong> Triggered when the left mouse button, previously pressed down, is released. Used to finalize actions like completing a drawing.<\/p>\n<p><strong>4. cv2.EVENT_RBUTTONDOWN:<\/strong> This happens when the right mouse button is pressed down. Useful for context menus or alternate interactions.<\/p>\n<p><strong>5. cv2.EVENT_RBUTTONUP:<\/strong> This occurs when the right mouse button, previously pressed down, is released. Concludes actions tied to the right button.<\/p>\n<p><strong>6. cv2.EVENT_MBUTTONDOWN:<\/strong> Triggered when the middle mouse button is pressed down. Can initiate various interactions or commands.<\/p>\n<p><strong>7. cv2.EVENT_MBUTTONUP:<\/strong> This occurs when the middle mouse button, previously pressed down, is released. Concludes actions involving the middle button.<\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Sr.No<\/b><\/td>\n<td><b>Mouse event &amp; its Description<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<td><span style=\"font-weight: 400\">cv.EVENT_MOUSEMOVE<\/span><\/p>\n<p><span style=\"font-weight: 400\">When the mouse pointer has moved over the window.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">2<\/span><\/td>\n<td><span style=\"font-weight: 400\">cv.EVENT_LBUTTONDOWN<\/span><\/p>\n<p><span style=\"font-weight: 400\">Indicates that the left mouse button is pressed.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">3<\/span><\/td>\n<td><span style=\"font-weight: 400\">cv.EVENT_RBUTTONDOWN<\/span><\/p>\n<p><span style=\"font-weight: 400\">Event of that, the right mouse button is pressed.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">4<\/span><\/td>\n<td><span style=\"font-weight: 400\">cv.EVENT_MBUTTONDOWN<\/span><\/p>\n<p><span style=\"font-weight: 400\">Indicates that the middle mouse button is pressed.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">5<\/span><\/td>\n<td><span style=\"font-weight: 400\">cv.EVENT_LBUTTONUP<\/span><\/p>\n<p><span style=\"font-weight: 400\">When the left mouse button is released.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">6<\/span><\/td>\n<td><span style=\"font-weight: 400\">cv.EVENT_RBUTTONUP<\/span><\/p>\n<p><span style=\"font-weight: 400\">When the right mouse button is released.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">7<\/span><\/td>\n<td><span style=\"font-weight: 400\">cv.EVENT_MBUTTONUP<\/span><\/p>\n<p><span style=\"font-weight: 400\">Indicates that the middle mouse button is released.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">8<\/span><\/td>\n<td><span style=\"font-weight: 400\">cv.EVENT_LBUTTONDBLCLK<\/span><\/p>\n<p><span style=\"font-weight: 400\">This event occurs when the left mouse button is double clicked.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">9<\/span><\/td>\n<td><span style=\"font-weight: 400\">cv.EVENT_RBUTTONDBLCLK<\/span><\/p>\n<p><span style=\"font-weight: 400\">This indicates that the right mouse button is double clicked.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">10<\/span><\/td>\n<td><span style=\"font-weight: 400\">cv.EVENT_MBUTTONDBLCLK<\/span><\/p>\n<p><span style=\"font-weight: 400\">This indicates that the middle mouse button is double clicked.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">11<\/span><\/td>\n<td><span style=\"font-weight: 400\">cv.EVENT_MOUSEWHEEL<\/span><\/p>\n<p><span style=\"font-weight: 400\">Positive for forward and negative for backward scrolling.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>These mouse events serve as building blocks for developing interactive image processing and computer vision applications. By responding to these events, you can create a wide range of user interactions, making your applications more engaging and dynamic.<\/p>\n<p><strong>We can Use the following code to check the faculties among OpenCV Mouse Events,<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import cv2  \r\n# Storing the mouse events supported by OpenCV as a list\r\nmouse_events = [i for i in dir(cv2) if 'EVENT' in i]\r\n# Displaying all the mouse events available\r\nprint(mouse_events)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/faculties-among-OpenCV-Mouse-Events.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-126035 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/faculties-among-OpenCV-Mouse-Events.webp\" alt=\"faculties among OpenCV Mouse Events\" width=\"868\" height=\"892\" \/><\/a><\/p>\n<h3>Mouse Magic Unleashed: Crafting Dynamic Interactions with OpenCV&#8217;s Callback Functions<\/h3>\n<p>In OpenCV, callback functions play a crucial role in responding to user interactions such as mouse events. These functions are designed to be called automatically by OpenCV whenever a specific mouse event occurs. Callback functions allow you to define custom logic that should be executed in response to different interactions, providing you with the flexibility to create interactive and dynamic applications.<\/p>\n<h4>Syntax of Callback Function for Mouse Events:<\/h4>\n<p><strong>The syntax of a callback function for mouse events is as follows:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def callback_function(event, x, y, flags, param):\r\n    # Your code here<\/pre>\n<h4>Parameters of the Callback Function:<\/h4>\n<p><strong>The callback function for mouse events receives the following parameters:<\/strong><\/p>\n<p><strong>1. Event:<\/strong> An integer code representing the type of mouse event that occurred. The event codes correspond to different interactions, such as mouse movement, button presses, and button releases. Common event codes include cv2.EVENT_MOUSEMOVE, cv2.EVENT_LBUTTONDOWN, cv2.EVENT_LBUTTONUP, and so on.<\/p>\n<p><strong>2. x:<\/strong> The x-coordinate of the point where the mouse event occurred.<\/p>\n<p><strong>3. y:<\/strong> The y-coordinate of the point where the mouse event occurred.<\/p>\n<p><strong>4. Flags:<\/strong> Additional flags associated with the event. These flags provide additional context about the event, such as whether a specific keyboard key was pressed simultaneously with the mouse event.<\/p>\n<p><strong>5. param:<\/strong> An optional parameter that you can pass when setting up the mouse callback using cv2.setMouseCallback(). This parameter allows you to pass custom data to your callback function, enabling you to maintain the state or pass additional information as needed.<\/p>\n<p>Inside the callback function, you can use the provided parameters to define specific behaviors based on the type of mouse event that occurred, the coordinates of the event, and any additional context provided by the flags.<\/p>\n<p>By creating and configuring a callback function for mouse events, you can build interactive applications that respond to user interactions in real time, enhancing the user experience and enabling dynamic visual manipulations.<\/p>\n<h3>Introduction to Creating Circles Over a Black Blank Window:<\/h3>\n<p>Dive into a realm of creativity as we unveil the magic of creating circles on a canvas of infinite possibilities. In this section, we&#8217;ll guide you through the captivating process of using OpenCV&#8217;s cv2.setMouseCallback() to draw circles over a blank black window. Whether it&#8217;s unleashing your inner artist or testing concepts, this technique empowers you to effortlessly craft circles with the precision of your cursor.<\/p>\n<h4>Using cv2.setMouseCallback() to Create Circles:<\/h4>\n<p>OpenCV&#8217;s cv2.setMouseCallback() function enables you to respond to mouse events and seamlessly create circles on a blank window.<\/p>\n<p><strong> Here&#8217;s the syntax, parameter explanation, and code with a detailed explanation:<\/strong><\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">cv2.setMouseCallback(window_name, callback_function)<\/pre>\n<p><strong>Parameters:<\/strong><\/p>\n<ul>\n<li><strong>Window_name:<\/strong> The name of the window where mouse events are tracked.<\/li>\n<li><strong>Callback_function:<\/strong> The function that is called when a mouse event occurs.<\/li>\n<\/ul>\n<p><strong>Code with Explanation:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import cv2\r\nimport numpy as np  # Import the NumPy module\r\n\r\n# Callback function for drawing circles\r\ndef draw_circle(event, x, y, flags, param):\r\n    if event == cv2.EVENT_LBUTTONDOWN:\r\n        cv2.circle(blank_image, (x, y), 20, (255, 255, 255), -1)\r\n\r\n# Create a black blank window\r\nwindow_size = (800, 600)\r\nblank_image = np.zeros((window_size[1], window_size[0], 3), dtype=np.uint8)\r\n\r\ncv2.namedWindow(\"Blank Window\")\r\ncv2.setMouseCallback(\"Blank Window\", draw_circle)\r\n\r\nwhile True:\r\n    cv2.imshow(\"Blank Window\", blank_image)\r\n    key = cv2.waitKey(1) &amp; 0xFF\r\n    if key == ord(\"q\"):\r\n        break\r\n\r\ncv2.destroyAllWindows()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Create-Circles-output.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-126037 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Create-Circles-output.webp\" alt=\"Create Circles output\" width=\"1195\" height=\"943\" \/><\/a><\/p>\n<p>1. We import the required modules, including OpenCV (cv2) and NumPy (np).<\/p>\n<p>2. The draw_circle function is defined as the callback function. It draws a white circle centred at the mouse click position when the left mouse button is pressed.<\/p>\n<p>3. A blank black window is created using NumPy. Its size is defined by window_size.<\/p>\n<p>4. The window is named &#8220;Blank Window&#8221; using cv2.namedWindow().<\/p>\n<p>5. cv2.setMouseCallback() associates the callback function `draw_circle` with the &#8220;Blank Window&#8221;. This enables the window to respond to mouse events.<\/p>\n<p>6. The main loop displays the blank window and listens for key presses. Pressing &#8216;q&#8217; exits the loop and closes the window.<\/p>\n<p>By harnessing the synergy of cv2.setMouseCallback(), you&#8217;ll immerse yourself in a world of interactive creativity, drawing circles that reflect your every click on a canvas of endless potential.<\/p>\n<h3>Introduction to Creating a Rectangle Over an Image Using Mouse Events:<\/h3>\n<p>Unlock the ability to define regions of interest with the elegance of your cursor. In this section, we embark on a journey through OpenCV&#8217;s captivating cv2.setMouseCallback() technique, revealing how effortlessly you can draw rectangles over images. Whether it&#8217;s pinpointing objects or framing important details, this approach empowers you to create interactive rectangles that add depth to your visual projects.<\/p>\n<h4>Using cv2.setMouseCallback() to Create Rectangles:<\/h4>\n<p>OpenCV&#8217;s cv2.setMouseCallback() function opens the door to dynamic rectangle creation on images.<\/p>\n<p><strong> Below, you&#8217;ll find the syntax, parameter explanation, and a code snippet with detailed explanation:<\/strong><\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">cv2.setMouseCallback(window_name, callback_function)<\/pre>\n<p><strong>Parameters:<\/strong><\/p>\n<ul>\n<li><strong>Window_name:<\/strong> The name of the window where mouse events are tracked.<\/li>\n<li><strong>Callback_function:<\/strong> The function that is called when a mouse event occurs.<\/li>\n<\/ul>\n<p><strong>Code with Explanation:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import cv2\r\nimport numpy as np\r\n\r\n# Global variables to track rectangle drawing\r\nrectangle_start = None\r\nrectangle_end = None\r\ndrawing = False\r\n\r\n# Callback function for drawing rectangles\r\ndef draw_rectangle(event, x, y, flags, param):\r\n    global rectangle_start, rectangle_end, drawing\r\n\r\n    if event == cv2.EVENT_LBUTTONDOWN:\r\n        rectangle_start = (x, y)\r\n        drawing = True\r\n    elif event == cv2.EVENT_LBUTTONUP:\r\n        rectangle_end = (x, y)\r\n        drawing = False\r\n\r\n# Create a black blank window\r\nwindow_size = (800, 600)\r\nblank_image = np.zeros((window_size[1], window_size[0], 3), dtype=np.uint8)\r\n\r\n# Create a window and set callback\r\ncv2.namedWindow(\"Blank Window\")\r\ncv2.setMouseCallback(\"Blank Window\", draw_rectangle)\r\n\r\nwhile True:\r\n    display_image = blank_image.copy()\r\n\r\n    if rectangle_start and not drawing:\r\n        cv2.rectangle(display_image, rectangle_start, rectangle_end, (0, 255, 0), 2)\r\n\r\n    cv2.imshow(\"Blank Window\", display_image)\r\n    key = cv2.waitKey(1) &amp; 0xFF\r\n    if key == ord(\"q\"):\r\n        break\r\n\r\ncv2.destroyAllWindows()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Creating-a-Rectangle-output-1.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-126039 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Creating-a-Rectangle-output-1.webp\" alt=\"Creating a Rectangle output\" width=\"1194\" height=\"942\" \/><\/a><\/p>\n<p><strong>Explanation:<\/strong><\/p>\n<p>1. We import the necessary modules, including OpenCV (cv2) and NumPy (np).<\/p>\n<p>2. rectangle_start and rectangle_end are global variables to track the points of the rectangle.<\/p>\n<p>3. Drawing indicates if the user is currently drawing the rectangle.<\/p>\n<p>4. The draw_rectangle function serves as the callback. It tracks mouse events to capture the starting and ending points of the rectangle.<\/p>\n<p>5. When the left mouse button is pressed, rectangle_start is set, and drawing is initiated. When the button is released, rectangle_end is set, and drawing is halted.<\/p>\n<p>6. The loaded image is displayed in a window named &#8220;Image&#8221;.<\/p>\n<p>7. The rectangle, if currently being drawn, is displayed on display_image.<\/p>\n<p>8. Pressing &#8216;q&#8217; exits the loop and closes the window.<\/p>\n<p>By grasping the concept of cv2.setMouseCallback(), you&#8217;ll gain the power to seamlessly draw rectangles over images, enhancing your visual projects with interactive precision and detail.<\/p>\n<h3>Using cv2.setMouseCallback() to Use the Mouse as a Paintbrush:<\/h3>\n<p>With OpenCV&#8217;s cv2.setMouseCallback() function, the potential for turning your cursor into a paintbrush is at your fingertips.<\/p>\n<p><strong>Below, you&#8217;ll find the syntax, parameter explanation, and a code snippet with detailed explanation:<\/strong><\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">cv2.setMouseCallback(window_name, callback_function)<\/pre>\n<p><strong>Parameters:<\/strong><\/p>\n<ul>\n<li><strong>Window_name:<\/strong> The name of the window where mouse events are tracked.<\/li>\n<li><strong>Callback_function:<\/strong> The function that is called when a mouse event occurs.<\/li>\n<\/ul>\n<p><strong>Code with Explanation:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import cv2\r\nimport numpy as np\r\n\r\n# Global variables for drawing\r\ndrawing = False\r\ncolor = (0, 0, 255)  # BGR color (red)\r\n\r\n# Callback function for using the mouse as a paintbrush\r\ndef use_paintbrush(event, x, y, flags, param):\r\n    global drawing, color\r\n\r\n    if event == cv2.EVENT_LBUTTONDOWN:\r\n        drawing = True\r\n        cv2.circle(painting, (x, y), 10, color, -1)\r\n    elif event == cv2.EVENT_LBUTTONUP:\r\n        drawing = False\r\n    elif event == cv2.EVENT_MOUSEMOVE and drawing:\r\n        cv2.circle(painting, (x, y), 10, color, -1)\r\n\r\n# Create a black blank window for painting\r\nwindow_size = (800, 600)\r\n\r\npainting = np.zeros((window_size[1], window_size[0], 3), dtype=np.uint8)\r\n\r\n\r\n\r\n\r\n# Create a window and set the callback\r\ncv2.namedWindow(\"Painting Canvas\")\r\n\r\ncv2.setMouseCallback(\"Painting Canvas\", use_paintbrush)\r\n\r\n\r\n\r\n\r\nwhile True:\r\n    cv2.imshow(\"Painting Canvas\", painting)\r\n    key = cv2.waitKey(1) &amp; 0xFF\r\n    if key == ord(\"q\"):\r\n        break\r\n    elif key == ord(\"b\"):\r\n\r\n        color = (255, 0, 0)  # Change color to blue\r\n    elif key == ord(\"g\"):\r\n\r\n        color = (0, 255, 0)  # Change color to green\r\n    elif key == ord(\"r\"):\r\n\r\n        color = (0, 0, 255)  # Change color to red\r\n\r\ncv2.destroyAllWindows()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Mouse-as-a-Paintbrush-output.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-126040 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Mouse-as-a-Paintbrush-output.webp\" alt=\"Mouse as a Paintbrush output\" width=\"1197\" height=\"945\" \/><\/a><\/p>\n<p><strong>Explanation:<\/strong><\/p>\n<p>1. We import the necessary modules, including OpenCV (cv2) and NumPy (np).<\/p>\n<p>2. Drawing indicates if you&#8217;re currently drawing with the &#8220;paintbrush.&#8221;<\/p>\n<p>3. The color variable holds the current BGR color value (starting with red).<\/p>\n<p>4. The `use_paintbrush` function serves as the callback. It captures mouse events to draw circles on the &#8220;painting&#8221; canvas.<\/p>\n<p>5. When the left mouse button is pressed, a circle is drawn at the cursor&#8217;s position. The drawing continues as long as the button is held down.<\/p>\n<p>6. Releasing the left mouse button stops drawing.<\/p>\n<p>7. Moving the mouse while holding down the button results in continuous drawing.<\/p>\n<p>8. The blank painting canvas is displayed in a window named &#8220;Painting Canvas.<\/p>\n<p>9. Pressing &#8216;q&#8217; exits the loop and closes the window.<\/p>\n<p>10. Pressing &#8216;b&#8217;, &#8216;g&#8217;, or &#8216;r&#8217; changes the paintbrush color to blue, green, or red respectively.<\/p>\n<p>By harnessing the potential of `cv2.setMouseCallback()`, you&#8217;ll seamlessly transform your cursor into a digital paintbrush, allowing you to express your creativity through vibrant strokes on a canvas of boundless possibilities.<\/p>\n<h3>Introduction to Putting Text Over an Image:<\/h3>\n<p>Unlock a realm of annotation and communication as we explore the art of adding text overlays to images. In this section, we delve into the captivating process of leveraging OpenCV to effortlessly embed textual information onto visuals. Whether you&#8217;re providing context, labelling objects, or personalizing your creations, this technique empowers you to add a dynamic layer of information to your images.<\/p>\n<h4>Code for Putting Text Over an Image:<\/h4>\n<p>Using OpenCV, adding text over a picture is a simple operation. <strong>Here is some sample code that shows how to accomplish this:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import cv2\r\nimport numpy as np\r\n\r\n# Create a black blank window\r\nwindow_size = (800, 600)\r\nblank_image = np.zeros((window_size[1], window_size[0], 3), dtype=np.uint8)\r\n\r\n# Define text properties\r\ntext = \"Data Flair\"\r\nfont = cv2.FONT_HERSHEY_SIMPLEX\r\nfont_scale = 1.5\r\nfont_color = (255, 255, 255)  # BGR color (white)\r\nthickness = 2\r\n\r\n# Get text size\r\ntext_size = cv2.getTextSize(text, font, font_scale, thickness)[0]\r\ntext_width, text_height = text_size\r\n\r\n# Calculate text position\r\nx = (blank_image.shape[1] - text_width) \/\/ 2\r\ny = blank_image.shape[0] \/\/ 2  # Center vertically\r\n\r\n# Put text on the blank image\r\ncv2.putText(blank_image, text, (x, y), font, font_scale, font_color, thickness)\r\n\r\ncv2.imshow(\"Image with Text\", blank_image)\r\ncv2.waitKey(0)\r\ncv2.destroyAllWindows()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Putting-Text-Over-output.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-126041 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/Putting-Text-Over-output.webp\" alt=\"Putting Text Over output\" width=\"1198\" height=\"943\" \/><\/a><\/p>\n<p>By harnessing the capabilities of OpenCV, you&#8217;ll seamlessly infuse your images with textual information, enhancing their communication potential and enabling personalized storytelling.<\/p>\n<h3>Summary<\/h3>\n<p>Exploring the realm of mouse events in OpenCV has unveiled a world of interactive possibilities for image manipulation and creativity. From creating circles and rectangles to turning the mouse into a digital paintbrush, the dynamic potential of cv2.setMouseCallback() has been revealed. By harnessing this capability, you can effortlessly add text overlays, annotate images, and bring interactivity to your projects.<\/p>\n<p>OpenCV&#8217;s mouse events empower you to engage with images in ways that transcend static visuals, enabling you to create intuitive applications, artistic masterpieces, and educational tools. By understanding the syntax, parameters, and practical code examples provided in this article, you&#8217;ve gained a toolkit to embark on a journey of dynamic image interactions and enhanced user experiences.<\/p>\n<p>With the power of mouse events in OpenCV at your fingertips, you&#8217;re poised to explore, innovate, and transform images into captivating stories that resonate with audiences and breathe life into your visual endeavours.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unlock the power of interaction in your image processing endeavours with OpenCV&#8217;s captivating mouse events! In the digital realm, where pixels come to life, learn how to harness the click, drag, and magic of&#46;&#46;&#46;<\/p>\n","protected":false},"author":86671,"featured_media":135613,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27755],"tags":[31069,31066,31028,31065,31067,31068,31064,29991],"class_list":["post-120184","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-opencv-tutorials","tag-handle-mouse-events-in-opencv","tag-handling-opencv-mouse-events","tag-learn-opencv","tag-mouse-event-in-opencv","tag-mouse-events-using-opencv","tag-mouse-events-with-opencv","tag-opencv-mouse-events","tag-opencv-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>OpenCV Mouse Events - DataFlair<\/title>\n<meta name=\"description\" content=\"Mouse events in OpenCV are essential for creating interactive applications that involve user interactions with images and videos.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/opencv-mouse-events\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"OpenCV Mouse Events - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Mouse events in OpenCV are essential for creating interactive applications that involve user interactions with images and videos.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/opencv-mouse-events\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-04-06T12:30:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-06T13:03:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2024\/04\/opencv-mouse-events-scaled.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"2560\" \/>\n\t<meta property=\"og:image:height\" content=\"1340\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"OpenCV Mouse Events - DataFlair","description":"Mouse events in OpenCV are essential for creating interactive applications that involve user interactions with images and videos.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/opencv-mouse-events\/","og_locale":"en_US","og_type":"article","og_title":"OpenCV Mouse Events - DataFlair","og_description":"Mouse events in OpenCV are essential for creating interactive applications that involve user interactions with images and videos.","og_url":"https:\/\/data-flair.training\/blogs\/opencv-mouse-events\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2024-04-06T12:30:41+00:00","article_modified_time":"2024-04-06T13:03:12+00:00","og_image":[{"width":2560,"height":1340,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2024\/04\/opencv-mouse-events-scaled.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/opencv-mouse-events\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/opencv-mouse-events\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/0e594f928e31fc96628ac40f6ae74f49"},"headline":"OpenCV Mouse Events","datePublished":"2024-04-06T12:30:41+00:00","dateModified":"2024-04-06T13:03:12+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/opencv-mouse-events\/"},"wordCount":1881,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/opencv-mouse-events\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2024\/04\/opencv-mouse-events-scaled.webp","keywords":["handle mouse events in opencv","handling opencv mouse events","learn opencv","mouse event in opencv","mouse events using opencv","mouse events with opencv","opencv mouse events","opencv tutorials"],"articleSection":["OpenCV Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/opencv-mouse-events\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/opencv-mouse-events\/","url":"https:\/\/data-flair.training\/blogs\/opencv-mouse-events\/","name":"OpenCV Mouse Events - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/opencv-mouse-events\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/opencv-mouse-events\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2024\/04\/opencv-mouse-events-scaled.webp","datePublished":"2024-04-06T12:30:41+00:00","dateModified":"2024-04-06T13:03:12+00:00","description":"Mouse events in OpenCV are essential for creating interactive applications that involve user interactions with images and videos.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/opencv-mouse-events\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/opencv-mouse-events\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/opencv-mouse-events\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2024\/04\/opencv-mouse-events-scaled.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2024\/04\/opencv-mouse-events-scaled.webp","width":2560,"height":1340,"caption":"opencv mouse events"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/opencv-mouse-events\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"OpenCV Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/opencv-tutorials\/"},{"@type":"ListItem","position":3,"name":"OpenCV Mouse Events"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/0e594f928e31fc96628ac40f6ae74f49","name":"TechVidvan Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","caption":"TechVidvan Team"},"description":"TechVidvan Team provides high-quality content &amp; courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.","url":"https:\/\/data-flair.training\/blogs\/author\/test001\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/120184","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/86671"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=120184"}],"version-history":[{"count":5,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/120184\/revisions"}],"predecessor-version":[{"id":134071,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/120184\/revisions\/134071"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/135613"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=120184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=120184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=120184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}