1

我有以下 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 图像上,它只会给出失真的结果。如何解决?

4

3 回答 3

2

根据文档rgb2ind(单击此处):

像这样加载 rgb 图像时:

[X,map] = rgb2ind(RGB,n),医生说:

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

因此,您最好直接过滤 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')

这给出了这个:

在此处输入图像描述

于 2014-10-24T12:29:28.273 回答
2

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

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')

这是我得到的图像:

于 2014-11-01T04:29:52.140 回答
0

我认为您不需要使用gray2indand rgb2ind,看看这是否有效。

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')
于 2014-10-24T12:30:48.987 回答