C++ opencv 图像 扭曲旋转 双线性插值 双三次插值 和 畸变矫正 双线性插值 实现

程序下载链接:程序下载

老规矩,先上代码:

#include "opencv2/flann.hpp"
#include "opencv2/opencv_modules.hpp"
#include "opencv2/stitching/detail/matchers.hpp"
#include "opencv2/imgcodecs/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include <opencv2\opencv.hpp>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\objdetect\objdetect.hpp>
#include <opencv2\imgproc\types_c.h>
#include <opencv2\objdetect\objdetect_c.h>

#include <stdlib.h>
#include <math.h>
#include <vector>
using namespace cv;
using namespace std;
#define PI 3.1415926535
#define RADIAN(angle) ((angle)*PI/180.0)

#define INVALDE_DEM_VALUE  -9999999

double amax, Radius;


double GetDistence(double x, double y, double xm, double ym)
{
   
	return sqrt(pow((x - xm), 2) + pow((y - ym), 2));
}

double cubic_coeff(double x) {
   
	x = (x > 0) ? x : -x;
	if (x < 1) {
   
		return 1 - 2 * x*x + x * x*x;
	}
	else if (x < 2) {
   
		return 4 - 8 * x + 5 * x*x - x * x*x;
	}
	return 0;
}

Mat changeShapeResampleBilinear(const Mat &src) {
   
	Mat imgAffine = Mat::zeros(src.rows, src.cols, src.type());
	int row = imgAffine.rows, col = imgAffine.cols;
	double xm = src.rows / 2.0;
	double ym = src.cols / 2.0;
	
	
	for (int x = 0; x < row; x++) {
   
		for (int y = 0; y < col; y++) {
   

			double X = x / ((row - 1) / 2.0) - 1.0;
			double Y = y / ((col - 1) / 2.0) - 1.0;
			double r = sqrt(X * X + Y * Y);

			if (r >= Radius/(row/2)) {
   
				imgAffine.at<Vec3b>(x, y)[0] = saturate_cast<uchar>(src.at<Vec3b>(x, y)[0]);
				imgAffine.at<Vec3b>(x, y)[1] = saturate_cast<uchar>(src.at<Vec3b>(x, y)[1]);
				imgAffine.at<Vec3b>(x, y)[2] = saturate_cast<uchar>(src.at<Vec3b>(x, y)[2]);
			}
			else {
   

				double theta = RADIAN(amax)*(Radius - GetDistence(x, y, xm, ym)) / Radius;

				//double theta = 1.0 + X * X + Y * Y - 2.0*sqrt(X * X + Y * Y);//修改不用(1-r)*(1-r)
				double x_ = cos(theta)*X - sin(theta)*Y;
				double y_ = sin(theta)*X + cos(theta)*Y;

				x_ = (x_ + 1.0)*((row - 1) / 2.0);
				y_ = (y_ + 1.0)*((col - 1) / 2.0);


				if (x_ < 0 || y_ < 0 || x_ >= src.rows || y_ >= src.cols) {
   
					for (int c = 0; c < 3; c++) {
   
						imgAffine.at<Vec3b>(x, y)[c] = saturate_cast<uchar>(0);
					}
				}
				else {
   
					//左上角坐标(X1,Y1)
					//计算双线性插值   
					int X1 = (int)x_;
					int Y1 = (int)y_;

					for (int c = 0; c < 3; c++) {
   
						if (X1 == (src.rows - 1) || Y1 == (src.cols - 1)) {
   
							imgAffine.at<Vec3b>(x, y)[c] = saturate_cast<uchar>(src.at<Vec3b>(X1, Y1)[c]);
						}
						else {
   
							//四个顶点像素值
							//注意访问越界
							int aa = src.at<Vec3b>(X1, Y1)[c];
							int bb = src.at<Vec3b>(X1, Y1 + 1)[c];
							int cc = src.at<Vec3b>(X1 + 1, Y1)[c];
							int dd = src.at<Vec3b>(X1 + 1, Y1 + 1)[c];

							double dx = x_ - (double)X1;
							double dy = y_ - (double)Y1;
							double h1 = aa + dx * (bb - aa)
  • 8
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值