极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 22907|回复: 2

图像上画矩形~ 先来个基础篇~ 了解真正自己想要的。。你懂得~ 题目要长~ =。=

[复制链接]
发表于 2012-8-7 10:33:14 | 显示全部楼层 |阅读模式
网上大多数都把 一堆代码写在外部 void on_mouse (...) 里面。 但实际用到时写在里面会有很多局限性。

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

所以可以用1个或多个CvPoint 把里面的鼠标点击信息提取出来就达到了这个函数的目的了。[pre lang="cpp" line="1" file="DrawRect"]#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;
}[/code]
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-23 22:06 , Processed in 0.040455 second(s), 17 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表