issue in converting matrix to rgb image

I was trying to replace pixel values by their corresponding 3x3 non overlapping block. since the output matrix was of type double I converted it to uint8 type.
But when I displayed the image in the figure window it was gray instead of coloured image . I tried cat(3, gray, gray, gray) but image is still colourless.
What did I do wrong?
img= imread('C:\Users\prach\MATLAB programs\Lenna_(test_image).png');
new = blockproc(img, [10 10], fun);
new1= uint8(new);
imshow(new1)
rgbnew= cat(3, new1, new1, new1);
imshow(rgbnew)

7 Comments

I forgot to add the definitiion of fun. Here it is
fun = @(block_struct) meadian(block_struct.data);
@PRACHI Your original "Lenna_(test_image).png" is gray-scale or color image?
Remember that median() works along the first dimension by default, so median() of a 10 x 10 block is a 1 x 10 block. Unless, that is, the block is rgb, in which case median() of a 10 x 10 x 3 block is a 1 x 10 x 3 block. You probably want median(block_struct.data, [1 2])
Notice also that you are not doing a sliding window, so your output would be about 10 time smaller than the input.
@Subhadeep original image is a color image
PRACHI Sood
PRACHI Sood on 11 May 2020
Edited: PRACHI Sood on 11 May 2020
@Walter yes, reducing spatial resolution was my aim. I will try it with median(block_struct.data, [1 2]) also. Thankyou for the information.
edit: I tried the alternative but it throws the error:
"error using median (line 94)
Dimension argument must be a positive integer scalar within indexing range."
sounds like you are using a slightly older version of MATLAB that does not permit multiple dimensions. In that case as long as the image grayscale, median(block_struct.data(:))
But if you are dealing with rgb then it gets a bit messier,
median(reshape(block_struct.data,[],1,3),1)
@Walter Roberson
The median(block_struct.data(:)) worked perfectly. Thankyou very much.

Sign in to comment.

Answers (1)

You need to use imsplit() to get the individual color channels, and then process them and combine them with cat(3).

4 Comments

I tried this solution. But it gives the error :
"Unable to perform assignment because the size of the left side is 171-by-171 and the size of the right side is
128-by-128."
I tried to solve this but failed. Following is my code.
new = blockproc(img, [3 3], fun);
new1= uint8(new);
rgbim(:,:,1)= new1;
rgbim(:,:,2)= new1;
rgbim(:,:,3)= new1;
subplot(2,4,n-1), imshow(rgbim);
Attach the m-file and image. All you gave is a small partial snippet that can't run. How are you defining fun()???
See my attached blockproc() demos.
The blockproc_color_means.m demo gives this image. Is it like you want?
Apologies for the incomplete information. Attaching the .m file and the image.
Yes, the output images shown in your last comment as output of blockproc_color_means.m is my desired output. I will go through the list of demos you have provided. Thankyou very much.

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Asked:

on 3 Apr 2020

Commented:

on 12 May 2020

Community Treasure Hunt

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

Start Hunting!