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; }