MATLAB: How can i calculate mean-square error(MSE) and PSNR values for the segmented image

Image Processing Toolboxmsepsnr

  • I want to find MSE and PSNR value for the segmented image
rgb = imread('ws1.jpg');
I = rgb2gray(rgb);
hy = fspecial('sobel');
hx = hy';
Iy = imfilter(double(I), hy, 'replicate');
Ix = imfilter(double(I), hx, 'replicate');
gradmag = sqrt(Ix.^2 + Iy.^2);
L = watershed(gradmag);
Lrgb = label2rgb(L);
se = strel('disk',15);
Io = imopen(I, se);%opening
Ie = imerode(I, se);
Iobr = imreconstruct(Ie, I); %opening-by-reconstruction
Ioc = imclose(Io, se); %closing
Iobrd = imdilate(Iobr, se);%closing-by-reconstruction
Iobrcbr = imreconstruct(imcomplement(Iobrd), imcomplement(Iobr));%opening-closing-by-reconstruction
Iobrcbr = imcomplement(Iobrcbr);
fgm = imregionalmax(Iobrcbr);%calculate regional maxima to obtain foreground markers
I2 = I; %superimpose
I2(fgm) = 255;
%clean the edges of the marker blobs
se2 = strel(ones(5,5));
fgm2 = imclose(fgm, se2);
fgm3 = imerode(fgm2, se2);
fgm4 = bwareaopen(fgm3,15); %shink them a bit e.g 20
I3 = I;
I3(fgm4) = 255;
%Compute Background Markers
bw = im2bw(Iobrcbr, graythresh(Iobrcbr));
D = bwdist(bw);
DL = watershed(D);
bgm = DL == 0;
%Compute the Watershed Transform of the Segmentation Function
gradmag2 = imimposemin(gradmag, bgm | fgm4);
L = watershed(gradmag2);
figure, imshow(L);
I4 = I;
I4(imdilate(L == 0, ones(3,3)) | bgm | fgm4) = 255;
figure, imshow(I4);
title('Markers and object boundaries superimposed on original image (I4)');
Lrgb = label2rgb(L, 'jet', 'w', 'shuffle');
figure, imshow(Lrgb)
title('Colored watershed label matrix (Lrgb)');

Best Answer

Attached is a PSNR demo.