VS+opencv 마우스 이동 그림 구현
#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()//
{
Mat image_ROI=image(rect_img); // ROI( )
image_ROI.convertTo(win_image,image_ROI.type()); // image_ROI win_image
//// Mat win_image=image(rect_img); // Why?
imshow(" ",win_image);
}
void on_mouse( int event, int x, int y, int flags, void* ustc) //int x,int y, (x,y) , , x , y
{
// static , ,
// , ,
static Point p0;
static int xrect_img; // , x、y
static int yrect_img;
if(event==CV_EVENT_LBUTTONDOWN)
{
p0=Point(x,y); //
xrect_img=rect_img.x;
yrect_img=rect_img.y;
}
if(event==CV_EVENT_MOUSEMOVE&& (flags & CV_EVENT_LBUTTONDOWN)) // ,
{
int dx=x-p0.x;
int dy=y-p0.y;
if(x>=0 && x<=win_width-1 && y>=0 && y<=win_height-1) //
{
rect_img=Rect(xrect_img-dx,yrect_img-dy,rect_img.width,rect_img.height); // dx、dy( )
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); //
rect_img=Rect(0,0,win_width,win_height); //
//win_image.create(win_height,win_width,image.type());
//Mat tmp=image(rect_win); //
//tmp.convertTo(win_image,tmp.type()); // ROI
Mat win_image=image(rect_img);
namedWindow(" ", 1);
imshow(" ",win_image);
setMouseCallback(" ", on_mouse);
waitKey();
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
OpenCV의 waitKey()는 0xFFOpenCV를 사용하는 사람은 다음과 같은 작업을 수행할 수 있습니다. 大部分的教學都告訴你要在叫用 0xFF 0&運算, 把高位元內容清掉, 只留下最低一個位元組的按鍵碼.這是因為過去 waitKey()傳回值中除了最低位元組的 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.