如何在感兴趣的区域显示我的图像和绘制矩形并获得坐标?

时间:2018-04-05 11:17:12

标签: python opencv image-processing cv2

我想要的是阅读图像并将其固定在屏幕上,然后手动在感兴趣的区域上绘制矩形并将它们作为矩形的坐标输出,因为矩形保留在图像上以知道哪个区域我标记的兴趣。

第一步是实际读取图像并将其固定在屏幕上,我按照文档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()

它不起作用并给了我这个错误:

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

我已经运行了这些命令:

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

但仍然无效

下一步是在图像上绘制矩形并打印输出,我检查了https://www.python-course.eu/tkinter_events_binds.php以及Store mouse click event coordinates with matplotlib,但答案有点陈旧,我需要使用CV2而不是matplotlib,除非我别无选择。所以那里的指导将受到赞赏。

1 个答案:

答案 0 :(得分:0)

首先,您需要从源代码重新安装OpenCV,就像@Wool pointed out一样。 (你可以跳过那篇文章第3步的第二部分,因为virtualenv,scipy,matplotlib,scikit-image,scikit-learn和ipython并不是必需的。)

至于代码,您可以使用类似的内容(基于this文章):

$ ./bin/classes <dat/classes.txt
Class: ID-CSCI112, Name-Programming with C,                      Credits-3
Class: ID-CSCI127, Name-Joy and Beauty of Data,                  Credits-4
Class: ID-CSCI132, Name-Basic Data Structures and Algorithms,    Credits-4
Class: ID-CSCI338, Name-Computer Science Theory,                 Credits-3
Class: ID-CSCI215, Name-Social and Ethical Issues in CS,         Credits-3
Class: ID-ARCH112, Name-Introduction to Design,                  Credits-3
Class: ID-COMX112, Name-Interpersonal Skills in the Workplace,   Credits-1
Class: ID-HSTA101, Name-American History,                        Credits-4
Class: ID-XXXXXXX, Name-XXXXXX,                                  Credits-0

CSCI course! - Programming with C
CSCI course! - Joy and Beauty of Data
CSCI course! - Basic Data Structures and Algorithms
CSCI course! - Computer Science Theory
CSCI course! - Social and Ethical Issues in CS
CSCI classes: 5
CSCI class credits: 17

如果将其保存到文件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]) ,则可以运行click_select.py

单击鼠标左键标记矩形的第一个点,然后单击它标记第二个点。释放鼠标按钮后,您应该看到一个洋红色矩形(如果您正确安装了cv2)。按python3 click_select.py YOURIMAGE按钮将退出循环,打印指向stdout的点列表并保存带注释的图像。

示例图片:

Gulls flying in Azov-Syvash National Park

示例输出:

q

selection.png:

Annotated image of gulls flying over Azov sea