Opencv之双线性插值(包含一些优化技巧)

 1.介绍

    对于双线性插值,学过图像处理的人,其理论应该是随手拈来,代码也是很容易被写出来的,但工业应用的时候,会做一些技巧性优化。

    奥秘之处在于两点:

    1)源图像和目标图像几何中心的对齐

     双线性插值算法及需要注意事项提到,如果起始点设置为(0,0)的时候,会导致一部分数据没被充分利用

    2)将浮点运算转换成整数运算

     图像处理界双线性插值算法的优化提到,对于插值运算,如果不做特别处理,肯定是有浮点数运算,因此可以先进行放大,接着进行整型运算,最后进行右移运算(缩小操作)

2.代码

#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include<iostream>

using namespace std;
using namespace cv;

void nearNeighbor(Mat& matSrc, Mat& matDst1, Mat& matDst2);

int main() {
	Mat matSrc, matDst1, matDst2;
	matSrc = imread("Lena.jpg", 2 | 4);
	matDst1 = Mat(cv::Size(800, 1000), matSrc.type(), Scalar::all(0));
	matDst2 = Mat(matDst1.size(), matSrc.type(), Scalar::all(0));
	bilinearity(matSrc, matDst1, matDst2);

	return 0;
}


//放大系数为2048,保证不溢出,且精确度比较高; 加0.5是保证几何中心,不然有些点用不到
void bilinearity(Mat& matSrc, Mat& matDst1, Mat& matDst2) {
	double scale_x = (double)matSrc.cols / matDst1.cols;
	double scale_y = (double)matSrc.rows / matDst1.rows;

	uchar* dataDst = matDst1.data;
	int stepDst = matDst1.step; //宽*3
	uchar* dataSrc = matSrc.data;
	int stepSrc = matSrc.step;
	int iWidthSrc = matSrc.cols;
	int iHiehgtSrc = matSrc.rows;

	for (int j = 0; j < matDst1.rows; ++j)
	{
		float fy = (float)((j + 0.5) * scale_y - 0.5);
		int sy = cvFloor(fy);
		fy -= sy;
		sy = std::min(sy, iHiehgtSrc - 2);
		sy = std::max(0, sy);

		short cbufy[2];
		cbufy[0] = cv::saturate_cast<short>((1.f - fy) * 2048);
		cbufy[1] = 2048 - cbufy[0];

		for (int i = 0; i < matDst1.cols; ++i)
		{
			float fx = (float)((i + 0.5) * scale_x - 0.5);
			int sx = cvFloor(fx);
			fx -= sx;

			if (sx < 0) {
				fx = 0, sx = 0;
			}
			if (sx >= iWidthSrc - 1) {
				fx = 0, sx = iWidthSrc - 2;
			}

			short cbufx[2];
			cbufx[0] = cv::saturate_cast<short>((1.f - fx) * 2048);
			cbufx[1] = 2048 - cbufx[0];

			for (int k = 0; k < matSrc.channels(); ++k)
			{
				*(dataDst + j*stepDst + 3 * i + k) = (*(dataSrc + sy*stepSrc + 3 * sx + k) * cbufx[0] * cbufy[0] +
					*(dataSrc + (sy + 1)*stepSrc + 3 * sx + k) * cbufx[0] * cbufy[1] +
					*(dataSrc + sy*stepSrc + 3 * (sx + 1) + k) * cbufx[1] * cbufy[0] +
					*(dataSrc + (sy + 1)*stepSrc + 3 * (sx + 1) + k) * cbufx[1] * cbufy[1]) >> 22;
			}
		}
	}
	cv::imwrite("linear_1.jpg", matDst1);

	cv::resize(matSrc, matDst2, matDst1.size(), 0, 0, 1);
	cv::imwrite("linear_2.jpg", matDst2);
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
二次线性是一种在图像处理中常用的方法,可以用于对图像进行缩放、旋转等操作。在 Python 中,可以使用 OpenCV 库来实现二次线性。 下面是一个实现二次线性的示例代码: ```python import cv2 def bilinear_interpolation(img, x, y): # 取整数部分和小数部分 x1, y1 = int(x), int(y) x2, y2 = x1+1, y1+1 r1 = (x2-x)*(y2-y) r2 = (x-x1)*(y2-y) r3 = (x2-x)*(y-y1) r4 = (x-x1)*(y-y1) # 根据公式进行计算 result = img[y1,x1]*r1 + img[y1,x2]*r2 + img[y2,x1]*r3 + img[y2,x2]*r4 return result def resize_bilinear(img, dst_size): height, width = img.shape[:2] dst_height, dst_width = dst_size dst_img = np.zeros((dst_height, dst_width, 3), dtype=np.uint8) scale_x, scale_y = float(width) / dst_width, float(height) / dst_height for dst_y in range(dst_height): for dst_x in range(dst_width): # 计算原图上对应的坐标 src_x = (dst_x + 0.5) * scale_x - 0.5 src_y = (dst_y + 0.5) * scale_y - 0.5 if src_x < 0 or src_x >= width or src_y < 0 or src_y >= height: continue # 进行计算 dst_img[dst_y, dst_x] = bilinear_interpolation(img, src_x, src_y) return dst_img ``` 其中,`bilinear_interpolation` 函数实现了二次线性的算法,`resize_bilinear` 函数则是用于对图像进行缩放的函数。 使用示例: ```python import cv2 # 读取图像 img = cv2.imread('test.jpg') # 缩放图像 dst_size = (int(img.shape[1] * 0.5), int(img.shape[0] * 0.5)) dst_img = resize_bilinear(img, dst_size) # 显示图像 cv2.imshow('Original', img) cv2.imshow('Resized', dst_img) cv2.waitKey(0) ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值