• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

matlab实现灰度图像伪彩色处理

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

1.基于灰度图像的伪彩色处理

代码展示:


clear all;
close all;
 
I = double(rgb2gray(imread('图片1.jpg')));
[M N] = size(I);
I2 = zeros(M, N, 3);                                 %初始化三通道
 
for x = 1 : M
   for y = 1 : N
      if I(x, y) <= 127                                 % R
          I2(x, y, 1) = 0;
      elseif I(x, y) <= 191
          I2(x, y, 1) = 4 * I(x, y) - 510;
      else
          I2(x, y, 1) = 255;
      end
       
      if I(x, y) <= 63                                  % G
          I2(x, y, 2) = 254 - 4 * I(x, y);
      elseif I(x, y) <= 127
          I2(x, y, 2) = 4 * I(x, y) - 254;
      elseif I(x, y) <= 191
          I2(x, y, 2) = 255;
      else
          I2(x, y, 2) = 1022 - 4 * I(x, y);
      end
       
      if I(x, y) <= 63                                 % B
          I2(x, y, 3) = 255;
      elseif I(x, y) <= 127
          I2(x, y, 3) = 510 - 4 * I(x, y);
      else
          I2(x, y, 3) = 0;
      end
   end
end
 
imshow(uint8(I2));
1

2.基于灰度分层的伪彩色图像

代码展示:

I=imread('图片1.jpg');
I=rgb2gray(I)
imshow(I)             %显示灰度图像
thresholds= [45 65 84 108 134 157 174 189 206 228]; %设置阈值
G2C=grayslice(I,thresholds);   % 密度分层
figure;
mymap = [0 0 0                  %黑色
    1 0 0                       %红色
    0 1 0                       %绿色
    0 0 1                       %蓝色
    1 1 0                       %黄色
    1 1 1];                     %白色
surf(peaks)
imshow(G2C,colormap(mymap));    %显示伪彩色图像

 3.基于频域的伪彩色图像处理

代码展示:

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

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
曲速未来 提醒:增加使用Delphi Packer来躲避恶意软件分类发布时间:2022-07-18
下一篇:
Delphi操作word表格发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap