about
本文参考了:
- GarfieldEr007同学的Python-OpenCV �杂项(二)(三): 鼠标事件、 程序性能的检测和优化
- DoubleLi同学的opencv2 使用鼠标绘制矩形并截取和保存矩形区域图像
- jihite同学的python实现 全局变量的两种解决办法
不用鼠标交互,手动设参
先来个热身运动的先,直接手动设参选择。一个要点就是把一个RGB图片看成一个3维的数组。
import cv2
def main():
org = cv2.imread('lena.jpg')
cv2.imshow('image', org) #origin picture
cv2.waitKey(0)
roi = org[20:, 100:]
cv2.imshow('roi', roi) #region of interesting
cv2.waitKey(0)
if __name__ == '__main__':
main()
- 运行结果
Origin图片
roi图片
之前网上找的话,有什么说用cv2.cv的方法
>>>import cv2.cv as cv
error: No module named 'cv'
- 导入失败原因,有网友说:
OpenCV 3.0.0 does not have the cv2.cv module anymore.
我安装了OpenCV 3.1.0的,所以导入失败了。
- 坐标,numpy数组是采用左上角为坐标原点的方法:访问方法
img[point_y: point_y+height, point_x: point_x+width]
预备知识
- OpenCV画矩形
实例点这里
用到函数:
cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) → img
- 简单参数说明,详见官网:
img – Image.
pt1 – Vertex of the rectangle.
pt2 – Vertex of the rectangle opposite to pt1.
color – Rectangle color or brightness (grayscale image).
thickness – Thickness of lines that make up the rectangle.
- OpenCV画圆形
实例点这里
用到函数
cv2.circle(img, center, radius, color[, thickness[, lineType[, shift]]]) →img
- 详见官网, 简单参数说明:
img – Image where the circle is drawn.
center – Center of the circle.
radius – Radius of the circle.
color – Circle color.
thickness – Thickness of the circle outline, if positive. Negative thickness means that a filled circle is to be drawn.
- OpenCV 复制图像
实例点 这里
用到函数
img2 = img.copy()
- OpenCV画文本
用到函数
cv2.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]]) → None
详见官网, 参数简要说明:
img – Image.
text – Text string to be drawn.
org – Bottom-left corner of the text string in the image.
fontScale – Font scale factor that is multiplied by the font-specific base size.
color – Text color.
thickness – Thickness of the lines used to draw a text.
鼠标交互切割矩形
接下来,就是本文重点了。先吐个槽,网上有资源,但搜到的都是C++的。本来有点气馁的,还好,有官网在,文档写得很清楚,而且接口函数名字变化不大,稍微做下修改就行了。
- 代码实现
import cv2
global img
global point1, point2
def on_mouse(event, x, y, flags, param):
global img, point1, point2
img2 = img.copy()
if event == cv2.EVENT_LBUTTONDOWN: #左键点击
point1 = (x,y)
cv2.circle(img2, point1, 10, (0,255,0), 5)
cv2.imshow('image', img2)
elif event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_LBUTTON): #按住左键拖曳
cv2.rectangle(img2, point1, (x,y), (255,0,0), 5)
cv2.imshow('image', img2)
elif event == cv2.EVENT_LBUTTONUP: #左键释放
point2 = (x,y)
cv2.rectangle(img2, point1, point2, (0,0,255), 5)
cv2.imshow('image', img2)
min_x = min(point1[0],point2[0])
min_y = min(point1[1],point2[1])
width = abs(point1[0] - point2[0])
height = abs(point1[1] -point2[1])
cut_img = img[min_y:min_y+height, min_x:min_x+width]
cv2.imwrite('lena3.jpg', cut_img)
def main():
global img
img = cv2.imread('lena.jpg')
cv2.namedWindow('image')
cv2.setMouseCallback('image', on_mouse)
cv2.imshow('image', img)
cv2.waitKey(0)
if __name__ == '__main__':
main()
实现效果:当按住左键不放且不动时,就会出现个绿圈在图片上,当按住左键拖曳鼠标时,就会画出个蓝色矩形,最后当释放鼠标时,则出现个红色的选定框.最后最后再随意按一个键盘键,就会结束程序。
运行结果
原图:
运行交互:
结果截图:
鼠标响应参数:
- event
define CV_EVENT_MOUSEMOVE 0 滑动
define CV_EVENT_LBUTTONDOWN 1 击键点击
define CV_EVENT_RBUTTONDOWN 2 右键点击
define CV_EVENT_MBUTTONDOWN 3 中间点击
define CV_EVENT_LBUTTONUP 4 左键释放
define CV_EVENT_RBUTTONUP 5 右键释放
define CV_EVENT_MBUTTONUP 6 中间释放
define CV_EVENT_LBUTTONDBLCLK 7 左键双击
define CV_EVENT_RBUTTONDBLCLK 8 右键双击
define CV_EVENT_MBUTTONDBLCLK 9 中间释放
- flags
define CV_EVENT_FLAG_LBUTTON 1 左键拖拽
define CV_EVENT_FLAG_RBUTTON 2 右键拖拽
define CV_EVENT_FLAG_MBUTTON 4 中间拖拽
define CV_EVENT_FLAG_CTRLKEY 8 (8~15)按Ctrl不放事件
define CV_EVENT_FLAG_SHIFTKEY 16 (16~31)按Shift不放事件
define CV_EVENT_FLAG_ALTKEY 32 (32~39)按Alt不放事件(后面8-39还有待研究)