Opencv 鼠标画矩形

 此文转载自 http://www.geek-workshop.com/thread-1604-1-1.html

 网上大多数都把 一堆代码写在外部 void on_mouse (...) 里面。 但实际用到时写在里面会有很多局限性。

 我们只需要鼠标带来的点的坐标信息就可以。 

 所以可以用1个或多个CvPoint 把里面的鼠标点击信息提取出来就达到了这个函数的目的了。

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
 
//第一个点 point_c 是矩形起始左上角的定点
//第二个点 point 用来确立矩形大小宽和高
CvPoint point_c;
CvPoint point;
void on_mouse(int event, int x, int y, int flags, void* dr)
{
        //左键点击 按下
        if (event == CV_EVENT_LBUTTONDOWN)
        {
                point_c.x = x;
                point_c.y = y;
        }
        //左键拖拽 flags 
        if (flags == CV_EVENT_FLAG_LBUTTON)
        {
                point.x = x;
                point.y = y;
        }
}
 
int _tmain(int argc, _TCHAR* argv[])
{
 
        cvNamedWindow("Rectangle", 1);
 
        IplImage *img_rect = cvCreateImage(cvSize(400, 400), 8, 3);
 
        cvSetMouseCallback("Rectangle", on_mouse, 0);
 
        for (;;)
        {                
                cvZero(img_rect);
 
                if (point.x > 0 && point.y > 0)
                {
                        cvRectangle(img_rect,point_c,point,CV_RGB(0,0,255),2);
                }
 
 
                cvShowImage("Rectangle", img_rect);
 
                char c = cvWaitKey(1);
 
                if (c == 27)
                {
                        break;
                }
        }
 
        cvDestroyAllWindows();
        cvReleaseImage(&img_rect);
 
        return 0;
}

 

转载于:https://www.cnblogs.com/Rachel-Liu/archive/2013/03/15/2962374.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenCV是一个开源的计算机视觉库。它提供了许多处理数字图像和视频的函数,而其中之一就是矩形ROI截图。 首先,要创建一个矩形ROI,需要知道需要截取的区域的左上角和右下角的坐标。可以使用OpenCV自带的鼠标事件函数,在图像上拖动鼠标选择需要截取的区域并获取该区域的坐标。 其次,使用cv2.rectangle函数将矩形绘制在图像上。函数参数包括:原始图像,矩形左上角坐标,矩形右下角坐标,颜色和线宽度。 最后,使用numpy的切片操作截取原始图像中的ROI区域。切片的参数是左上角和右下角坐标,保持一致性。 示例程序如下: ```python import cv2 import numpy as np # Mouse callback function def draw_rect(event, x, y, flags, params): global x_init, y_init, drawing, top_left_pt, bottom_right_pt if event == cv2.EVENT_LBUTTONDOWN: drawing = True x_init, y_init = x, y elif event == cv2.EVENT_MOUSEMOVE: if drawing: top_left_pt = (min(x_init, x), min(y_init, y)) bottom_right_pt = (max(x_init, x), max(y_init, y)) img[y_init:y, x_init:x] = 255 - img[y_init:y, x_init:x] elif event == cv2.EVENT_LBUTTONUP: drawing = False top_left_pt = (min(x_init, x), min(y_init, y)) bottom_right_pt = (max(x_init, x), max(y_init, y)) img[y_init:y, x_init:x] = 255 - img[y_init:y, x_init:x] # MAIN SCRIPT drawing = False top_left_pt, bottom_right_pt = (-1, -1), (-1, -1) # Open image img = cv2.imread('test.png') copy = img.copy() # Create a window cv2.namedWindow("Window") # Set the mouse callback function to "draw_rect" cv2.setMouseCallback("Window", draw_rect) while True: cv2.imshow("Window", img) k = cv2.waitKey(1) & 0xFF # If "c" is pressed, reset the window if k == ord('c'): img = copy.copy() # If "r" is pressed, crop the ROI elif k == ord('r'): if top_left_pt != (-1, -1) and bottom_right_pt != (-1, -1): roi = copy[top_left_pt[1]:bottom_right_pt[1], top_left_pt[0]:bottom_right_pt[0]] cv2.imshow("ROI", roi) # If "q" is pressed, exit elif k == ord('q'): break cv2.destroyAllWindows() ``` 该程序允许用户在图像上拖动鼠标选择需要截取的区域。在选择完区域后,按下“r”键就可以截取该区域并显示在一个新窗口中。按下“c”键可以重置窗口。如果想要结束程序,按下“q”键即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值