Skip to content
Advertisement

How can i display my images and draw rectangles over regions of interests and get the coordinates?

What i want is to read an image and have it fixed on the screen, then manually draw rectangles on the regions of interest and have them outputed as coordinates of the rectangle as have the rectangle stay on the image to know which region of interest i marked.

The first step is actually reading the image and fixing it on the screen, what i did following the docs http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_image_display/py_image_display.html :

JavaScript

it didn’t work and gave me this error :

JavaScript

I have ran these commands:

JavaScript

but still didn’t work

The next step is drawing rectangles on the image and having output printed , i checked https://www.python-course.eu/tkinter_events_binds.php as well as Store mouse click event coordinates with matplotlib but the answer is kind of old and i need mine using CV2 not matplotlib unless i have no other choice. So guidance there will be appreciated.

Advertisement

Answer

Firstly, you need to reinstall OpenCV from source, just as @Wool pointed out. (You can skip second part of Step 3 of that article, since virtualenv, scipy, matplotlib, scikit-image, scikit-learn and ipython are not really necessary.)

As for the code, you can use something like this (based on this article):

JavaScript

If you save it to file click_select.py, you can then run python3 click_select.py YOURIMAGE.

Clicking down the left mouse button marks first point of a rectangle, and clicking it up marks second point. After releasing a mouse button you should see a magenta rectangle (if you have cv2 installed properly). Pressing the q button will exit the loop, print list of points to stdout and save annotated image.

Example image:

Gulls flying in Azov-Syvash National Park

Example output:

JavaScript

selection.png:

Annotated image of gulls flying over Azov sea

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement