我如何显示我的图像并在感兴趣的区域上绘制矩形并获取坐标?
How can i display my images and draw rectangles over regions of interests and get the coordinates?
我想要的是读取图像并将其固定在屏幕上,然后在感兴趣的区域上手动绘制矩形并将它们输出为矩形的坐标,因为矩形留在图像上以了解哪个我标记的感兴趣区域。
第一步实际上是读取图像并将其固定在屏幕上,我按照文档所做的 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 as well as Store mouse click event coordinates with matplotlib 但答案有点旧,我需要使用 CV2
而不是 matplotlib 除非我没有其他选择。因此,将不胜感激。
首先,您需要从源代码重新安装 OpenCV,就像@Wool pointed out 一样。 (您可以跳过该文章第 3 步的第二部分,因为 virtualenv、scipy、matplotlib、scikit-image、scikit-learn 和 ipython 并不是真正必要的。)
至于代码,你可以使用这样的东西(基于this文章):
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
,则可以 运行 python3 click_select.py YOURIMAGE
.
单击鼠标左键标记矩形的第一个点,向上单击标记第二个点。释放鼠标按钮后,您应该会看到一个洋红色矩形(如果您正确安装了 cv2)。按 q
按钮将退出循环,将点列表打印到标准输出并保存注释图像。
示例图片:
示例输出:
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:
我想要的是读取图像并将其固定在屏幕上,然后在感兴趣的区域上手动绘制矩形并将它们输出为矩形的坐标,因为矩形留在图像上以了解哪个我标记的感兴趣区域。
第一步实际上是读取图像并将其固定在屏幕上,我按照文档所做的 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 as well as Store mouse click event coordinates with matplotlib 但答案有点旧,我需要使用 CV2
而不是 matplotlib 除非我没有其他选择。因此,将不胜感激。
首先,您需要从源代码重新安装 OpenCV,就像@Wool pointed out 一样。 (您可以跳过该文章第 3 步的第二部分,因为 virtualenv、scipy、matplotlib、scikit-image、scikit-learn 和 ipython 并不是真正必要的。)
至于代码,你可以使用这样的东西(基于this文章):
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
,则可以 运行 python3 click_select.py YOURIMAGE
.
单击鼠标左键标记矩形的第一个点,向上单击标记第二个点。释放鼠标按钮后,您应该会看到一个洋红色矩形(如果您正确安装了 cv2)。按 q
按钮将退出循环,将点列表打印到标准输出并保存注释图像。
示例图片:
示例输出:
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: