使用OpenCV在图片中画矩形框
2017-12-19 18:38
357 查看
一种使用OpenCV在图片中画矩形框的简单方法
import cv2
center=[]
circumference=[]
move=[]
def drawCircle(action, x, y, flags, userdata):
# Referencing global variables
global center, circumference,source,dummy
# Action to be taken when left mouse button is pressed
if action==cv2.EVENT_LBUTTONDOWN:
center=[(x,y)]
# Mark the center
cv2.circle(source, center[0], 1, (255,255,0), 2, cv2.LINE_AA );
# Action to be taken when left mouse button is released
elif action==cv2.EVENT_LBUTTONUP:
circumference=[(x,y)]
cv2.rectangle(source,center[0],circumference[0],(0,255,0),3)
elif action==cv2.EVENT_MOUSEMOVE and flags==cv2.EVENT_FLAG_LBUTTON:
move=[(x,y)]
source= dummy.copy()
cv2.rectangle(source,center[0],move[0],(0,255,0),3)
cv2.imshow("Window",source)
source = cv2.imread('D:/Projects/object_recognition/test/test_1.jpg')
# Make a dummy image, will be useful to clear the drawing
dummy = source.copy()
cv2.namedWindow("Window")
# highgui function called when mouse events occur
cv2.setMouseCallback("Window", drawCircle)
k = 0
# loop until escape character is pressed
while k!=27 :
cv2.imshow("Window", source)
cv2.putText(source,"Choose center, and drag, Press ESC to exit and c to clear" ,(10,30), cv2.FONT_HERSHEY_SIMPLEX, 0.7,(255,255,255), 2 );
k = cv2.waitKey(20) & 0xFF
# Another way of cloning
if k==99:
source= dummy.copy()
cv2.destroyAllWindows()
import cv2
center=[]
circumference=[]
move=[]
def drawCircle(action, x, y, flags, userdata):
# Referencing global variables
global center, circumference,source,dummy
# Action to be taken when left mouse button is pressed
if action==cv2.EVENT_LBUTTONDOWN:
center=[(x,y)]
# Mark the center
cv2.circle(source, center[0], 1, (255,255,0), 2, cv2.LINE_AA );
# Action to be taken when left mouse button is released
elif action==cv2.EVENT_LBUTTONUP:
circumference=[(x,y)]
cv2.rectangle(source,center[0],circumference[0],(0,255,0),3)
elif action==cv2.EVENT_MOUSEMOVE and flags==cv2.EVENT_FLAG_LBUTTON:
move=[(x,y)]
source= dummy.copy()
cv2.rectangle(source,center[0],move[0],(0,255,0),3)
cv2.imshow("Window",source)
source = cv2.imread('D:/Projects/object_recognition/test/test_1.jpg')
# Make a dummy image, will be useful to clear the drawing
dummy = source.copy()
cv2.namedWindow("Window")
# highgui function called when mouse events occur
cv2.setMouseCallback("Window", drawCircle)
k = 0
# loop until escape character is pressed
while k!=27 :
cv2.imshow("Window", source)
cv2.putText(source,"Choose center, and drag, Press ESC to exit and c to clear" ,(10,30), cv2.FONT_HERSHEY_SIMPLEX, 0.7,(255,255,255), 2 );
k = cv2.waitKey(20) & 0xFF
# Another way of cloning
if k==99:
source= dummy.copy()
cv2.destroyAllWindows()
相关文章推荐
- 使用opencv对图片进行边缘特征提取
- python使用opencv将灰度图人脸图片转化为RGB
- opencv imencode和imdecode使用,用于网络传输图片
- Erlang/Elixir: 使用 OpenCV, Python 搭建图片缩略图服务器
- opencv在图片指定区域内填充矩形,并在图片中显示矩形的轮廓
- OpenCV实践之路——矩形图片的对角线分割
- opengl离屏渲染(不需要和窗口绑定,仅当作一个可以渲染一张图片的API使用)+ opencv显示
- OpenCV1-读写图片,使用Matplotlib
- 在MFC对话框中显示图片的三种方法(有两种使用OpenCv)
- Android中使用Bitmap类将矩形图片转为圆形的方法
- [OpenCV开发]OpenCV图像编码和解码 imencode和imdecode使用,用于网络传输图片
- 使用Python和OpenCV通过网址URL获取图片
- unity 使用超椭圆方程对图片进行裁切制作圆角矩形
- 使用 Visual Studio 2008 和 OpenCV 在窗口显示图片或视频
- 使用OpenCV画矩形
- 【循序渐进地学好OpenCV&4】使用2.0的API显示图片以及OpenCV的自动化内存管理
- OpenCV2基础操作----直线、矩形、圆、椭圆函数的使用
- 转换排列Qt中使用OpenCV显示图片时,Mat结构转换为QImage结构的问题
- qt中使用opencv处理图片 QImage 和 IplImage 相互之间转换问题
- 使用opencv3 生成棋盘格图片源码