Matlab 保持图像矩阵大小不变对图像放大和缩小 您所在的位置:网站首页 matlab设置矩阵大小 Matlab 保持图像矩阵大小不变对图像放大和缩小

Matlab 保持图像矩阵大小不变对图像放大和缩小

2024-06-03 09:14| 来源: 网络整理| 查看: 265

效果如图所示:

        Matlab里的imresize函数可以对图像放大和缩小,但这同时也会改变图像矩阵的大小,如果想要上图所示的结果,需要再进行一些处理,处理代码如下所示。

clc close all; % 从当前目录下打开一张图片 [filename, filepath] = uigetfile({'*.jpg;*.ppm; jpeg *.;*.bmp;*.png'},'Choose Input Image'); if isequal(filename,0) || isequal(filepath,0) disp('User pressed cancel') return else fullfp = fullfile(filepath, filename); end image = imread(fullfp); %代表要处理的图像 mysize = size(image); %把图像转换成灰度图 if numel(mysize) > 2 image = rgb2gray(image); end r_e = mysize(1); c_e = mysize(2); subplot(2,3,1); imshow(image,[]); title('Input Image'); temp1 = imresize(image,2); %表示把图像放大到原来的两倍,但同时图像矩阵也是变成了原来的两倍 [r_t1,c_t1] = size(temp1); s = temp1(round(r_t1/2)-floor(r_e/2) : round(r_t1/2)+ceil(r_e/2)-1, round(c_t1/2)-floor(c_e/2) : round(c_t1/2)+ceil(c_e/2)-1); subplot(2,3,2); imshow(s,[]); title('Magnification'); temp2 = imresize(image,0.5); %表示把图像缩小到原来的一半,但同时图像矩阵也变成了原来的一半 [r_t2,c_t2] = size(temp2); temp3 = zeros(r_e,c_e); temp3(round(r_e/2)-floor(r_t2/2) : round(r_e/2)+ceil(r_t2/2)-1, round(c_e/2)-floor(c_t2/2) : round(c_e/2)+ceil(c_t2/2)-1) = temp2; ss = temp3; subplot(2,3,3); imshow(ss,[]); title('Minification');

ps:直接调用imresize函数而不进行处理的效果如下图所示:

看起来图像没怎么变化,但存储图像的矩阵已经变大或变小了,如下图所示:

image为原始图像矩阵,temp1为放大后的图像矩阵,temp2为缩小后的图像矩阵。

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有