To convert 3D color image to 2D .

3 views (last 30 days)
Chithra Shruthi
Chithra Shruthi on 11 Oct 2015
Commented: Walter Roberson on 12 Oct 2015
I am trying to denoise color images using mean, median, adaptive filters. But there is this error: ?? Undefined function or method 'conv2' for input arguments of type 'double' and attributes 'full 3d real'.
I need the output images in color scale. So 'rgb2gray' is ruled out. Any better options other than 'rgb2ind' and 'extracting color channels'to debug this code.

Answers (2)

Image Analyst
Image Analyst on 11 Oct 2015
Just cast to double
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Blur them
kernel = ones(7)/49;
blurredRedChannel = conv2(double(redChannel), kernel, 'same');
blurredGreenChannel = conv2(double(greenChannel), kernel, 'same');
blurredBlueChannel = conv2(double(blueChannel), kernel, 'same');

Walter Roberson
Walter Roberson on 11 Oct 2015
If you need to process a 3D array in your convolution call then you need to call convn() instead of conv2()
Image Analyst's code processes only a 2D slice of the 3D array, and so does not handle the case that you need to consider the color components linearly in your convolution.
If you want to process each 2D slice with the same 2D filter then you can call conv2 in a loop, or you could call convn with a convolution array that happens to be 2D.
  4 Comments
Image Analyst
Image Analyst on 12 Oct 2015
Then use conv2(), like I showed you. You can also use it on the intact RGB image and it will do it a color channel at a time.
Walter Roberson
Walter Roberson on 12 Oct 2015
You cannot change the dimensions of your color image to use filter2 and so on, at least not and get useful results.
You can extract individual color planes from the image and do the processing on those, the way that Image Analyst showed.
But if you insist... then reshape the color image to rows by (columns times 3) to get a 2D array that you can filter, and eventually reshape the result back to 3D. You will have some odd edge effects. But that is not a problem. You have not defined the interactions that you want between the color planes so weird edge interactions are not against any requirement you have given so far.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!