zoukankan      html  css  js  c++  java
  • Matlab DIP(瓦)ch6彩色图像处理练习

        

          这一章主要是熟悉一些彩色空间处理的基本操作,对色彩学有进一步的认识。其主要内容包括rgb图像和索引图像,各种彩色空间之间的转换,彩色变换和彩色空间滤波,包过彩色平滑和锐化,最后进行彩色边缘检测和rgb向量空间的图像分割。代码如下:

    %% 生成RGB立方体
    clc
    clear
    rgbcube(-10,-10,4);%3个参数表示观看图像视角的点坐标
    axis on;grid on;
    title('RGB立方体1');


    figure,rgbcube(10,10,4);%10,10,4是默认的坐标
    axis on;grid on;
    title('RGB立方体2');

    %其运行结果如下:


    %不过貌似也看不出视角点的坐标就是(-10,-10,4)和(10,10,4),不过2个视角在xy平面对称倒是真的


    %% 用较少的颜色来近似一幅索引图像
    clc
    clear
    %[X,map]=imread('.\images\dipum_images_ch06\Fig0604(a)(iris).tif');
    RGB=imread('.\images\dipum_images_ch06\Fig0615(d)(Iris Original).tif');
    [X map]=rgb2ind(RGB,256);%一定要采用这句,不能用上面注释掉的那句,否则后面的结果根本都一样
    imshow(X,map);%max(X(:))=255;
    title('原始索引彩色图像(256色)');
    %其运行结果如下:


    [Y,newmap]=imapprox(X,map,128);%用较少的128颜色来近似一幅索引图像
    figure,imshow(Y,newmap);
    title('索引图像(128色)');
    %其运行结果如下:



    [Y,newmap]=imapprox(X,map,64);%用较少的64颜色来近似一幅索引图像
    figure,imshow(Y,newmap);
    title('索引图像(64色)');
    %其运行结果如下:



    [Y,newmap]=imapprox(X,map,32);%用较少的32颜色来近似一幅索引图像
    figure,imshow(Y,newmap);
    title('索引图像(32色)');
    %其运行结果如下:



    [Y,newmap]=imapprox(X,map,16);%用较少的16颜色来近似一幅索引图像
    figure,imshow(Y,newmap);
    title('索引图像(16色)');
    %其运行结果如下:



    [Y,newmap]=imapprox(X,map,8);%用较少的8颜色来近似一幅索引图像
    figure,imshow(Y,newmap);
    title('索引图像(8色)');
    %其运行结果如下:



    [Y,newmap]=imapprox(X,map,4);%用较少的4颜色来近似一幅索引图像
    figure,imshow(Y,newmap);
    title('索引图像(4色)');
    %其运行结果如下:



    [Y,newmap]=imapprox(X,map,2);%用较少的2颜色来近似一幅索引图像
    figure,imshow(Y,newmap);
    title('索引图像(2色)');
    %其运行结果如下:




    %% dither使用
    clc
    clear
    RGB=imread('.\images\dipum_images_ch06\Fig0604(a)(iris).tif');
    imshow(RGB);
    title('dither真彩色(256_256_256)图像');
    %其运行结果如下:



    GRAY=rgb2gray(RGB);
    figure,imshow(GRAY);
    title('灰度图像256色');
    %其运行结果如下:



    BW=dither(rgb2gray(RGB));
    figure,imshow(BW)
    title('灰度抖动处理后图像');
    %其运行结果如下:



    [IND,map]=rgb2ind(RGB,256);%将RGB图像转换成索引图像
    %figure,imshow(IND,map);%显示索引图像
    X=dither(RGB,map);%抖动处理
    figure,imshow(X);%显示抖动图像
    title('彩色抖动处理后图像');
    %其运行结果如下:



    X=dither(RGB,jet(256));%jet函数为HSV空间的一个变种
    figure,imshow(X);
    title('HSV变换后抖动处理');
    %其运行结果如下:



    I1=ind2gray(IND,map);
    figure,imshow(I1);
    title('恢复为灰度图像');
    %其运行结果如下:



    I2=ind2rgb(IND,map);
    figure,imshow(I2);
    title('恢复为彩色图像');
    %其运行结果如下:



    %% grayslice函数使用
    clc
    clear
    I=imread('.\images\dipum_images_ch06\Fig0615(a)(Aerial Original).tif.tif');
    imshow(I);
    title('原始灰度图像(256色)');
    %其运行结果如下:



    X=grayslice(I,16);%表示从原图像I中等分16份来创建索引图像
    figure,imshow(X,jet(16));%jet(16)后是一个16*3的矩阵
    title('HSV16份灰度索引');
    %其运行结果如下:



    %% 综合运用前面的
    clc
    clear
    RGB=imread('.\images\dipum_images_ch06\Fig0619(a)(RGB_iris).tif');
    imshow(RGB);
    title('原始真彩图像');
    %其运行结果如下:



    [X1,map1]=rgb2ind(RGB,8,'nodither');%将rgb图转换成索引图
    figure,imshow(X1,map1);
    title('8色无抖动处理后索引图像');
    %其运行结果如下:



    [X2,map2]=rgb2ind(RGB,8,'dither');%将rgb图转换成索引图
    figure,imshow(X2,map2);
    title('8色有抖动处理后索引图像');%有抖动和无抖动的具体区别在哪呢?
    %其运行结果如下:



    I=rgb2gray(RGB);%转换成了灰度图像
    I1=dither(I);%将灰度图像抖动处理后就是二值图像了
    figure,imshow(I1);
    title('采用抖动处理后的灰度图像');
    %其运行结果如下:



    %% rgb2***函数使用
    clc
    clear
    RGB=imread('.\images\dipum_images_ch06\Fig0602(b)(RGB_color_cube).tif');
    imshow(RGB);
    title('rgb原图');
    %其运行结果如下:



    NTSC=rgb2ntsc(RGB);
    figure,imshow(NTSC);
    title('RGB转换成NTSC后');
    %其运行结果如下:



    RGB2=ntsc2rgb(NTSC);
    figure,imshow(RGB2);
    title('NTSC转换成RGB后');
    %其运行结果如下:



    YCBCR=rgb2ycbcr(RGB);
    figure,imshow(YCBCR);
    title('RGB转换成YCBCR后');
    %其运行结果如下:



    RGB3=ycbcr2rgb(YCBCR);
    figure,imshow(RGB3);
    title('YCBCR转换成RGB后');
    %其运行结果如下:



    HSV=rgb2hsv(RGB);
    figure,imshow(HSV);
    title('RGB转换成HSV后');
    %其运行结果如下:



    RGB4=hsv2rgb(HSV);
    figure,imshow(RGB4);
    title('HSV转换成RGB后');
    %其运行结果如下:



    HSI=rgb2hsi(RGB);
    figure,imshow(HSI);
    title('RGB转换成HSI后');
    %其运行结果如下:



    RGB5=hsi2rgb(HSI);
    figure,imshow(RGB5);
    title('HSI转换成RGB后');
    %其运行结果如下:



    %很明显同时显示9幅图用内存太大,电脑容易卡死


    %% interplq函数的使用
    clc
    clear
    z=interp1q([0 255]',[0 255]',[0:255]');%线性插值,z=[0 1 2 ... 255];
    
    
    %% spline函数的使用
    clc
    clear
    x=0:10;
    y=sin(x);
    subplot(121),plot(x,y,'+',x,y,'r');%画xy图像,并标出x和y点
    xx=0:.25:10;
    yy=spline(x,y,xx);
    subplot(122),plot(x,y,'o',xx,yy,'b');%也是画xy图像,标出x和y点,但是这中间插值了隔.25的数,只是没被标出来
    %其运行结果如下:



    %% ice函数的使用
    clc
    clear
    f=imread('.\images\dipum_images_ch06\Fig0619(a)(RGB_iris).tif');
    g=ice('image',f);%将f由指定映射交互式进行变换
    %其运行结果如下:



    f1=imread('.\images\dipum_images_ch06\Fig0617(a)(JLK Magenta).tif');
    g=ice('image',f1,'space','CMY');%修改CMY彩色空间
    %其运行结果如下:



    %% 彩色图像平滑
    clc
    clear
    fc=imread('.\images\dipum_images_ch06\Fig0604(a)(iris).tif');
    imshow(fc);
    title('原始真彩图像(平滑处理)');
    %其运行结果如下:



    fr=fc(:,:,1);%取分量r
    fg=fc(:,:,2);%取分量g
    fb=fc(:,:,3);%取分量b


    h=rgb2hsi(fc);
    H=h(:,:,1);%取分量h
    S=h(:,:,2);%取分量s
    I=h(:,:,3);%取分量i


    w=fspecial('average',15);
    I_filtered=imfilter(I,w,'replicate');
    h=cat(3,H,S,I_filtered);%cat函数是拼接数组的函数,这里将在第3维上进行拼接。
    f=hsi2rgb(h);%平滑亮度分量后的rgb图像
    f=min(f,1);%保证元素值最大为1,因为按公式转换为rgb后可能出现大于1的情况
    figure,imshow(f);
    title('仅平滑亮度分量所得到的RGB图像');
    %其运行结果如下:



    fc_filtered=imfilter(fc,w,'replicate');
    figure,subplot(121),imshow(fc_filtered);
    title('分别平滑rgb后得到的rgb图像');


    h_filtered=imfilter(h,w,'replicate');
    f=hsi2rgb(h_filtered);
    f=min(f,1);
    subplot(122),imshow(f);
    title('分别平滑hsi后得到的rgb图像');
    %其运行结果如下:



    %% 彩色图像锐化
    clc
    clear
    fc=imread('.\images\dipum_images_ch06\Fig0604(a)(iris).tif');
    subplot(121),imshow(fc);
    title('原始真彩图像(锐化处理)');


    w=fspecial('average',15);
    fc_filtered=imfilter(fc,w,'replicate');
    subplot(122),imshow(fc_filtered);
    title('分别平滑rgb3分量后');
    %其运行结果如下:



    lapmask=[1 1 1;1 -8 1;1 1 1];
    fen=imsubtract(fc_filtered,imfilter(fc_filtered,lapmask,'replicate'));
    figure,imshow(fen);
    title('拉普拉斯增强图像');
    %其运行结果如下:



    LPA=imfilter(fc,lapmask,'replicate');
    figure,subplot(121),imshow(LPA);
    title('对原图拉普拉斯算子后');


    fen=imsubtract(fc,imfilter(fc,lapmask,'replicate'));
    subplot(122),imshow(fen);
    title('用拉普拉斯算子增强真彩色');
    %其运行结果如下:



    %%
    clc
    clear
    fc=imread('.\images\dipum_images_ch06\Fig0604(a)(iris).tif');
    imshow(fc);
    title('原始真彩图像(边缘检测)');
    %其运行结果如下:



    [VG,A,PPG]=colorgrad(fc);%VG为向量梯度,A为向量角度,PPG为计算每一维梯度后合成
    figure,subplot(121),imshow(VG);
    title('在rgb空间计算梯度图像');


    subplot(122),imshow(PPG);
    title('分别在rgb计算梯度然后相加');
    %其运行结果如下:



    figure,imshow(abs(VG-PPG),[]);
    title('2种梯度的绝对值');

    %% 
    clc
    clear
    f=imread('.\images\dipum_images_ch06\Fig0627(a)(jupitermoon_original).tif');
    imshow(f);
    title('原始真彩图像(分割)');
    %其运行结果如下:



    figure,mask=roipoly(f);%roipoly为选择感兴趣的多边形
    title('交互选取采用点');

    red=immultiply(mask,f(:,:,1));%immultipy函数为2幅图像对应的元素相乘
    green=immultiply(mask,f(:,:,2));
    blue=immultiply(mask,f(:,:,3));
    g=cat(3,red,green,blue);%将对应的3个分量重新组合
    figure,imshow(g);



    %g2=immultiply(mask,f);%为什么直接这样相乘是错误的呢
    %figure,imshow(g2);


    [M,N,K]=size(g);%这里k为3
    I=reshape(g,M*N,3);%I为M*N行,3列的数组
    idx=find(mask);
    I=double(I(idx,1:3));
    [C,m]=covmatrix(I);%计算出协方差矩阵C和均值m

    d=diag(C);%方差
    sd=sqrt(d)'%标准差

    E25=colorseg('euclidean',f,25,m);%使用高斯距离进行彩色分割
    figure,subplot(221),imshow(E25);
    title('使用高斯距离25的分割');


    E50=colorseg('euclidean',f,50,m);
    subplot(222),imshow(E50);
    title('使用高斯距离50的分割');


    E75=colorseg('euclidean',f,75,m);
    subplot(223),imshow(E75);
    title('使用高斯距离75的分割');


    E100=colorseg('euclidean',f,100,m);
    subplot(224),imshow(E100);
    title('使用高斯距离100的分割');
    %其运行结果如下:



    M25=colorseg('mahalanobis',f,25,m,C);%使用马氏距离分割
    figure,subplot(221),imshow(M25);
    title('使用马氏距离25的分割');


    M50=colorseg('mahalanobis',f,50,m,C);
    subplot(222),imshow(M50);
    title('使用马氏距离50的分割');


    M75=colorseg('mahalanobis',f,75,m,C);
    subplot(223),imshow(M75);
    title('使用马氏距离75的分割');


    M100=colorseg('mahalanobis',f,100,m,C);
    subplot(224),imshow(M100);
    title('使用马氏距离100的分割');


         通过本次试验,更进一步了解了彩色空间的处理。欢迎交流!

  • 相关阅读:
    不弹出提示直接关闭页面
    orcale表解锁
    序列化和反序列化
    js 实现post传参
    简易实现 instanceOf
    简易实现virtualdom
    react中setState同步、异步问题
    CMake Qt 配置 OpenCV
    VS执行时打开cmd
    VS2019+Qt5.15.2环境配置
  • 原文地址:https://www.cnblogs.com/tornadomeet/p/2386037.html
Copyright © 2011-2022 走看看