最邻近插值-双线性插值

opencv实现最邻近插值

//最邻近插值
void Nearest_neighbor ()
{
	int src_h = m_src.size().height;
	int src_w = m_src.size().width;
	//缩放后图像的大小
	int dst_h = src_h * m_h_times; 
	int dst_w= src_w * m_h_times;
	Mat dst(dst_h,dst_w,m_src.type());
	
	int row, col;
	//通道数,彩色图像通道数是3,灰度图的通道数1
	int channel = m_src.channe1s();
	for (int i = 0;i< dst_h;i++) {
		row=i* ((float)src_h / (f1oat)dst_h)+0.5;
		for (int j=0;j< dst_w; j++) {
			col=j*((f1oat)src_w/ (float)dst_w)+0.5;
			 if (channe1==1)
				 dst.at<uchar>(i,j) = m_src.at<uchar>(row,co1);
			 else if(channel == 3)
				 dst. at<Vec3b>(i,j)= m_src.at<Vec3b>(row,co1);
		}
	}
	namedWindow("Nearest",1); 
	imshow("Nearest",dst);
}

opencv实现双线性插值

void bilinear_interpolation()
{
	int sre_h = m_src.size().height;
	int src_w = n_src.size().width;
	int dst_h = src_h * m_h_times; 
	int dst_w = src_w * m_h_times;
	Mat dst(dst_h, dst_w,m_src. type());
	
	for (int i = 0;i< dst_h; i++){
		float srcx_h = i*((float)src_h / (float)dst_h);
		for (int j= 0: j< dst_w; j++){
			float srcy_w=j*((float)src_w/ (float)dst_w);
			int n=(int)srcx_h;
			int m=(int)srcy_w;
			float n_res = srcx_h-n;
			float m_res = srcy_w-m;
			bool h_flag = false, w_flag =false;
			//边缘像素用邻近插值 
			if (n=src_h-1)
				h_flag = true;
			if (m== src_-1)
				 w_flag = true;
			if (dst.channels()==1){
				if (!h_flag && !w_flag) {
					dst.at<uchar>(i,j) = m_src.at<uchar>(n,m) * (1 - n_res) * (1 - m_res) + m_src.at<uchar>(n +1,m) * n_res * (1- m_res)
+ m_src.at<uchar>(n,m+ 1)* (1 - n_res) * m_res + m_sre. at<uchar>(n + 1,m+1) * n_res * m_res;
				}
				else{
					dst.at<uchar>(i,j) = m_src.at<uchar>(n, m);
				}
			}
			else{
				if (!h_flng && !w_flag){
					dst.at<Vec3b>(i,j) = m_src. at<Vec3b>(n, m) * (1 - n_res) * (1 - mxes) + m_src.at<Vec3b>(n + 1, m) * n_res * (1 - _res)
+ m_arc. at<Vec3b>(n,m + 1)* (1 - n_res) * m_res + m_src. at<Vec3b>(n + 1, m + 1) * n_res * m_res;
				}
				else{
					dst.at<Vec3b>(i,j) = m_src.at<Vec3b>(n,m);
				}
			}
		}
	}
	namedWindow("Bilinear",1);
	imshow("Bilinear", dst);
}

整合后,可以实现图像的放大和缩小

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

using namespace std;
using namespace cv;

Mat m_src;
float m_h_times, m_w_times;

void Input(Mat src, float h_times, float w_times)
{
	m_src = src;
	m_h_times = h_times;
	m_w_times = w_times;
}

//最邻近插值
void Nearest_neighbor ()
{
	int src_h = m_src.size().height;
	int src_w = m_src.size().width;
	//缩放后图像的大小
	int dst_h = src_h * m_h_times; 
	int dst_w= src_w * m_h_times;
	Mat dst(dst_h,dst_w,m_src.type());
	
	int row, col;
	//通道数,彩色图像通道数是3,灰度图的通道数1
	int channel = m_src.channe1s();
	for (int i = 0;i< dst_h;i++) {
		row=i* ((float)src_h / (f1oat)dst_h)+0.5;
		for (int j=0;j< dst_w; j++) {
			col=j*((f1oat)src_w/ (float)dst_w)+0.5;
			 if (channe1==1)
				 dst.at<uchar>(i,j) = m_src.at<uchar>(row,co1);
			 else if(channel == 3)
				 dst. at<Vec3b>(i,j)= m_src.at<Vec3b>(row,co1);
		}
	}
	namedWindow("Nearest",1); 
	imshow("Nearest",dst);
}

void bilinear_interpolation()
{
	int sre_h = m_src.size().height;
	int src_w = n_src.size().width;
	int dst_h = src_h * m_h_times; 
	int dst_w = src_w * m_h_times;
	Mat dst(dst_h, dst_w,m_src. type());
	
	for (int i = 0;i< dst_h; i++){
		float srcx_h = i*((float)src_h / (float)dst_h);
		for (int j= 0: j< dst_w; j++){
			float srcy_w=j*((float)src_w/ (float)dst_w);
			int n=(int)srcx_h;
			int m=(int)srcy_w;
			float n_res = srcx_h-n;
			float m_res = srcy_w-m;
			bool h_flag = false, w_flag =false;
			//边缘像素用邻近插值 
			if (n=src_h-1)
				h_flag = true;
			if (m== src_-1)
				 w_flag = true;
			if (dst.channels()==1){
				if (!h_flag && !w_flag) {
					dst.at<uchar>(i,j) = m_src.at<uchar>(n,m) * (1 - n_res) * (1 - m_res) + m_src.at<uchar>(n +1,m) * n_res * (1- m_res)
+ m_src.at<uchar>(n,m+ 1)* (1 - n_res) * m_res + m_sre. at<uchar>(n + 1,m+1) * n_res * m_res;
				}
				else{
					dst.at<uchar>(i,j) = m_src.at<uchar>(n, m);
				}
			}
			else{
				if (!h_flng && !w_flag){
					dst.at<Vec3b>(i,j) = m_src. at<Vec3b>(n, m) * (1 - n_res) * (1 - mxes) + m_src.at<Vec3b>(n + 1, m) * n_res * (1 - _res)
+ m_arc. at<Vec3b>(n,m + 1)* (1 - n_res) * m_res + m_src. at<Vec3b>(n + 1, m + 1) * n_res * m_res;
				}
				else{
					dst.at<Vec3b>(i,j) = m_src.at<Vec3b>(n,m);
				}
			}
		}
	}
	namedWindow("Bilinear",1);
	imshow("Bilinear", dst);
}

int main()
{
    Mat img = imread("这是你图片的地址");
	imshow("原图", img);
	
	Input(img, 1.5, 1.5);
	Nearest_neighbor();
	bilinear_interpolation();
	
	waitKey(0);
    return 0;
}

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值