实现不均匀的图像拉伸

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值