clear;close all
I=imread('图片1.jpg');
I=rgb2gray(I);
F=fft2(double(I));
F=fftshift(F); %中心化
sz=size(I);
fm=abs(F); %取模,傅里叶变换图像上的点的值成为了复数,取模后才能显示为图像
subplot(221),imshow(log(1+fm),[]) %对数变换 拉伸 增强显示视觉效果
threshold=60; % boundary between lower frequency and higher frequency
highF(sz(1),sz(2))=zeros;
lowF(sz(1),sz(2))=zeros;
for i=1:sz(1)
for j=1:sz(2)
d=sqrt((i-sz(1)/2)*(i-sz(1)/2)+(j-sz(2)/2)*(j-sz(2)/2));
if d<threshold
lowF(i,j)=F(i,j);
highF(i,j)=0;
else
highF(i,j)=F(i,j);
lowF(i,j)=0;
end
end
end
highF=ifftshift(highF);
highI=ifft2(highF);
lowF=ifftshift(lowF);
lowI=ifft2(lowF);
lowI=real(lowI); %reserve the real part
highI=real(highI); %reserve the real part
lowIII(sz(1),sz(2),3)=zeros;
highIII(sz(1),sz(2),3)=zeros;
for i=1:sz(1)
for j=1:sz(2)
lowIII(i,j,1)=lowI(i,j);
lowIII(i,j,2)=lowI(i,j);
lowIII(i,j,3)=lowI(i,j);
if highI(i,j)<10
% #4B0082
highIII(i,j,1)=hex2dec('4B');
highIII(i,j,2)=hex2dec('00');
highIII(i,j,3)=hex2dec('82');
elseif highI(i,j)<50
% #8B0000
highIII(i,j,1)=hex2dec('8B');
highIII(i,j,2)=hex2dec('00');
highIII(i,j,3)=hex2dec('00');
elseif highI(i,j)<100
% #FF4500
highIII(i,j,1)=hex2dec('FF');
highIII(i,j,2)=hex2dec('45');
highIII(i,j,3)=hex2dec('00');
elseif highI(i,j)<150
% #FFD700
highIII(i,j,1)=hex2dec('FF');
highIII(i,j,2)=hex2dec('D7');
highIII(i,j,3)=hex2dec('00');
elseif highI(i,j)<200
% #7A67EE
highIII(i,j,1)=hex2dec('7A');
highIII(i,j,2)=hex2dec('67');
highIII(i,j,3)=hex2dec('EE');
else
% #008B00
highIII(i,j,1)=hex2dec('00');
highIII(i,j,2)=hex2dec('8B');
highIII(i,j,3)=hex2dec('00');
end
end
end
lowIII=uint8(lowIII);
highIII=uint8(highIII);
subplot(222); imshow(lowIII); title('灰度图像');
subplot(223); imshow(highIII); title('伪彩色图相处');
4.基于位平面的伪彩色处理
代码展示:
clc;
clear all;
X=double(rgb2gray(imread('图片1.jpg')));
[h w] = size(X);
subplot(3,3,1);
imshow(X,[]);
title('原始图像');
tmp(h,w,3)=zeros;
color = ["4B","00","82","8B","00","00","FF","45","00","DD","A0","DD","7A","67","EE","00","8B","00","94","00","D3","00","00","80"];
for k=1:8
tmp(:,:,:)=0;
for i=1:h
for j=1:w
tempIndex = bitget(X(i,j),k); %灰度值分解为二进制串,然后取第k位
if tempIndex == 1
tmp(i,j,1) = hex2dec(color(3*(k-1)+1));
tmp(i,j,2) = hex2dec(color(3*(k-1)+2));
tmp(i,j,3) = hex2dec(color(3*(k-1)+3));
end
end
end
subplot(3,3,k+1);
imshow(tmp,[]);
ind = num2str(k);
imti = ['第',ind,'个位平面'];
title(imti);
end
5.基于金字塔的伪彩色处理
5.1高斯金字塔函数
function pyr = gauss_pyramid1(I,nlev)
pyr = cell(nlev,1);
pyr{1} = I;
G_LOWER = I;
f = [.05, .25, .4, .25, .05];
f = f'*f;
for l = 2:nlev
G_LOWER=G_LOWER(1:2:size(G_LOWER,1)-1,1:2:size(G_LOWER,2)-1); %降采样
pyr{l}=imfilter(G_LOWER,f,'replicate');
end
end
5.2不同分辨率金字塔上色
clear;
I = im2double(rgb2gray(imread('图片1.jpg')));
%¸高斯金字塔调用
gauss_pyr=gauss_pyramid1(I,5);
color = ['FF','00 ','FF','8B','00','8B','FF','00','00','CD','68','39','19','19','70'];
for k=1:length(gauss_pyr)
[M,N]=size(gauss_pyr{k});
for i=1:M
for j=1:N
if gauss_pyr{k}(i,j)<0.2
% 变色
gauss_pyr{k}(i,j,1)=hex2dec(color(3*k-2));
gauss_pyr{k}(i,j,2)=hex2dec(color(3*k-1));
gauss_pyr{k}(i,j,3)=hex2dec(color(3*k));
elseif gauss_pyr{k}(i,j)<0.3
% #FF00FF 洋红
gauss_pyr{k}(i,j,1)=hex2dec('FF');
gauss_pyr{k}(i,j,2)=hex2dec('00');
gauss_pyr{k}(i,j,3)=hex2dec('FF');
elseif gauss_pyr{k}(i,j)<0.4
% #FFD700金
gauss_pyr{k}(i,j,1)=hex2dec('FF');
gauss_pyr{k}(i,j,2)=hex2dec('D7');
gauss_pyr{k}(i,j,3)=hex2dec('00');
elseif gauss_pyr{k}(i,j)<0.6
% #4B0082靛青
gauss_pyr{k}(i,j,1)=hex2dec('4B');
gauss_pyr{k}(i,j,2)=hex2dec('00');
gauss_pyr{k}(i,j,3)=hex2dec('82');
elseif gauss_pyr{k}(i,j)<0.8
% #FFFF00纯黄
gauss_pyr{k}(i,j,1)=hex2dec('FF');
gauss_pyr{k}(i,j,2)=hex2dec('FF');
gauss_pyr{k}(i,j,3)=hex2dec('00');
else
% #008000纯绿
gauss_pyr{k}(i,j,1)=hex2dec('00');
gauss_pyr{k}(i,j,2)=hex2dec('80');
gauss_pyr{k}(i,j,3)=hex2dec('00');
end
请发表评论