python+OpenCV⿏标交互图⽚切割矩形区域about
本⽂参考了:
GarfieldEr007同学的
DoubleLi同学的
jihite同学的
不⽤⿏标交互,⼿动设参
先来个热⾝运动的先,直接⼿动设参选择。⼀个要点就是把⼀个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图⽚
lena.jpg
roi图⽚
roi.jpg
之前⽹上的话,有什么说⽤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画矩形
实例点
⽤到函数:
简单参数说明,详见:
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 = py()
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 = py()
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.imshow('image', img2)
elif event == cv2.EVENT_LBUTTONUP:        #左键释放
point2 = (x,y)
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()
实现效果:当按住左键不放且不动时,就会出现个绿圈在图⽚上,当按住左键拖曳⿏标时,就会画出
个蓝⾊矩形,最后当释放⿏标时,则出现个红⾊的选定框.最后最后再随意按⼀个键盘键,就会结束程序。
运⾏结果
原图:
lena.jpg
运⾏交互:
lena_mouse
结果截图:
lena_cut
⿏标响应参数:
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 中间拖拽rectangle函数opencv
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还有待研究)