程序下载链接:程序下载
老规矩,先上代码:
#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
#include
#include
#include
#include
#include
#include
#include
#include
#include
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);
double h2 = cc + dx * (dd - cc);
imgAffine.at<Vec3b>(x, y)[c] = saturate_cast<uchar>(h1 + dy * (h2 - h1));
}
}
}
}
}
}
return imgAffine;
}
/**
* @brief 三次卷积
*
* @param x 插值点列坐标
* @param y 插值点行坐标
* @param pdfValue 影像数据
* @param nWidth 影像数据宽度
* @param nHeight 影像数据高度
*/
double ResampleCubic(double x, double y, double * pdfValue, int nWidth, int nHeight)
{
double dfCubicValue;
int i = x;
int j = y;
/*we do not handle the border, attention*/
if ((i - 1) < 0 || (j - 1) < 0 || (j + 2) > (nHeight - 1) || (i + 2) > (nWidth - 1))
return INVALDE_DEM_VALUE;
/*get adjacent 16 values*/
double values[4][4];
for (int r = j - 1, s = 0; r <= j + 2; r++, s++) {
for (int c = i - 1, t = 0; c <= i + 2; c++, t++) {
values[s][t] = pdfValue[r*nWidth + c];
}
}
/*calc the coeff*/
double u = x - i;
double v = y - j;
double A[4], C[4];
for (int distance = 1, s = 0; distance >= -2; distance--, s++) {
A[s] = cubic_coeff(u + distance);
C[s] = cubic_coeff(v + distance);
}
dfCubicValue = 0;
for (int s = 0; s < 4; s++) {
for (int t = 0; t < 4; t++) {
dfCubicValue += values[s][t] * A[t] * C[s];
}
}
return dfCubicValue;
}
float BiCubicPloy(float x)
{
float abs_x = abs(x);//取x的绝对值
float a = -0.5;
if (abs_x <= 1.0)
{
return (a + 2)*pow(abs_x, 3) - (a + 3)*pow(abs_x, 2) + 1;
}
else if (abs_x < 2.0)
{
return a * pow(abs_x, 3) - 5 * a*pow(abs_x, 2) + 8 * a*abs_x - 4 * a;
}
else
return 0.0;
}
Mat changeShapeResampleCubic(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;
Mat g = src;
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_;
//获取目标图像(i,j)在原图中的坐标
int xm = X1 ;
int ym = Y1 ;
//取出映射到原图的整数部分
int xi = (int)xm;
int yi = (int)ym;
//取出映射到原图中的点的四周的16个点的坐标
int x0 = xi - 1;
int y0 = yi - 1;
int x1 = xi;
int y1 = yi;
int x2 = xi + 1;
int y2 = yi + 1;
int x3 = xi + 2;
int y3 = yi + 2;
if ((x0 >= 0) && (x3 < src.rows) && (y0 >= 0) && (y3 < src.cols))
{
//求出行和列所对应的系数
float dist_x0 = BiCubicPloy(xm - x0);
float dist_x1 = BiCubicPloy(xm - x1);
float dist_x2 = BiCubicPloy(xm - x2);
float dist_x3 = BiCubicPloy(xm - x3);
float dist_y0 = BiCubicPloy(ym - y0);
float dist_y1 = BiCubicPloy(ym - y1);
float dist_y2 = BiCubicPloy(ym - y2);
float dist_y3 = BiCubicPloy(ym - y3);
//k_i_j=k_i*k_j
float dist_x0y0 = dist_x0 * dist_y0;
float dist_x0y1 = dist_x0 * dist_y1;
float dist_x0y2 = dist_x0 * dist_y2;
float dist_x0y3 = dist_x0 * dist_y3;
float dist_x1y0 = dist_x1 * dist_y0;
float dist_x1y1 = dist_x1 * dist_y1;
float dist_x1y2 = dist_x1 * dist_y2;
float dist_x1y3 = dist_x1 * dist_y3;
float dist_x2y0 = dist_x2 * dist_y0;
float dist_x2y1 = dist_x2 * dist_y1;
float dist_x2y2 = dist_x2 * dist_y2;
float dist_x2y3 = dist_x2 * dist_y3;
float dist_x3y0 = dist_x3 * dist_y0;
float dist_x3y1 = dist_x3 * dist_y1;
float dist_x3y2 = dist_x3 * dist_y2;
float dist_x3y3 = dist_x3 * dist_y3;
imgAffine.at<Vec3b>(x, y) = (src.at<Vec3b>(x0, y0)*dist_x0y0 +
src.at<Vec3b>(x0, y1)*dist_x0y1 +
src.at<Vec3b>(x0, y2)*dist_x0y2 +
src.at<Vec3b>(x0, y3)*dist_x0y3 +
src.at<Vec3b>(x1, y0)*dist_x1y0 +
src.at<Vec3b>(x1, y1)*dist_x1y1 +
src.at<Vec3b>(x1, y2)*dist_x1y2 +
src.at<Vec3b>(x1, y3)*dist_x1y3 +
src.at<Vec3b>(x2, y0)*dist_x2y0 +
src.at<Vec3b>(x2, y1)*dist_x2y1 +
src.at<Vec3b>(x2, y2)*dist_x2y2 +
src.at<Vec3b>(x2, y3)*dist_x2y3 +
src.at<Vec3b>(x3, y0)*dist_x3y0 +
src.at<Vec3b>(x3, y1)*dist_x3y1 +
src.at<Vec3b>(x3, y2)*dist_x3y2 +
src.at<Vec3b>(x3, y3)*dist_x3y3);
}
}
}
}
}
return imgAffine;
}
void main() {
cout << "输入最大旋转角度" << endl;
cin >> amax;
cout << "输入扭曲旋转半径" << endl;
cin >> Radius;
const char *filename = "D:\\THU.jpg";//图像路径
Mat pano = cv::imread(filename);
namedWindow("pano", 1);
imshow("pano", pano);
Mat bilShow;
bilShow = pano;
bilShow = changeShapeResampleBilinear(bilShow);
namedWindow("bil", 1);
imshow("bil", bilShow);
Mat cubShow;
cubShow = pano;
cubShow = changeShapeResampleCubic(cubShow);
namedWindow("cub", 1);
imshow("cub", cubShow);
long time = clock();
Mat img = imread(filename);
Mat drcimg(img.rows, img.cols, CV_8UC3);
cv::imshow("矫正前", img);
CvPoint lenscenter(img.cols / 2, img.rows / 2);//镜头中心在图像中的位置
CvPoint src_a, src_b, src_c, src_d;//a、b、c、d四个顶点
//矫正参数
double r;//矫正前像素点跟镜头中心的距离
double s;//矫正后像素点跟镜头中心的距离
CvPoint2D32f mCorrectPoint;//矫正后点坐标
double distance_to_a_x, distance_to_a_y;//求得中心点和边界的距离
for (int row = 0; row < img.rows; row++)//操作数据区,要注意OpenCV的RGB的存储顺序为GBR
for (int cols = 0; cols < img.cols; cols++)//示例为亮度调节
{
r = sqrt((row - lenscenter.y)*(row - lenscenter.y) + (cols - lenscenter.x)*(cols - lenscenter.x));
s = 0.9998 - 4.2932*pow(10, -4)*r + 3.4327*pow(10, -6)*pow(r, 2) - 2.8526*pow(10, -9)*pow(r, 3) + 9.8223*pow(10, -13)*pow(r, 4);//比例
mCorrectPoint = cvPoint2D32f((cols - lenscenter.x) / s * 1.35 + lenscenter.x, (row - lenscenter.y) / s * 1.35 + lenscenter.y);
//越界判断
if (mCorrectPoint.y < 0 || mCorrectPoint.y >= img.rows - 1)
{
continue;
}
if (mCorrectPoint.x < 0 || mCorrectPoint.x >= img.cols - 1)
{
continue;
}
src_a = cvPoint((int)mCorrectPoint.x, (int)mCorrectPoint.y);
src_b = cvPoint(src_a.x + 1, src_a.y);
src_c = cvPoint(src_a.x, src_a.y + 1);
src_d = cvPoint(src_a.x + 1, src_a.y + 1);
distance_to_a_x = mCorrectPoint.x - src_a.x;//在原图像中与a点的水平距离
distance_to_a_y = mCorrectPoint.y - src_a.y;//在原图像中与a点的垂直距离
drcimg.at<Vec3b>(row, cols)[0] =
img.at<Vec3b>(src_a.y, src_a.x)[0] * (1 - distance_to_a_x)*(1 - distance_to_a_y) +
img.at<Vec3b>(src_b.y, src_b.x)[0] * distance_to_a_x*(1 - distance_to_a_y) +
img.at<Vec3b>(src_c.y, src_c.x)[0] * distance_to_a_y*(1 - distance_to_a_x) +
img.at<Vec3b>(src_d.y, src_d.x)[0] * distance_to_a_y*distance_to_a_x;
drcimg.at<Vec3b>(row, cols)[1] =
img.at<Vec3b>(src_a.y, src_a.x)[1] * (1 - distance_to_a_x)*(1 - distance_to_a_y) +
img.at<Vec3b>(src_b.y, src_b.x)[1] * distance_to_a_x*(1 - distance_to_a_y) +
img.at<Vec3b>(src_c.y, src_c.x)[1] * distance_to_a_y*(1 - distance_to_a_x) +
img.at<Vec3b>(src_d.y, src_d.x)[1] * distance_to_a_y*distance_to_a_x;
drcimg.at<Vec3b>(row, cols)[2] =
img.at<Vec3b>(src_a.y, src_a.x)[2] * (1 - distance_to_a_x)*(1 - distance_to_a_y) +
img.at<Vec3b>(src_b.y, src_b.x)[2] * distance_to_a_x*(1 - distance_to_a_y) +
img.at<Vec3b>(src_c.y, src_c.x)[2] * distance_to_a_y*(1 - distance_to_a_x) +
img.at<Vec3b>(src_d.y, src_d.x)[2] * distance_to_a_y*distance_to_a_x;
}
cv::imwrite("矫正完成.bmp", drcimg);
cv::imshow("矫正完成", drcimg);
cv::waitKey(0);
img.release();
drcimg.release();
waitKey();
return;
}
在设计图像扭曲的时候,先进行扭曲,再进行插值
首先定义距离函数
double GetDistence(double x, double y, double xm, double ym)
{
return sqrt(pow((x - xm), 2) + pow((y - ym), 2));
}
在这里图像扭曲使用了双线性插值
定义一个和原图像大小相同的Mat矩阵,初始化为0,用来接受赋值输出。这个
找到图像的中心点,xm,ym
然后对图像的像素点进行遍历处理
这里使用了图像的归一化和还原用来消除数据之间的影响
归一化在-1~+1之间是统计的坐标分布
double X = x / ((row - 1) / 2.0) - 1.0;
double Y = y / ((col - 1) / 2.0) - 1.0;
x_ = (x_ + 1.0)((row - 1) / 2.0);
y_ = (y_ + 1.0)((col - 1) / 2.0);
r:double r = sqrt(X * X + Y * Y);
因为像素点归一化了,所以这个时候也要对规定的半径进行归一化,所以使用Radius/(row/2)
R以内的就应该进行旋转,然后插值,具体如下:
Theta:double theta = RADIAN(amax)*(Radius - GetDistence(x, y, xm, ym)) / Radius;
得到的是旋转角
double x_ = cos(theta)*X - sin(theta)*Y;
double y_ = sin(theta)*X + cos(theta)*Y;
得到位置之后进行归一化的还原,上面已经解释过。
对于超出图像边界的像素点,三个通道赋值0
if (x_ < 0 || y_ < 0 || x_ >= src.rows || y_ >= src.cols) {
for (int c = 0; c < 3; c++) {
imgAffine.at(x, y)[c] = saturate_cast(0);
}
}
对于像素边界以内的,这个时候使用双线性插值进行赋值。
在三个通道以内,图像的右和下方的边界进行原值赋值,这是考虑到半径正好等于图像的一半的情况,
然后获得右方,下方,右下方的像素点,进行计算,计算函数如下
得到的结果赋值给mat图像,然会给主函数,并且imgshow
双三次插值:
和双线性插值的使用的旋转函数相同,不同的地方在于插值的地方双三次插值时选用的最近的16个点,双线性是最近的四个点
首先写出三次卷积函数
float BiCubicPloy(float x)
{
float abs_x = abs(x);//取x的绝对值
float a = -0.5;
if (abs_x <= 1.0)
{
return (a + 2)pow(abs_x, 3) - (a + 3)pow(abs_x, 2) + 1;
}
else if (abs_x < 2.0)
{
return a * pow(abs_x, 3) - 5 * apow(abs_x, 2) + 8 * aabs_x - 4 * a;
}
else
return 0.0;
}
然后计算周围16个点的权重
```cpp
//取出映射到原图中的点的四周的16个点的坐标
int x0 = xi - 1;
int y0 = yi - 1;
int x1 = xi;
int y1 = yi;
int x2 = xi + 1;
int y2 = yi + 1;
int x3 = xi + 2;
int y3 = yi + 2;
//在范围内进行计算,将16个点的权重之和作为新像素点
if ((x0 >= 0) && (x3 < src.rows) && (y0 >= 0) && (y3 < src.cols))
{
//求出行和列所对应的系数
float dist_x0 = BiCubicPloy(xm - x0);
float dist_x1 = BiCubicPloy(xm - x1);
float dist_x2 = BiCubicPloy(xm - x2);
float dist_x3 = BiCubicPloy(xm - x3);
float dist_y0 = BiCubicPloy(ym - y0);
float dist_y1 = BiCubicPloy(ym - y1);
float dist_y2 = BiCubicPloy(ym - y2);
float dist_y3 = BiCubicPloy(ym - y3);
//k_i_j=k_i*k_j
float dist_x0y0 = dist_x0 * dist_y0;
float dist_x0y1 = dist_x0 * dist_y1;
float dist_x0y2 = dist_x0 * dist_y2;
float dist_x0y3 = dist_x0 * dist_y3;
float dist_x1y0 = dist_x1 * dist_y0;
float dist_x1y1 = dist_x1 * dist_y1;
float dist_x1y2 = dist_x1 * dist_y2;
float dist_x1y3 = dist_x1 * dist_y3;
float dist_x2y0 = dist_x2 * dist_y0;
float dist_x2y1 = dist_x2 * dist_y1;
float dist_x2y2 = dist_x2 * dist_y2;
float dist_x2y3 = dist_x2 * dist_y3;
float dist_x3y0 = dist_x3 * dist_y0;
float dist_x3y1 = dist_x3 * dist_y1;
float dist_x3y2 = dist_x3 * dist_y2;
float dist_x3y3 = dist_x3 * dist_y3;
imgAffine.at<Vec3b>(x, y) = (src.at<Vec3b>(x0, y0)*dist_x0y0 +
src.at<Vec3b>(x0, y1)*dist_x0y1 +
src.at<Vec3b>(x0, y2)*dist_x0y2 +
src.at<Vec3b>(x0, y3)*dist_x0y3 +
src.at<Vec3b>(x1, y0)*dist_x1y0 +
src.at<Vec3b>(x1, y1)*dist_x1y1 +
src.at<Vec3b>(x1, y2)*dist_x1y2 +
src.at<Vec3b>(x1, y3)*dist_x1y3 +
src.at<Vec3b>(x2, y0)*dist_x2y0 +
src.at<Vec3b>(x2, y1)*dist_x2y1 +
src.at<Vec3b>(x2, y2)*dist_x2y2 +
src.at<Vec3b>(x2, y3)*dist_x2y3 +
src.at<Vec3b>(x3, y0)*dist_x3y0 +
src.at<Vec3b>(x3, y1)*dist_x3y1 +
src.at<Vec3b>(x3, y2)*dist_x3y2 +
src.at<Vec3b>(x3, y3)*dist_x3y3);
}
}
输出结果:
鲜活的镜头中心在图像中的位置,一般中心都是最准确的
我们首先求出图像像素点距离镜头中心各个距离所对应的k值,使用四次多项式进行拟合
定义矫正参数:
//矫正参数
double r;//矫正前像素点跟镜头中心的距离
double s;//矫正后像素点跟镜头中心的距离
采用双先行擦和执法进行拟合
做越界判断
//越界判断
if (mCorrectPoint.y < 0 || mCorrectPoint.y >= img.rows - 1)
{
continue;
}
if (mCorrectPoint.x < 0 || mCorrectPoint.x >= img.cols - 1)
{
continue;
}
对三个通道的每一个通道的右方,下方,右下方的像素点进行双线性插值计算
src_a = cvPoint((int)mCorrectPoint.x, (int)mCorrectPoint.y);
src_b = cvPoint(src_a.x + 1, src_a.y);
src_c = cvPoint(src_a.x, src_a.y + 1);
src_d = cvPoint(src_a.x + 1, src_a.y + 1);
然后三通道赋值
drcimg.at<Vec3b>(row, cols)[0] =
img.at<Vec3b>(src_a.y, src_a.x)[0] * (1 - distance_to_a_x)*(1 - distance_to_a_y) +
img.at<Vec3b>(src_b.y, src_b.x)[0] * distance_to_a_x*(1 - distance_to_a_y) +
img.at<Vec3b>(src_c.y, src_c.x)[0] * distance_to_a_y*(1 - distance_to_a_x) +
img.at<Vec3b>(src_d.y, src_d.x)[0] * distance_to_a_y*distance_to_a_x;
drcimg.at<Vec3b>(row, cols)[1] =
img.at<Vec3b>(src_a.y, src_a.x)[1] * (1 - distance_to_a_x)*(1 - distance_to_a_y) +
img.at<Vec3b>(src_b.y, src_b.x)[1] * distance_to_a_x*(1 - distance_to_a_y) +
img.at<Vec3b>(src_c.y, src_c.x)[1] * distance_to_a_y*(1 - distance_to_a_x) +
img.at<Vec3b>(src_d.y, src_d.x)[1] * distance_to_a_y*distance_to_a_x;
drcimg.at<Vec3b>(row, cols)[2] =
img.at<Vec3b>(src_a.y, src_a.x)[2] * (1 - distance_to_a_x)*(1 - distance_to_a_y) +
img.at<Vec3b>(src_b.y, src_b.x)[2] * distance_to_a_x*(1 - distance_to_a_y) +
img.at<Vec3b>(src_c.y, src_c.x)[2] * distance_to_a_y*(1 - distance_to_a_x) +
img.at<Vec3b>(src_d.y, src_d.x)[2] * distance_to_a_y*distance_to_a_x;