1

i have the following Matlab code to manipulate two images, gray scale image, and RGB image. The point is to apply Average,Gaussian, and Laplacian filters on both images.

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

The code is working fine for the gray scale image. but on RGB image it just gives a distorted result. How to fix that?

Rashid
  • 4,326
  • 2
  • 29
  • 54
HussainBiedouh
  • 99
  • 2
  • 11

3 Answers3

2

According to the doc for rgb2ind (click here):

when loading a rgb image like so:

[X,map] = rgb2ind(RGB,n), the doc says:

Note The values in the resultant image X are indexes into the colormap map and should not be used in mathematical processing, such as filtering operations.

So you might be better off filtering directly the RGB images. The following works fine:

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

which gives this:

enter image description here

Benoit_11
  • 13,905
  • 2
  • 24
  • 35
  • Ok, but then why it worked fine with an indexed gray scale image ? – HussainBiedouh Oct 24 '14 at 13:09
  • 1
    @Djhseen because `gray_table` is the same as `gray` but it's not true for `rgb`. – Rashid Oct 24 '14 at 13:13
  • Yep, rgb/truecolor lookup tables are in 3D vs in 2D for grayscale images. All values are similar for the 3 dimensions in grayscale (r,g and b) whereas it's not the case for rgb. – Benoit_11 Oct 24 '14 at 13:16
  • great. If the answer solved your problem please mark it as accepted (check mark at the top left). Thanks! – Benoit_11 Oct 24 '14 at 14:07
2

you are reading the image in the following way:

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

With this, you can apply the filter's because the way that you applied made some changes on the image colors:

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

This is the image I get:

Mohammed Ali
  • 1,117
  • 1
  • 16
  • 31
0

I think you don't need to use gray2ind and rgb2ind, see if this works.

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')
Rashid
  • 4,326
  • 2
  • 29
  • 54