Matlab-数字图像处理-基础实验-图片展缩(最近邻、双线性插值)

2023-05-16

Matlab-数字图像处理-基础实验-图片展缩(使用最近邻、双线性插值)

Problem Statement

1.使用MATLAB开发环境来实现缩放转换。 开发一个函数,可以实现通过最近邻插值的图像缩放:
out = nearest(img, a)
其中img是图片字符串,a是比例因子,a<1或者a>1。
2.开发一个函数,可以实现通过双线性插值的图像缩放:
out = bi_linear(img, a)
其中img是图片字符串,a是比例因子,a<1或者a>1。

Procedure

(A)out = nearest(img, a)
(1) Read image to “img_original” and get image size [row,col].
(2) Initialize the rows and columns of the new image.
(3) Use class()function to obtain the data type of the original image ( “img_original”), so that the new image (out) data type is consistent with the original image.
(4) Traverse the new image (out) and assign to it by calculating the (x,y) of the original image.
(5) Show the results.
(B)out = bi_linear(img, a)
(1) Read image to “img_original” and get image size [row,col].
(2) Initialize the rows and columns of the new image.
(3) Use class()function to obtain the data type of the original image ( “img_original”), so that the new image (out) data type is consistent with the original image.
(4)Loop through the image:
(5) x = i/a; y = j/a;
(6) Calculate x1 and y1
(7) Set the image boundary
(8) Define 4 pixel points P1, P2, P3 and P4, and then assign values to the image through Formula 6 or Formular 3,4,5
Formula 6:
f(x, y) = [ f(1, 0) – f(0, 0)] x + [ f(0, 1) – f(0, 0)]y+ [f(1, 1) – f(0, 1) - f(1, 0) + f(0, 0)]xy+f(0, 0).
Formular 3,4,5:
f(x, 0) = f(0, 0) + x [ f(1, 0) – f(0, 0)]
f(x, 1) = f(0, 1) + x [ f(1, 1) – f(0, 1)]
f(x, y) = f(x, 0) + y [ f(x, 1) – f(x, 0)].
(9)End Loop.
(10)Display image.

Source code

(A)out = nearest(img, a)

function [out] = nearest(img, a)
img_original=imread(img);%500*500
img_original=rgb2gray(img_original);
[row,col] = size(img_original); 

%round()舍入到最接近的整数
row = round(row*a);     % 新图像行
col = round(col*a);     % 新图像列
%250*250 *0.5

% 使用class获得原图像的数据类型,使得新图像数据类型与原图像保持一致
out = zeros(row,col,class(img_original));
%250*250

for i=1:row
    for j=1:col
            x = round(i/a);
            y = round(j/a);
            % 为了避免x和y等于0而报错,采取+1处理即可
            if x < 1
                x = 1;
            end
            if y < 1
                y = 1;
            end
            out(i,j) = img_original(x,y);
    end
end

% 显示原图像
figure;
imshow(img);
title("Original Image");

% 显示新图像
out = im2uint8(mat2gray(out));
figure;
imshow(out);
title("New Image");


(B)out = bi_linear(img, a)

img1=imread("F:\cxy_m\1.png");
img2=nearest("F:\cxy_m\1.png",0.5);
img3=bi_linear1("F:\cxy_m\1.png",0.5);
subplot(1,3,1),imshow(img1),title("original");
subplot(1,3,2),imshow(img2),title("nearest");
subplot(1,3,3),imshow(img3),title("bi-linear");

function [out] = bi_linear1(img,a)
img_original=imread(img);%500*500
%将彩色图像装为灰度图像
img_original=rgb2gray(img_original);
[row,col] = size(img_original);
new_row = round(row*a);  
new_col = round(col*a);  
%初始化
out = zeros(new_row,new_col,class(img_original));
%遍历
for i = 1:new_row
    for j = 1:new_col
        x = i / a;
        y = j / a;
        x1 = x - floor(x);
        y1 = y - floor(y);
        %边界设置
        if x < 1
           x = 1;
        end
        if y < 1
           y = 1;
        end
        
        if x > row 
            x = row;
        end
        if y > col
            y = col;
        end
        
        p1 = img_original(floor(x), floor(y));%(0,0)
        p2 = img_original(floor(x), ceil(y));%(0,1)
        p3 = img_original(ceil(x), floor(y));%(1,0)
        p4 = img_original(ceil(x), ceil(y));%(1,1)

%         %PPT8 P17 公式6
%         out(i, j) =	(p3 - p1)* x1  + (p2 - p1)*y1+... 
% 			+ (p4 - p2 - p3+ p1)*x1*y1+...
% 			+ p1;
        f1 = p1 + x1*(p3 - p1);%公式三
        f2 = p2 + x1*(p4 - p2);%公式四
        out(i,j) = f1 + y1 * (f2 - f1);%公式五
    end
end
out = im2uint8(mat2gray(out));
figure;
imshow(out);
title('Bilinear interpolation');
end

sample program output

在这里插入图片描述
在这里插入图片描述

Problems you encountered

(i)It should output one image, but it output three images at once.
在这里插入图片描述
在这里插入图片描述
The code here conflicts with the floor() function.I changed it to:x<1,y<1 to solve it.
(ii)使用公式3,4,5会出现网格(在现等一位大佬解惑)
在这里插入图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Matlab-数字图像处理-基础实验-图片展缩(最近邻、双线性插值) 的相关文章

  • pip版本过低无法更新 解决方案

    问题 xff1a 方案一 xff08 失败 xff09 xff1a python span class token operator span m pip install span class token operator span spa
  • MOOC数据结构(下)(自主模式)-平均气温(Temperature)

    平均气温 Temperature Description A weather station collects temperature data from observation stations all over the country
  • javascript的事件介绍

    你的点赞就是继续前行的动力 xff0c 嘻嘻 文章目录 事件概述二 事件绑定2 1HTML事件2 2 DOM0级事件2 3 DOM2级事件 三 鼠标事件四 焦点事件五 滚动事件六 键盘事件6 1 介绍6 2 属性 七 手机触摸事件7 1 手
  • 获取手机页面的元素位置坐标

    解决方案一 xff1a 在手机开发者选项中 xff0c 将指针位置打开 xff0c 可以在屏幕上方看到当前点击位置的坐标点 X Y 例如 xff1a P 1 1 X 545 Y 1846 Xv 0 0 Yv 0 0 Prs 1 0 Size
  • ubuntu 触摸屏横屏变竖屏解决 触摸功能仍然是横屏的问题

    在经过横屏转竖屏时 发现触摸屏仍然是横屏时候的触摸点 横屏时触摸屏正常使用 xff0c 竖屏时不正常 解决 xff1a 需要将 触摸屏进行旋转 通过 input 通过 xrandr o right 旋转后触摸屏不正常 旋转触摸的坐标轴 xf
  • 计算机组成原理三:总线

    第三章 总线 1 总线概念 计算机硬件系统有2种互联方式 一种是各部件之间单独连线 称为分散连接 另一种是将各部件连到一组公关信息 传输线上 称为总线连接 总线是一组线路 将计算机的各个硬件连接在一起 让它们可以通过这条公共线路进行数据的传
  • Matlab-数字图像处理-获取图片rgb颜色分量及截取子图

    Matlab 数字图像处理基础实验 获取图片rgb颜色分量及截取子图 Problem Statement 问题描述 1 Proficient in Matlab tools and complete two Matlab functions
  • ICRA2020论文整理(SLAM + Deep Learning)

    参考 https github com PaoPaoRobot ICRA2020 paper list 目录 1 SLAM 2 Deep Learning in Robotics and Automation 3 Localization
  • win10+ubuntu16.04双系统下完全删除并重装ubuntu16.04

    参考文章 xff1a 1 彻底删除Ubuntu EFI分区及启动项 xff1a https blog csdn net mtllyb article details 78635757 2 Create a bootable USB stic
  • Ubuntu18.04开机挂载硬盘

    Linux与Windows的文件资源管理在加载上有所区别 xff0c Windows自动加载主板上连接的所有磁盘 xff0c 而Linux默认只挂载系统所在的分区 xff0c 使用其他分区则需要使用mount命令手动挂载 若需系统启动时自动
  • [Docker] 删除所有httpd镜像创建的容器 - 详细解释

    故意 一不小心创建了很多httpd容器 或者包含 httpd 关键字 xff0c 有的在运行 xff0c 有的已经停止了 xff0c 有的已经退出 xff0c 如果我想强制一键删除 xff0c 该怎么办呢 xff1f 其实很简单 xff0c
  • 直流调速器(有刷电调)的工作原理

    以前测试过无刷电调 xff0c 它是把锂电池的直流电转化为三相交流波形 xff0c 最近买了有刷电调 xff0c 也研究一下它的调速原理 调速器如下 xff1a 飞马 30A单向有刷电调 固定翼飞机专用 带动力启动开关 测试平台 连接示波器
  • 百度开发测试工程师在线笔试

    时间 xff1a 2020 9 3 19 xff1a 00 21 xff1a 00 笔试内容 xff1a 选择题 编程题 1 选择题 xff08 30个 xff09 50分钟 xff0c 60分 xff0c 涵盖内容较广 xff0c 包含计
  • ROS1重温:自定义头文件、源文件

    ROS1重温 xff1a 自定义头文件 源文件 自定义头文件 源文件的意义自定义头文件创建自定义头文件使用头文件 xff0c 并在程序中直接实现函数功能修改 CMakeLists txt 文件 自定义头文件中实现函数功能的源文件创建自定义头
  • spring cloud bus 消息总线 原理总结

    1 spring cloud bus spring cloud bus整合java的事件处理机制和消息中间件的发送和接收 xff0c 主要是由发送端 接收端和事件组成 目前spring cloud bus只实现了RabbitMq和Kafka
  • NGUI扩展:为面板(Panel)添加自定义图片遮罩

    前一阵使用NGUI开发时遇到一个实现圆形小地图的需求 小地图上除了地图背景外还有一大堆的零零碎碎的角色提示信息啥的 xff0c 因此创建了一个panel进行绘制 xff0c 剩下的就是如何让这个panel只在一个圆形的区域内进行显示 NGU
  • Unity3D使用RenderCommand渲染外轮廓

    外轮廓渲染方式原来的做法使用CommandBuffer 外轮廓渲染方式 我这里所要介绍的外轮廓是使用模糊后处理实现的 xff0c 不涉及到边缘查找或是顶点扩展这些 xff0c 简单的说这种方式渲染外轮廓总共分三步 1 用单色渲染目标物体到R
  • Matlab-计算直方图+直方图均衡

    数字图像处理基础实验 计算直方图 43 直方图均衡 Problem Statement xff08 问题描述 xff09 1 Histograms are the basis for numerous spatial domain proc
  • Unity 5 全局光照GI与新的烘焙系统初探

    GI是啥 Realtime GI xff0c 实时全局光照 xff0c 听上去就是一个非常高大上的词 xff0c 但是越高大上就越令人心生敬畏 xff0c 因为世上没有免费的午餐 xff0c 越好的效果意味着越多的消耗 xff0c 对于移动
  • Unity5 (5.0-5.2) Shader编译机制初探 - 小心,Shader在吞噬你的内存

    又被Unity坑了一把 xff0c 简单说下吧 xff0c 下面都是流水账 xff0c 结论就写在最后了 xff0c 就是Unity5 5 2的shader编译机制真是不咋地 1 Why Always me 问题是这样的 xff0c 我照着

随机推荐