-1

I need a little help guys in Matlab in Matrix Dimensions, I Have two images imported by imread function:

im1 = imread('1.jpg');

im2 = imread('2.jpg');

im1 is the reference image, while im2 is the Noisy image.

In the workspace window, Matlab shows the im2 Dimensions like this: 768x1024x3 while im2 displayed as: 768x1024

They are both RGB, there's no greyscale images, In fact the second image is the a compressed image (performed compression algorithm on it ) while the first image is natural JPEG Image, untouched

and for calculating MSE/PNSR for both images, the matrix dimensions must be the same.

I Will need to transform im1 dimensions to be 3d like the first image (768x1024)

I tried this functions (squeeze, reshape) and with no success

  • It's not quite clear, but one of your images is probably greyscale. Take a look at [this SO question](http://stackoverflow.com/questions/2619668/how-to-convert-a-grayscale-matrix-to-an-rgb-matrix-in-matlab) for some tips – GJStein Jul 19 '15 at 20:19
  • They are both RGB, there's no greyscale images, In fact the second image is the a compressed image (performed compression algorithm on it ) while the first image is natural JPEG Image, untouched – Ahmad Shadi Jul 19 '15 at 20:22
  • Where is the colour information when you have only one channel? 768x1024 is monochrome or indexed. Do you have a colormap for the image? – Daniel Jul 19 '15 at 20:26
  • Could you please explain more ? – Ahmad Shadi Jul 19 '15 at 20:28

2 Answers2

0

You were on the right track with repmat. Here's the correct syntax:

im2 = repmat(im2, [1 1 3]);

This says you want 1 replicate along the first dimension, 1 replicate along the second dimension, and 3 replicates along the third dimension.

A. Donda
  • 7,941
  • 1
  • 17
  • 45
  • Thank you for your replay Donda, I performed that command, and i got this results: im2 new value: 768x1024x9, That didn't fix the problem – Ahmad Shadi Jul 19 '15 at 20:14
  • Ok, then you have to apply the command to `im1`. In your question it is not clear which of your images is RGB and which is monochrome. – A. Donda Jul 19 '15 at 20:16
  • They are both RGB, im1 is the reference image, while im2 is the Noisy image. I performed this command: im1 = repmat(im1, [1 1 3]); but i got a huge number in results – Ahmad Shadi Jul 19 '15 at 20:17
  • 1
    Well you wrote that one image has the size 768x1024. That's clearly monochrome, 1 color plane. – A. Donda Jul 19 '15 at 20:19
  • I understood you question so: You have one image of size 768x1024x3 and one of size 768x1024, and you want to make the second one the same size as the first. If this is not what you want, you'll have to make your question more precise. – A. Donda Jul 19 '15 at 20:20
  • That's right Donda, I Want to make the second one the same size as the first. I apologize for my bad English verbs – Ahmad Shadi Jul 19 '15 at 20:23
  • Good. Well then the code applied to the 2d matrix should do what you want: make it the same size as the 3d matrix. – A. Donda Jul 19 '15 at 20:33
  • Yes, the code worked, but the MSE/PNSR Results was invalid MSE = 7815.79953172472 PSNR = 9.20106949279798 – Ahmad Shadi Jul 19 '15 at 20:38
  • 1
    @AhmadShadi, I have to say I had my doubts about whether replicating data is a valid approach here. But I'm afraid I don't know anything about these measures applied to image data. Maybe your question should not be how to equalize the matrix sizes, but what is the right way to compute the measures for images with different color properties. – A. Donda Jul 19 '15 at 20:41
  • If so, edit your question, make sure the title reflects the new focus, and also use helpful tags, so that the right people find your question. – A. Donda Jul 19 '15 at 20:42
  • Thank you very much Donda, I appreciate your help – Ahmad Shadi Jul 19 '15 at 20:44
0

Are you sure that both are RGB images because im2 has only one channel and it looks grayscale but it can also be a colormap image in that case try

[im2, map] = imread('im2.jpg');

and see if anything is appearing in map variable. If the image is indeed colormap image, the map variable should be of size 256 X 3.

What donda has suggested is repeating the grayscale channel 3 times to make it of size 768x1024x3. Another possibility is that noisy image was created by converting RGB image to grayscale or by taking green channel of RGB image. Verify the source of the image in that case.

About PSNR computation I have a feeling that there is some problem with your code. I have given my code below use this and see if it works. Get back to me if you face any problem.

function [Psnr_DB] = psnr(I,I_out)
I = double(I);
I_out = double(I_out);
total_error = 0;
for iterz = 1:size(I,3)
    for iterx = 1:size(I,1)
        for itery = 1:size(I,2)
            total_error = total_error + (I(iterx,itery,iterz)-I_out(iterx,itery,iterz))^2;
        end
    end
end
MSE = total_error/numel(I);
Psnr = (255^2)/MSE;
Psnr_DB = 10*log10(Psnr) %#ok<NOPRT>