【问题标题】:Applying Matlab Images Filter On RGB Images在 RGB 图像上应用 Matlab 图像过滤器
【发布时间】:2014-12-20 06:35:41
【问题描述】:

我有以下 Matlab 代码来处理两个图像,灰度图像和RGB 图像。 重点是在两个图像上应用AverageGaussianLaplacian 过滤器。

%%Clear
clear
clc
%%Reading images
gray=imread('cameraman.tif')
[gray_table gray_map]=gray2ind(gray,256)
rgb=imread('peppers.png')
[rgb_table rgb_map]=rgb2ind(rgb,256)
%%Creating filters
average=fspecial('average',3)
gaussian=fspecial('gaussian',3,0.5)
laplacian=fspecial('laplacian',0.9)
%%Applying filters
average_filterd_gray_table=imfilter(gray_table,average)
gaussian_filterd_gray_table=imfilter(gray_table,gaussian)
laplacian_filterd_gray_table=imfilter(gray_table,laplacian)
average_filterd_rgb_table=imfilter(rgb_table,average)
gaussian_filterd_rgb_table=imfilter(rgb_table,gaussian)
laplacian_filterd_rgb_table=imfilter(rgb_table,laplacian)
%%view
figure
subplot(1,4,1),imshow(gray_table,gray_map),title('Original Indexed Gray')
subplot(1,4,2),imshow(average_filterd_gray_table,gray_map),title('Average Filtered Indexed Gray')
subplot(1,4,3),imshow(gaussian_filterd_gray_table,gray_map),title('Gaussian Filtered Indexed Gray')
subplot(1,4,4),imshow(laplacian_filterd_gray_table,gray_map),title('Laplacian Filtered Indexed Gray')
figure
subplot(1,4,1),imshow(rgb_table,rgb_map),title('Original Indexed RGB')
subplot(1,4,2),imshow(average_filterd_rgb_table,rgb_map),title('Average Filtered Indexed RGB')
subplot(1,4,3),imshow(gaussian_filterd_rgb_table,rgb_map),title('Gaussian Filtered Indexed RGB')
subplot(1,4,4),imshow(laplacian_filterd_rgb_table,rgb_map),title('Laplacian Filtered Indexed RGB')

代码在灰度图像上运行良好。但在 RGB 图像上,它只会给出失真的结果。如何解决?

【问题讨论】:

  • 您是否尝试将过滤器分别应用于每个频道?或者分别到每个 HSV 通道?或者只是 HSV 的“V”通道
  • @Dan 你能写代码吗?
  • 那么建议的答案对您有帮助吗?如果是这样,请接受一个,以便线程关闭

标签: matlab plot image-editing image-effects


【解决方案1】:

您正在通过以下方式阅读图像:

rgb=imread('peppers.png')
[rgb_table rgb_map]=rgb2ind(rgb,256);

这样,您可以应用过滤器,因为您应用的方式对图像颜色进行了一些更改:

[rgb_table rgb_map]=imread('peppers.png')
average=fspecial('average',3);
gaussian=fspecial('gaussian',3,0.5);
laplacian=fspecial('laplacian',0.9);
average_filterd_rgb_table=imfilter(rgb_table,average);
gaussian_filterd_rgb_table=imfilter(rgb_table,gaussian);
laplacian_filterd_rgb_table=imfilter(rgb_table,laplacian);
subplot(2,2,1),imshow(rgb),title('Original Indexed RGB')
subplot(2,2,2),imshow(average_filterd_rgb_table),title('Average Filtered Indexed RGB')
subplot(2,2,3),imshow(gaussian_filterd_rgb_table),title('Gaussian Filtered Indexed RGB')
subplot(2,2,4),imshow(laplacian_filterd_rgb_table),title('Laplacian Filtered Indexed RGB')

这是我得到的图像:

【讨论】:

    【解决方案2】:

    根据rgb2ind的文档(点击here):

    当像这样加载 rgb 图像时:

    [X,map] = rgb2ind(RGB,n),文档说:

    注意结果图像 X 中的值是索引 colormap 映射,不应用于数学处理,例如 作为过滤操作。

    因此,您最好直接过滤 RGB 图像。以下工作正常:

    clear
    clc
    close all
    
    RGBImage = imread('peppers.png');
    
    average = fspecial('average',3);
    gaussian=fspecial('gaussian',3,0.5);
    laplacian=fspecial('laplacian',0.9);
    
    RGB_Average = imfilter(RGBImage,average);
    RGB_Gaussian= imfilter(RGBImage,gaussian);
    RGB_Laplacian = imfilter(RGBImage,laplacian);
    
    figure;
    subplot(2,2,1)
    imshow(RGBImage)
    title('Original')
    
    subplot(2,2,2)
    imshow(RGB_Average)
    title('Average')
    
    subplot(2,2,3)
    imshow(RGB_Gaussian)
    title('Gaussian')
    
    subplot(2,2,4)
    imshow(RGB_Laplacian)
    title('Laplacian')
    

    这给出了这个:

    【讨论】:

    • 好的,但是为什么它在索引灰度图像上工作得很好?
    • @Djhseen 因为gray_tablegray 相同,但rgb 并非如此。
    • 是的,对于灰度图像,RGB/真彩色查找表是 3D 和 2D 的。灰度 3 维(r、g 和 b)的所有值都相似,而 rgb 则不然。
    • 太棒了。如果答案解决了您的问题,请将其标记为已接受(左上角的复选标记)。谢谢!
    【解决方案3】:

    我认为你不需要使用gray2indrgb2ind,看看这是否有效。

    gray=imread('cameraman.tif');
    rgb=imread('peppers.png');
    %%Creating filters
    average=fspecial('average',3);
    gaussian=fspecial('gaussian',3,0.5);
    laplacian=fspecial('laplacian',0.9);
    %%Applying filters
    average_filterd_gray=imfilter(gray,average);
    gaussian_filterd_gray=imfilter(gray,gaussian);
    laplacian_filterd_gray=imfilter(gray,laplacian);
    average_filterd_rgb=imfilter(rgb,average);
    gaussian_filterd_rgb=imfilter(rgb,gaussian);
    laplacian_filterd_rgb=imfilter(rgb,laplacian);
    %%view
    figure
    subplot(2,2,1),imshow(gray),title('Original Indexed Gray')
    subplot(2,2,2),imshow(average_filterd_gray),title('Average Filtered Indexed Gray')
    subplot(2,2,3),imshow(gaussian_filterd_gray),title('Gaussian Filtered Indexed Gray')
    subplot(2,2,4),imshow(laplacian_filterd_gray),title('Laplacian Filtered Indexed Gray')
    figure
    subplot(2,2,1),imshow(rgb),title('Original Indexed RGB')
    subplot(2,2,2),imshow(average_filterd_rgb),title('Average Filtered Indexed RGB')
    subplot(2,2,3),imshow(gaussian_filterd_rgb),title('Gaussian Filtered Indexed RGB')
    subplot(2,2,4),imshow(laplacian_filterd_rgb),title('Laplacian Filtered Indexed RGB')
    

    【讨论】:

      猜你喜欢
      • 2011-09-15
      • 2011-09-15
      • 2017-05-28
      • 1970-01-01
      • 2015-06-17
      • 1970-01-01
      • 2019-07-31
      • 1970-01-01
      • 2014-08-21
      相关资源
      最近更新 更多