matlab如何做如下图片处理
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了matlab如何做如下图片处理相关的知识,希望对你有一定的参考价值。
如何将灰度图片不同亮度处理成彩色图形式,并用不同颜色标记
clear;clc[x,y]=meshgrid(linspace(-1,1,512));
A=1-sqrt((x.^2+y.^2));
A=uint8(255*A);
subplot 121
imshow(A)
subplot 122
imshow(A,'colormap',[[zeros(32,2),linspace(.3,.5,32)'];jet(256);[linspace(.5,1,32);linspace(0,1,32);linspace(0,1,32)]']) 参考技术A function R=gray2rgb(img1,img2)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This Program converts a gray image ro RGB image based on the colors of the destination image. The better the destination image match with the source %
%gray image, the better the coloring will be. The program takes some time as the searching time is high. You can decrease the searching time by taking %
%only samples from the used color image but quality may decrease. U can use jittered sampling for improving running speed. %
% %
% You can use also use the attahed test images, Use the following combinations for better result nature1.jpg(as img1) and nature2.jpg(as img2) or %
% test1.jpg(as img1) and test2.jpg (as img2)
% %
% %
% Usage %
% gray2rgb('nature1.jpg','nature2.jpg');
%
% %
% Authors : Jeny Rajan %
% Chandrashekar P.S %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% img1 - Source Image (gray image)
% img2 - Selected color image for coloring the gray image.
tic
clc;
warning off;
imt=imread(img1);
ims=imread(img2);
[sx sy sz]=size(imt);
[tx ty tz]=size(ims);
if sz~=1
imt=rgb2gray(imt);
end
if tz~=3
disp ('img2 must be a color image (not indexed)');
else
imt(:,:,2)=imt(:,:,1);
imt(:,:,3)=imt(:,:,1);
% Converting to ycbcr color space
nspace1=rgb2ycbcr(ims);
nspace2= rgb2ycbcr(imt);
ms=double(nspace1(:,:,1));
mt=double(nspace2(:,:,1));
m1=max(max(ms));
m2=min(min(ms));
m3=max(max(mt));
m4=min(min(mt));
d1=m1-m2;
d2=m3-m4;
% Normalization
dx1=ms;
dx2=mt;
dx1=(dx1*255)/(255-d1);
dx2=(dx2*255)/(255-d2);
[mx,my,mz]=size(dx2);
%Luminance Comparison
disp('Please wait..................');
for i=1:mx
for j=1:my
iy=dx2(i,j);
tmp=abs(dx1-iy);
ck=min(min(tmp));
[r,c] = find(tmp==ck);
ck=isempty(r);
if (ck~=1)
nimage(i,j,2)=nspace1(r(1),c(1),2);
nimage(i,j,3)=nspace1(r(1),c(1),3);
nimage(i,j,1)=nspace2(i,j,1);
end
end
end
rslt=ycbcr2rgb(nimage)
figure,imshow(uint8(imt));
figure,imshow(uint8(rslt));
R=uint8(rslt);
toc
end 参考技术B 有啊。、、看简介
matlab矩阵缩小和放大
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为缩小后的图像矩阵 参考技术A 在MATLAB中,可以使用imresize();函数来实现矩阵缩小和放大。该函数可以实现改变图像大小、比例和像素数量的功能。
以上是关于matlab如何做如下图片处理的主要内容,如果未能解决你的问题,请参考以下文章