Javascript required
Skip to content Skip to sidebar Skip to footer

Using Cv2 Draw to Eliminate Overlaping Circles

Introduction

In this tutorial, nosotros will learn how to add a slider to a OpenCV window where we are displaying an paradigm, using Python.

A slider might be useful in scenarios where nosotros want to test dissimilar values for a function and we desire to alter those values manually in a simple user interface. For case, if we are converting an paradigm to black and white (similar covered hither) and we want to test dissimilar thresholds, we can use a slider to change the threshold value.

A simple example

As usual, we volition offset by importing the cv2 module. This volition make available the functions we need to brandish an image and add together a slider to it.

After that, nosotros will read an image from the file system. To do and then, we only demand to call the imread function, passing as input the path to the epitome. You lot tin check in more detail how to read and display an paradigm on this previous tutorial.

img = cv2.imread('C:/Users/N/Desktop/monument.png')            

After that, we volition display the image in a window, with a call to the imshow role. As first input nosotros need to pass the name of the window and as second the image. Annotation that we will define a variable to concord the proper noun of the window since we volition demand to also use it when creating the slider.

windowName = 'epitome'  cv2.imshow(windowName, img)            

To create a slider, we need to phone call the createTrackbar role from the cv2 module (note that OpenCV refers to this widget equally trackbar). The part receives the following parameters:

  • Name of the slider. We will telephone call it "slider";
  • Proper noun of the window where the slider will exist created. We will pass the previously created variable that holds the image window name;
  • Initial value of the slider in the scale. We will start at aught (the range for this widget always starts at 0);
  • Maximum value of the slider scale. We will use a value of 100;
  • Callback function that will exist called every time the position of the slider changes. Nosotros will cheque its definition beneath, merely it will be called on_change.
cv2.createTrackbar('slider', windowName, 0, 100, on_change)            

We will and then wait for the user to press a key to destroy the created window and end the plan.

cv2.waitKey(0) cv2.destroyAllWindows()            

To finalize, we volition check the implementation of the on_change role. Information technology will receive every bit input a variable with the current value of the slider. In the implementation of the function, we will just print the value received.

def on_change(value):     impress(value)            

The complete code can be seen below.

import cv2   def on_change(value):     print(value)   img = cv2.imread('C:/Users/N/Desktop/monument.png')  windowName = 'image'  cv2.imshow(windowName, img) cv2.createTrackbar('slider', windowName, 0, 100, on_change)  cv2.waitKey(0) cv2.destroyAllWindows()            

To test the previous code, only run it in a tool of your choice. I'll be using PyCharm, a Python IDE.

You should go a result like to figure 1. As tin can exist seen, a slider was added to the paradigm window, as expected. If you lot change its position, y'all should then meet its current value getting printed to the panel.

Adding a slider to the image with OpenCV.
Figure ane – Adding a slider to the paradigm with OpenCV.

A more than circuitous case

At present that we already covered the basics in the previous section, we will create a slightly more than complex application. Instead of simply press the value of the slider to the console, we will at present describe this value in the image.

All the code volition be the same except for the callback role. So, we will focus our analysis on the on_change function.

The kickoff thing nosotros will do is creating a copy of the original epitome. This is needed because the function to write text changes the image. So, if we always use the original image, the numbers will overlap over each other as nosotros are changing the slider. To avoid this, every time the callback executes, nosotros copy the original image (unaltered) and write the slider value on the epitome copy.

The procedure to copy an paradigm was covered in this post. In curt, we simply need to call the copy method on our original prototype.

To write text to an image, we need to call the putText function from the cv2 module. It receives the following parameters:

  • Image to write the text. We will laissez passer our paradigm copy;
  • Text to be fatigued, as a string. Nosotros will pass the value from the slider, after converting it to a string;
  • A tuple with the coordinates (x and y) of the lesser left corner where to put the text. We will put the text on the lesser left of the image. Nosotros will utilise x equal to 0 and y equal to the tiptop of the image minus x pixels (more details on how to get the image dimensions here);
  • The font, from the cv2 module. You can check the possible values here. We are going to use FONT_HERSHEY_SIMPLEX;
  • The calibration of the text. We will use a value of 1.0 to maintain the font-specific base of operations size;
  • A tuple with the BGR values for the color of the font. We will set up it to blackness (B=0, G=0, R=0);
  • The thickness of the lines to depict the text. We will use a value of 4.
cv2.putText(imageCopy, str(val), (0, imageCopy.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 0), 4)            

Now that nosotros accept fatigued the slider value in the image, nosotros volition display information technology in a window.

cv2.imshow(windowName, imageCopy)            

The whole callback function tin can be seen in the code snippet below.

def on_change(val):      imageCopy = img.copy()      cv2.putText(imageCopy, str(val), (0, imageCopy.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 0), 4)     cv2.imshow(windowName, imageCopy)            

The complete code can be seen below.

import cv2   def on_change(val):      imageCopy = img.copy()      cv2.putText(imageCopy, str(val), (0, imageCopy.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, i.0, (0, 0, 0), 4)     cv2.imshow(windowName, imageCopy)   img = cv2.imread('C:/Users/N/Desktop/monument.png')  windowName = 'prototype'  cv2.imshow(windowName, img) cv2.createTrackbar('slider', windowName, 0, 100, on_change)  cv2.waitKey(0) cv2.destroyAllWindows()            

Later running the previous code, you should get a result like shown on effigy 2. As can exist seen, when moving the slider, the value will exist drawn in the bottom left corner of the image.

Drawing the current slider value on the image.
Figure 2 – Cartoon the selected value on the epitome.

Other OpenCV Posts

  • Python OpenCV: Drawing circles
  • Python OpenCV: Drawing lines

Technical details

  • Operating system: Windows 8.1
  • Python version: 3.7.ii
  • OpenCV version: four.i.ii

desailllymindighisent.blogspot.com

Source: https://techtutorialsx.com/2020/12/13/python-opencv-add-slider-to-window/