matlab图像放大程序,图像放大并进行BiCubic插值 Matlab/C++代码
BiCubic插值原理:
双三次插值又称立方卷积插值。三次卷积插值是一种更加复杂的插值方式。该算法利用待采样点周围16个点的灰度值作三次插值,不仅考虑到4 个直接相邻点的灰度影响,而且考虑到各邻点间灰度值变化率的影响。三次运算可以得到更接近高分辨率图像的放大效果,但也导致了运算量的急剧增加。这种算法需要选取插值基函数来拟合数据,其最常用的插值基函数如图1所示,本次实验采用如图所示函数作为基函数。
构造BiCubic函数:
其中,a取-0.5.
BiCubic函数具有如下形状:
[source: R. Keys, (1981). "Cubic convolution interpolation for digital image processing". IEEE Transactions on Signal Processing, Acoustics, Speech, and Signal Processing 29 (6): 1153–1160.]
对待插值的像素点(x,y)(x和y可以为浮点数),取其附近的4x4邻域点(xi,yj), i,j = 0,1,2,3。按如下公式进行插值计算:
Matlab实现代码:
%双三次插值具体实现
clc,clear;
fff=imread('E:\Documents\BUPT\DIP\图片\lena.bmp');
ff =rgb2gray(fff);%转化为灰度图像
[mm,nn]=size(ff); %将图像隔行隔列抽取元素,得到缩小的图像f
m=mm/2;
n=nn/2;
f =zeros(m,n);
for i=1:m
for j=1:n
f(i,j)=ff(2*i,2*j);
end
end
k=5; %设置放大倍数
bijiao1 =imresize(f,k,'bilinear');%双线性插值结果比较
bijiao =uint8(bijiao1);
a=f(1,:);
c=f(m,:); %将待插值图像矩阵前后各扩展两行两列,共扩展四行四列
b=[f(1,1),f(1,1),f(:,1)',f(m,1),f(m,1)];
d=[f(1,n),f(1,n),f(:,n)',f(m,n),f(m,n)];
a1=[a;a;f;c;c];
b1=[b;b;a1';d;d];
ffff=b1';
f1=double(ffff);
g1 =zeros(k*m,k*n);
fori=1:k*m %利用双三次插值公式对新图象所有像素赋值
u=rem(i,k)/k;
i1=floor(i/k)+2;
A=[sw(1+u) sw(u) sw(1-u) sw(2-u)];
for j=1:k*n
v=rem(j,k)/k;
j1=floor(j/k)+2;
C=[sw(1+v);sw(v);sw(1-v);sw(2-v)];
B=[f1(i1-1,j1-1) f1(i1-1,j1) f1(i1-1,j1+1)f1(i1-1,j1+2)
f1(i1,j1-1) f1(i1,j1) f1(i1,j1+1) f1(i1,j1+2)
f1(i1+1,j1-1) f1(i1+1,j1) f1(i1+1,j1+1) f1(i1+1,j1+2)
f1(i1+2,j1-1) f1(i1+2,j1) f1(i1+2,j1+1)f1(i1+2,j1+2)];
g1(i,j)=(A*B*C);
end
end
g=uint8(g1);
imshow(uint8(f));title('缩小的图像'); %显示缩小的图像
figure,imshow(ff);title('原图'); %显示原图像
figure,imshow(g);title('双三次插值放大的图像'); %显示插值后的图像
figure,imshow(bijiao);title('双线性插值放大结果'); %显示插值后的图像
mse=0;
ff=double(ff);
g=double(g);
ff2=fftshift(fft2(ff)); %计算原图像和插值图像的傅立叶幅度谱
g2=fftshift(fft2(g));
figure,subplot(1,2,1),imshow(log(abs(ff2)),[8,10]);title('原图像的傅立叶幅度谱');
subplot(1,2,2),imshow(log(abs(g2)),[8,10]);title('双三次插值图像的傅立叶幅度谱');
基函数代码:
functionA=sw(w1)
w=abs(w1);
ifw<1&&w>=0
A=1-2*w^2+w^3;
elseifw>=1&&w<2
A=4-8*w+5*w^2-w^3;
else
A=0;
end
C++实现代码:
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include
#include
#include
using namespace cv;
using namespace std;
#define PI 3.14159265
float BiCubicPoly(float x);
void MyScaleBiCubicInter(Mat& src, Mat& dst, float TransMat[3][3]);
/**
* @function main
*/
int main( int argc, char** argv )
{
// load image
char* imageName = "images/Lenna_256.png";
Mat image;
image = imread(imageName,1);if(!image.data)
{
cout << "No image data" << endl;
return -1;
}
// show image
namedWindow("image", CV_WINDOW_AUTOSIZE);
imshow("image", image);
Mat dst;
float transMat[3][3] = { {2.0, 0, 0}, {0, 2.0, 0}, {0, 0, 1} };MyScaleBiCubicInter(image, dst, transMat);
namedWindow("out_image", CV_WINDOW_AUTOSIZE);
imshow("out_image", dst);
imwrite("Lenna_scale_biCubic2.jpg", dst);
waitKey(0);
return 0;
}
float BiCubicPoly(float x)
{
float abs_x = abs(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;
}
void MyScaleBiCubicInter(Mat& src, Mat& dst, float TransMat[3][3])
{
CV_Assert(src.data);
CV_Assert(src.depth() != sizeof(uchar));
// calculate margin point of dst image
float left = 0;
float right = 0;
float top = 0;
float down = 0;
float x = src.cols * 1.0f;
float y = 0.0f;
float u1 = x * TransMat[0][0] + y * TransMat[0][1];
float v1 = x * TransMat[1][0] + y * TransMat[1][1];
x = src.cols * 1.0f;
y = src.rows * 1.0f;
float u2 = x * TransMat[0][0] + y * TransMat[0][1];
float v2 = x * TransMat[1][0] + y * TransMat[1][1];
x = 0.0f;
y = src.rows * 1.0f;
float u3 = x * TransMat[0][0] + y * TransMat[0][1];
float v3 = x * TransMat[1][0] + y * TransMat[1][1];
left = min( min( min(0.0f,u1), u2 ), u3);
right = max( max( max(0.0f,u1), u2 ), u3);
top = min( min( min(0.0f,v1), v2 ), v3);
down = max( max( max(0.0f,v1), v2 ), v3);
// create dst image
dst.create(int(abs(right-left)), int(abs(down-top)), src.type());
CV_Assert( dst.channels() == src.channels() );
int channels = dst.channels();
int i,j;
uchar* p;
uchar* q0;
uchar* q1;
uchar* q2;
uchar* q3;
for( i = 0; i < dst.rows; ++i)
{
p = dst.ptr(i);
for ( j = 0; j < dst.cols; ++j)
{
//
x = (j+left)/TransMat[0][0] ;
y = (i+top)/TransMat[1][1] ;
int x0 = int(x) - 1;
int y0 = int(y) - 1;
int x1 = int(x);
int y1 = int(y);
int x2 = int(x) + 1;
int y2 = int(y) + 1;
int x3 = int(x) + 2;
int y3 = int(y) + 2;
if( (x0 >= 0) && (x3 < src.cols) && (y0 >= 0) && (y3 < src.rows) )
{
q0 = src.ptr(y0);
q1 = src.ptr(y1);
q2 = src.ptr(y2);
q3 = src.ptr(y3);
float dist_x0 = BiCubicPoly(x-x0);
float dist_x1 = BiCubicPoly(x-x1);
float dist_x2 = BiCubicPoly(x-x2);
float dist_x3 = BiCubicPoly(x-x3);
float dist_y0 = BiCubicPoly(y-y0);
float dist_y1 = BiCubicPoly(y-y1);
float dist_y2 = BiCubicPoly(y-y2);
float dist_y3 = BiCubicPoly(y-y3);
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;
switch(channels)
{
case 1:
{
break;
}
case 3:
{
p[3*j] = (uchar)(q0[3*x0] * dist_x0y0 +
q1[3*x0] * dist_x0y1 +
q2[3*x0] * dist_x0y2 +
q3[3*x0] * dist_x0y3 +
q0[3*x1] * dist_x1y0 +
q1[3*x1] * dist_x1y1 +
q2[3*x1] * dist_x1y2 +
q3[3*x1] * dist_x1y3 +
q0[3*x2] * dist_x2y0 +
q1[3*x2] * dist_x2y1 +
q2[3*x2] * dist_x2y2 +
q3[3*x2] * dist_x2y3 +
q0[3*x3] * dist_x3y0 +
q1[3*x3] * dist_x3y1 +
q2[3*x3] * dist_x3y2 +
q3[3*x3] * dist_x3y3 ) ;
p[3*j+1] = (uchar)(q0[3*x0+1] * dist_x0y0 +
q1[3*x0+1] * dist_x0y1 +
q2[3*x0+1] * dist_x0y2 +
q3[3*x0+1] * dist_x0y3 +
q0[3*x1+1] * dist_x1y0 +
q1[3*x1+1] * dist_x1y1 +
q2[3*x1+1] * dist_x1y2 +
q3[3*x1+1] * dist_x1y3 +
q0[3*x2+1] * dist_x2y0 +
q1[3*x2+1] * dist_x2y1 +
q2[3*x2+1] * dist_x2y2 +
q3[3*x2+1] * dist_x2y3 +
q0[3*x3+1] * dist_x3y0 +
q1[3*x3+1] * dist_x3y1 +
q2[3*x3+1] * dist_x3y2 +
q3[3*x3+1] * dist_x3y3 ) ;
p[3*j+2] = (uchar)(q0[3*x0+2] * dist_x0y0 +
q1[3*x0+2] * dist_x0y1 +
q2[3*x0+2] * dist_x0y2 +
q3[3*x0+2] * dist_x0y3 +
q0[3*x1+2] * dist_x1y0 +
q1[3*x1+2] * dist_x1y1 +
q2[3*x1+2] * dist_x1y2 +
q3[3*x1+2] * dist_x1y3 +
q0[3*x2+2] * dist_x2y0 +
q1[3*x2+2] * dist_x2y1 +
q2[3*x2+2] * dist_x2y2 +
q3[3*x2+2] * dist_x2y3 +
q0[3*x3+2] * dist_x3y0 +
q1[3*x3+2] * dist_x3y1 +
q2[3*x3+2] * dist_x3y2 +
q3[3*x3+2] * dist_x3y3 ) ;
float thre = 198.0f;
if( (abs(p[3*j]-q1[3*x1]) > thre) || (abs(p[3*j+1]-q1[3*x1+1]) > thre) ||
(abs(p[3*j+2]-q1[3*x1+2]) > thre) )
{
p[3*j] = q1[3*x1];
p[3*j+1] = q1[3*x1+1];
p[3*j+2] = q1[3*x1+2];
}
break;
}
}
}
}
}
}
matlab图像放大程序,图像放大并进行BiCubic插值 Matlab/C++代码相关推荐
- matlab提取图像特征程序,图像特征提取matlab程序
<图像特征提取matlab程序>由会员分享,可在线阅读,更多相关<图像特征提取matlab程序(2页珍藏版)>请在人人文库网上搜索. 1.直接帧间差分,计算阈值并进行二值化处理 ...
- 自适应滤波器在matlab仿真的程序_电气信息类专业课程之matlab系统仿真 第五章 BPSK通信系统(3)...
继续讲解! 上一篇文章提出了那么多问题, 不知道大家是否能回答上来啊? 如果回答不了, 有没有去问问度娘呢? 程序写完了,回想了一下辅导2017届学生毕业设计的过程,那是真累.我要求他们在现有程序基础 ...
- matlab 平滑曲线连接_平滑轨迹插值方法之多项式插值(附代码)
前言 今天我们来聊聊轨迹插值,在机器人的运动规划和控制领域,参考轨迹的生成是一个历史悠久的问题,已经发展出了一系列的方法.今天我们就来聊一聊轨迹插值领域中最常见的轨迹插值方法:多项式插值. 说明:本文 ...
- Matlab 样条插值小程序
Matlab 样条插值小程序 经常需要简单方便的插值小程序,进行数据处理,可以用Matlab轻松实现: % Matlab 样条插值小程序; clear; clc; x=[]; %x坐标 y=[]; % ...
- 利用图像内插法放大缩小图像 Matlab
原文:https://blog.csdn.net/Goldfish442/article/details/61933735 利用图像内插法放大缩小图像 Matlab 内插是利用已知数据来估计未知位置的 ...
- opencv学习笔记19:图像金字塔和图像拉普拉斯金字塔 (用于图像放大和缩小)
图像金字塔原理 图像金字塔:只不同分辨率,不同尺寸子图构成的集合. 取样 向下取样:有一个图像G0,对它重新采样,得到分辨率小些的G1,以此类推. 向上取样:有一个图像G3,然后变成像素更多的G2,以 ...
- LabVIEW操作鼠标滚轮放大/缩小图像
之前分享过一篇关于LabVIEW采集鼠标.键盘数据的文章:LabVIEW采集鼠标.键盘数据,本篇博文将分享一个关于鼠标滚轮的有意思小技巧:操作鼠标滚轮来放大和缩小图片. 示例效果如下所示: 本示例功能 ...
- 图像金字塔实现图像缩放_一个简单的VueJS组件,用于图像放大/产品缩放
图像金字塔实现图像缩放 Vue放大镜 (vue-magnifier) Vue Magnifier is free component for basic image zoom practices. Y ...
- 【图像融合】基于matlab DSIFT多聚焦图像融合【含Matlab源码 2224期】
⛄一.SIFT配准简介 1 算法概述 在实时系统中,算法的输入为相机数据流,当前输入的图像与上一张相似度很高时应不参与融合,由于在体视显微镜下序列图像存在较大程度的偏移,所以融合前还需要进行图像配准, ...
- 【图像处理】基于半色调技术的图像打印程序(Image Printing Program Based on Halftoning)
实验要求 本实验后面的图像给出了用点模式近似表示的10 个灰度级.每一个灰度级用一个3 x 3 的黑白点模式表示.用黑点全部填充的3 x 3 区域近似表示灰度级为0 的黑色灰度级,全部填充白点的3 ...
最新文章
- 一文概览图卷积网络基本结构和最新进展(附视频代码)
- 冯珊珊_高球一姐【冯珊珊】签约【GREENJOY】成为品牌全球代言人
- idea远程调试修改代码_使用IDEA远程调试线上代码
- 更新ubuntu软件源
- verilog基础-状态机之FPGA独立按键消抖设计与验证(熟练testbench的写法)
- 用户创建自定义的参数ID
- Cisco开CASE方法
- php中添加一个链接,使用php在推文中链接一个标签
- android studio导入eclipse项目各种问题,eclipse项目导入android studio 各类问题及解决方法...
- ajax返回来总是html,ajax返回类型
- MyBatis 接口绑定方案及多参数传递、动态 SQL、ThreadLocal、缓存
- fpga 输入信号除了1和0还有别的状态?
- 求抛物线和直线交点_关于抛物线大题的参考经验(5):浙江历年学考题回顾...
- php基础知识和函数
- 10款概念手机,哪款是你的最爱
- 5.2使用select,poll
- Android 触摸事件转换为鼠标事件
- 计算机telnet命令大全,Telnet命令
- CRM系统中的线索、客户、联系人、商机
- App Inventor 2能编译出苹果iOS版App吗?
热门文章
- 误差状态方程与雅可比矩阵
- 怎么让模糊的数字变清楚_如何用ps将模糊图片变清晰?
- window10截取动图(录制视频转gif)
- 树莓派 ubuntu gpio_玩转GPIO之点亮三色信号灯(非树莓派)
- IndentationError: expected an indented block解决方案
- Unity 接入图灵机器人
- 计算机单位厘米 像素,像素厘米转换器
- Python Re正则表达式之group(0)、group(1)
- 终于找到了,中国知网免费下载论文诀窍!
- 《Java编程那些事儿----这是一本书》