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

Question:

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 :

img = cv2.imread('/home/user/Desktop/test_pic/1-0.png',0)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

it didn’t work and gave me this error :

cv2.error: /io/opencv/modules/highgui/src/window.cpp:583: error: (-2) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function cvShowImage

I have ran these commands:

sudo apt-get install build-essential checkinstall cmake pkg-config yasm
sudo apt-get install qt5-default libgtk2.0-dev libtbb-dev

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.

Asked By: Jess

||

Answers:

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

import cv2

SELECTION_COLOUR = (222,0,222)
WINDOW_NAME = "Select regions with a mouse"
OUTPUT_FILE = "selection.png"

def click_select(event, x, y, flags, data):
    image, points = data
    if event == cv2.EVENT_LBUTTONDOWN:
        points.append((x, y))
    elif event == cv2.EVENT_LBUTTONUP:
        points.append((x, y))
        cv2.rectangle(image, points[-2], points[-1], SELECTION_COLOUR, 2)
        cv2.imshow(WINDOW_NAME, image)

def show_mouse_select(image_filename):
    orig = cv2.imread(image_filename)
    image = orig.copy()
    cv2.namedWindow(WINDOW_NAME)

    points = []
    cv2.setMouseCallback(WINDOW_NAME, click_select, (image, points))

    while True:
        cv2.imshow(WINDOW_NAME, image)
        key = cv2.waitKey(1)
        if key == ord('q'): break       # press q to exit

    # Output points and save image
    if len(points)>1:
        print ("Points:")
        for i in range(0,len(points),2):
            a, b = points[i], points[i+1]
            print (min(a,b), max(a,b))

        cv2.imwrite(OUTPUT_FILE, image)
        print ("Saved to:", OUTPUT_FILE)

    cv2.destroyAllWindows()

if __name__=="__main__":
    from sys import argv
    show_mouse_select(argv[1])

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:

Points:
(27, 99) (53, 125)
(109, 22) (130, 46)
(42, 243) (71, 268)
(160, 209) (203, 249)
(146, 247) (178, 267)
(234, 285) (264, 310)
(261, 198) (306, 231)
(269, 226) (293, 250)
(285, 243) (312, 270)
(335, 257) (348, 268)
(355, 253) (370, 270)
(375, 248) (407, 269)
(417, 262) (451, 288)
(408, 293) (435, 311)
(356, 164) (392, 199)
(374, 193) (397, 221)
(440, 128) (464, 147)
(489, 150) (507, 168)
Saved to: selection.png

selection.png:

Annotated image of gulls flying over Azov sea

Answered By: Andriy Makukha
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.