#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
#define K 2 //表示取2个点
#define threshold 0.1 //缩放的程度
// Global variables
Mat src, src_copy, dst;
Mat map_x, map_y;
char* window = "Scale demo";
int samplePointNum = 0; //已经点了几次
Point myPoints[K]; //存放用户点击的坐标
bool flag = false; //点击2次,就设置flag,开始更新
void update_map( void )
{
int leftX = threshold*src.cols;
int rightX = src.cols - leftX;
int recLeftX = myPoints[0].x;
int recRightX = myPoints[1].x;
for( int j = 0; j < src.rows; j++ ) {
for( int i = 0; i < src.cols; i++ ) {
if(i > leftX && i < rightX) {
map_x.at<float>(j,i) = recLeftX + (i - leftX) * (recRightX - recLeftX)/(rightX - leftX);
map_y.at<float>(j,i) = j;
} else if(i <= leftX) {
map_x.at<float>(j,i) = i * recLeftX/leftX;
map_y.at<float>(j,i) = j;
} else {
map_x.at<float>(j,i) = recRightX + (i- rightX) * (src.cols - recRightX)/(src.cols - rightX);
map_y.at<float>(j,i) = j;
}
}
}
flag = true;
remap( src_copy, dst, map_x, map_y, INTER_LANCZOS4, BORDER_CONSTANT, Scalar(0, 0, 0) );
imshow( window, dst );
imwrite( "./result.jpg", dst );
return;
}
static void onMouse( int event, int x, int y, int, void* )
{
if(samplePointNum == K){
if(!flag)
update_map();
return;
}
if( event != EVENT_LBUTTONDOWN)
return;
rectangle(src, Point(x-3,y-3), Point(x+3,y+3), Scalar(255,0,0), 1);
myPoints[samplePointNum++] = Point(x,y);
imshow( window, src );
return;
}
int main( ) {
src = imread( "C:\\Users\\DELL\\Desktop\\1.jpg" );
src_copy = src.clone();
dst.create( src.rows, src.cols, src.type() );
map_x.create( src.rows, src.cols, CV_32FC1 );
map_y.create( src.rows, src.cols, CV_32FC1 );
namedWindow( window, CV_WINDOW_AUTOSIZE );
setMouseCallback( window, onMouse, 0 );
imshow( window, src );
waitKey(0);
return 0;
}
原:https://blog.csdn.net/abcd1992719g/article/details/25910211