这里所谓画点
的意思是指在单一像素点上画一个标记符,而不是画小圆点。使用的函数是cv2.drawMarker(img, position, color, ...)
关于鼠标回调函数的说明可以参考:opencv-python的鼠标交互操作
导入cv2后,通过help(cv2.drawMarker)可以看到函数的帮助文档如下:
drawMarker(...)
drawMarker(img, position, color[, markerType[, markerSize[, thickness[, line_type]]]]) -> img
. @brief Draws a marker on a predefined position in an image.
.
. The function cv::drawMarker draws a marker on a given position in the image. For the moment several
. marker types are supported, see #MarkerTypes for more information.
.
. @param img Image.
. @param position The point where the crosshair is positioned.
. @param color Line color.
. @param markerType The specific type of marker you want to use, see #MarkerTypes
. @param thickness Line thickness.
. @param line_type Type of the line, See #LineTypes
. @param markerSize The length of the marker axis [default = 20 pixels]
其中三个必选参数:img, position, color,其他参数是可选。三个必选参数说明如下:
(x, y)
(b, g, r)
其他参数说明如下:
cv2.LINE_4
,cv2.LINE_8
,cv2.LINE_AA
。其中cv2.LINE_AA
的AA表示抗锯齿,线会更平滑。markerType取值说明
数值 | 宏定义 | 说明 |
---|---|---|
0 | cv2.MARKER_CROSS | 十字线(横竖两根线) |
1 | cv2.MARKER_TILTED_CROSS | 交叉线(斜着两根线) |
2 | cv2.MARKER_STAR | 米字线(横竖加斜着共四根线) |
3 | cv2.MARKER_DIAMOND | 旋转45度的正方形 |
4 | cv2.MARKER_SQUARE | 正方形 |
5 | cv2.MARKER_TRIANGLE_UP | 尖角向上的三角形 |
6 | cv2.MARKER_TRIANGLE_DOWN | 尖角向下的三角形 |
下面是一个简单的画点程序
# -*- coding: utf-8 -*-
import cv2
import numpy as np
if __name__ == '__main__':
image = np.zeros((256, 256, 3), np.uint8)
color = (0, 255, 0)
cv2.drawMarker(image, (50, 50), color, markerType=0)
cv2.drawMarker(image, (100, 50), color, markerType=1)
cv2.drawMarker(image, (150, 50), color, markerType=2)
cv2.drawMarker(image, (200, 50), color, markerType=3)
cv2.drawMarker(image, (50, 100), color, markerType=4)
cv2.drawMarker(image, (100, 100), color, markerType=5)
cv2.drawMarker(image, (150, 100), color, markerType=6)
cv2.namedWindow('marker_type', 1)
cv2.imshow('marker_type', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
请特别注意,opencv在调用这些画图函数后,image的内容会被这些画图函数改变,也就是说,函数调用之后,我们就拿不回原始的image了,除非另外保存一份原始image的副本。在写一些交互画图函数时,这个特性需要格外注意。
该例子与opencv-python的鼠标交互操作中的例子相同
# -*- coding: utf-8 -*-
import cv2
import numpy as np
WIN_NAME = 'pick_points'
def onmouse_pick_points(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
print('x = %d, y = %d' % (x, y))
cv2.drawMarker(param, (x, y), (0, 255, 0))
if __name__ == '__main__':
image = np.zeros((256, 256, 3), np.uint8)
cv2.namedWindow(WIN_NAME, 0)
cv2.setMouseCallback(WIN_NAME, onmouse_pick_points, image)
while True:
cv2.imshow(WIN_NAME, image)
key = cv2.waitKey(30)
if key == 27: # ESC
break
cv2.destroyAllWindows()
上面程序中有几个注意点:
这里点了三次左键,终端输出以下内容:
x = 60, y = 55
x = 206, y = 113
x = 114, y = 192
如果需要删除
已经画了的点的功能,那么问题就变得略有些复杂了。
我们之前讲过,opencv在画了这些点之后,图像的像素已经事实上被改变了,想要紧紧通过当前图像将其恢复原状是不行的。所以为了实现删除功能,我们需要备份一张原始图像,一张用来对外显示的图像,以及一个由点坐标组成的list。
每次做删除点的操作后,我们都使用原始图像重置对外显示的图像,然后再把list中所有的点都重新画在对外显示的图像上,就可以实现删除点的效果。如果是增加点的操作,则不用重置图像。
实现代码如下:
下面代码中,左键
实现增加
一个点的操作,右键
依次删除
后面画上的点。
# -*- coding: utf-8 -*-
import cv2
import numpy as np
WIN_NAME = 'pick_points'
class DrawPoints(object):
def __init__(self, image, color,
marker_type=cv2.MARKER_CROSS,
marker_size=20,
thickness=1):
"""
Initialization of class DrawPoints
Parameters
----------
image: ndarray
source image. shape is [height, width, channels]
color: tuple
a tuple containing uint8 integers, designating B, G, R values,
separately
marker_type: int
marker type, between [0, 6]
marker_size: int
marker size, >=1
thickness: int
line thickness, >=1
"""
self.original_image = image
self.image_for_show = image.copy()
self.color = color
self.marker_type = marker_type
self.marker_size = marker_size
self.thickness = thickness
self.pts = []
def append(self, x, y):
"""
add a point to points list
Parameters
----------
x, y: int, int
coordinate of a point
"""
self.pts.append((x, y))
def pop(self):
"""
pop a point from points list
"""
pt = ()
if self.pts:
pt = self.pts.pop()
return pt
def reset_image(self):
"""
reset image_for_show using original image
"""
self.image_for_show = self.original_image.copy()
def draw(self):
"""
draw points on image_for_show
"""
for pt in self.pts:
cv2.drawMarker(self.image_for_show, pt, color=self.color,
markerType=self.marker_type,
markerSize=self.marker_size,
thickness=self.thickness)
def onmouse_pick_points(event, x, y, flags, draw_pts):
if event == cv2.EVENT_LBUTTONDOWN:
print('add: x = %d, y = %d' % (x, y))
draw_pts.append(x, y)
draw_pts.draw()
elif event == cv2.EVENT_RBUTTONDOWN:
pt = draw_pts.pop()
if pt:
print('delete: x = %d, y = %d' % (pt[0], pt[1]))
draw_pts.reset_image()
draw_pts.draw()
if __name__ == '__main__':
image = np.zeros((256, 256, 3), np.uint8)
draw_pts = DrawPoints(image, (0, 255, 0))
cv2.namedWindow(WIN_NAME, 0)
cv2.setMouseCallback(WIN_NAME, onmouse_pick_points, draw_pts)
while True:
cv2.imshow(WIN_NAME, draw_pts.image_for_show)
key = cv2.waitKey(30)
if key == 27: # ESC
break
cv2.destroyAllWindows()
终端输出如下:
add: x = 54, y = 51
add: x = 215, y = 81
add: x = 123, y = 121
add: x = 57, y = 197
add: x = 168, y = 210
delete: x = 168, y = 210
delete: x = 57, y = 197