亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

VS+opencv實(shí)現(xiàn)鼠標(biāo)移動(dòng)圖片

 更新時(shí)間:2019年01月18日 08:35:07   作者:fanyan008  
這篇文章主要為大家詳細(xì)介紹了VS+opencv實(shí)現(xiàn)鼠標(biāo)移動(dòng)圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

基于控制臺(tái)應(yīng)用程序+opencv,實(shí)現(xiàn)點(diǎn)擊鼠標(biāo)左鍵,可以拖動(dòng)圖片以顯示感興趣區(qū)域

#include <opencv2/highgui/highgui.hpp>

//#include <iostream> 
//using namespace std; 
using namespace cv;

int win_width=1400,win_height=700;
Mat image,win_image;       //申明全局變量
Rect rect_win,rect_img;

void moveImage()//實(shí)現(xiàn)移動(dòng)圖像
{ 
 Mat image_ROI=image(rect_img);  // 定義源圖像感興趣區(qū)域ROI(需要顯示的區(qū)域)
 image_ROI.convertTo(win_image,image_ROI.type());  // image_ROI 復(fù)制到 win_image
 ////也可以直接用 Mat win_image=image(rect_img); //但是很卡 Why?
 imshow("拼接結(jié)果",win_image);
}

void on_mouse( int event, int x, int y, int flags, void* ustc) //int x,int y,代表鼠標(biāo)位于窗口的(x,y)坐標(biāo)位置,窗口左上角默認(rèn)為原點(diǎn),向右為x軸,向下為y軸
{
  // static聲明靜態(tài)局部變量,值在函數(shù)調(diào)用結(jié)束后不消失而保留原值,
  //即其占用的存儲(chǔ)單元不釋放,在下次該函數(shù)調(diào)用時(shí),該變量保留上一次函數(shù)調(diào)用結(jié)束時(shí)的值
  static Point p0; 
  static int xrect_img;  //左鍵按下時(shí),窗口顯示圖像左上角在源圖像中x、y
  static int yrect_img;
 if(event==CV_EVENT_LBUTTONDOWN)
 {
  p0=Point(x,y); //獲取鼠標(biāo)左鍵按下時(shí)的起始點(diǎn)
   xrect_img=rect_img.x;
   yrect_img=rect_img.y;
 }
 if(event==CV_EVENT_MOUSEMOVE&& (flags & CV_EVENT_LBUTTONDOWN)) //左鍵按下,鼠標(biāo)移動(dòng)時(shí)
 {  
   int dx=x-p0.x;
   int dy=y-p0.y;  
   if(x>=0 && x<=win_width-1 && y>=0 && y<=win_height-1) //判斷鼠標(biāo)是否在窗口圖像區(qū)域內(nèi)
   {   
    rect_img=Rect(xrect_img-dx,yrect_img-dy,rect_img.width,rect_img.height);  //窗口顯示圖像移動(dòng)dx、dy(相對(duì)于鼠標(biāo)左鍵按下時(shí))
    if(rect_img.x<0) 
    { 
     rect_img.x=0; 
    } 
    if(rect_img.y<0) 
    { 
     rect_img.y=0; 
    }   
    if(rect_img.x > image.cols-rect_img.width-1) 
    { 
     rect_img.x=image.cols-rect_img.width-1; 
    } 
    if(rect_img.y > image.rows - rect_img.height-1) 
    { 
     rect_img.y=image.rows - rect_img.height-1; 
    } 

    moveImage();
   }  
 } 
}

void main()
{  
 image=imread("im.jpg");
 //int win_width=1400,win_height=700;     //固定窗口的大小1400 x 700
 //rect_win=Rect(0,0,win_width,win_height);   //窗口顯示矩形區(qū)
 rect_img=Rect(0,0,win_width,win_height);   //窗口圖像對(duì)應(yīng)的矩形區(qū)
 //win_image.create(win_height,win_width,image.type());
 //Mat tmp=image(rect_win);  // 窗口圖像對(duì)應(yīng)于源圖像中的區(qū)域
 //tmp.convertTo(win_image,tmp.type());  //復(fù)制一個(gè)圖像的ROI到另外一個(gè)圖像的指定區(qū)域
 Mat win_image=image(rect_img);
 namedWindow("拼接結(jié)果", 1);
 imshow("拼接結(jié)果",win_image);

 setMouseCallback("拼接結(jié)果", on_mouse); 
 waitKey();
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論