i can't reduce my MSE value in below edge detection method , please help me to solve my problem....

1 次查看(过去 30 天)
clear all;
clc;
%%Input image
img = imread ('10.jpg'); % Simple replace the 5.jpg to other no of images.
%Show input image
figure(1), imshow(img);
img = rgb2gray(img); % if the ip image is in clr convt into gray img.
img = double (img); % for conv of double to good precision... check the img values in the form 0.000 to 0.9999 by max
[m n]=size(img);
%Value for Thresholding
T_Low = 0.200;
% T_High = 0.175;
T_High = 0.275;
% T_high = 0.325;
%Gaussian Filter Coefficient
B = [2, 4, 5, 4, 2; 4, 9, 12, 9, 4;5, 12, 15, 12, 5;4, 9, 12, 9, 4;2, 4, 5, 4, 2 ];
B = 1/159.* B;
%Convolution of image by Gaussian Coefficient
A=conv2(img, B, 'same');
%Filter for horizontal and vertical direction
KGx = [-1, 0, 1; -2, 0, 2; -1, 0, 1];
KGy = [1, 2, 1; 0, 0, 0; -1, -2, -1];
%Convolution by image by horizontal and vertical filter
Filtered_X = conv2(A, KGx, 'same');
Filtered_Y = conv2(A, KGy, 'same');
%Calculate directions/orientations
arah = atan2 (Filtered_Y, Filtered_X);
arah = arah*180/pi;
pan=size(A,1);
leb=size(A,2);
%Adjustment for negative directions, making all directions positive
for i=1:pan
for j=1:leb
if (arah(i,j)<0)
arah(i,j)=360+arah(i,j);
end;
end;
end;
arah2=zeros(pan, leb);
%Adjusting directions to nearest 0, 45, 90, or 135 degree
for i = 1 : pan
for j = 1 : leb
if ((arah(i, j) >= 0 ) && (arah(i, j) < 22.5) || (arah(i, j) >= 157.5) && (arah(i, j) < 202.5) || (arah(i, j) >= 337.5) && (arah(i, j) <= 360))
arah2(i, j) = 0;
elseif ((arah(i, j) >= 22.5) && (arah(i, j) < 67.5) || (arah(i, j) >= 202.5) && (arah(i, j) < 247.5))
arah2(i, j) = 45;
elseif ((arah(i, j) >= 67.5 && arah(i, j) < 112.5) || (arah(i, j) >= 247.5 && arah(i, j) < 292.5))
arah2(i, j) = 90;
elseif ((arah(i, j) >= 112.5 && arah(i, j) < 157.5) || (arah(i, j) >= 292.5 && arah(i, j) < 337.5))
arah2(i, j) = 135;
end;
end;
end;
figure(2), imagesc(arah2); colorbar;
%Calculate magnitude
magnitude = (Filtered_X.^2) + (Filtered_Y.^2);
magnitude2 = sqrt(magnitude);
BW = zeros (pan, leb);
%Non-Maximum Supression
for i=2:pan-1
for j=2:leb-1
if (arah2(i,j)==0)
BW(i,j) = (magnitude2(i,j) == max([magnitude2(i,j), magnitude2(i,j+1), magnitude2(i,j-1)]));
elseif (arah2(i,j)==45)
BW(i,j) = (magnitude2(i,j) == max([magnitude2(i,j), magnitude2(i+1,j-1), magnitude2(i-1,j+1)]));
elseif (arah2(i,j)==90)
BW(i,j) = (magnitude2(i,j) == max([magnitude2(i,j), magnitude2(i+1,j), magnitude2(i-1,j)]));
elseif (arah2(i,j)==135)
BW(i,j) = (magnitude2(i,j) == max([magnitude2(i,j), magnitude2(i+1,j+1), magnitude2(i-1,j-1)]));
end;
end;
end;
BW = BW.*magnitude2;
figure(3), imshow(BW);
%Hysteresis Thresholding
T_Low = T_Low * max(max(BW));
T_High = T_High * max(max(BW));
T_res = zeros (pan, leb);
for i = 1 : pan
for j = 1 : leb
if (BW(i, j) < T_Low)
T_res(i, j) = 0;
elseif (BW(i, j) > T_High)
T_res(i, j) = 1;
%Using 8-connected components
elseif ( BW(i+1,j)>T_High || BW(i-1,j)>T_High || BW(i,j+1)>T_High || BW(i,j-1)>T_High || BW(i-1, j-1)>T_High || BW(i-1, j+1)>T_High || BW(i+1, j+1)>T_High || BW(i+1, j-1)>T_High)
T_res(i,j) = 1;
end;
end;
end;
edge_final = uint8(T_res.*255);
%Show final edge detection result
figure(4), imshow(edge_final);
squaredErrorImage = (double(img) - double(edge_final)) .^ 2;
mse = sum(sum(squaredErrorImage)) /((m * n));
RMSE = sqrt(mse);
PSNR = 10 * log10( 256^2 / mse);
mad = mean2(abs(double(img)-double(edge_final)));
message = sprintf('The mean square error is %.2f.\nThe PSNR = %.2f.\n THE RMSE=%.2f.\n THE mad=%.2f',mse, PSNR,RMSE,mad);
msgbox(message);
input image:
I WANT TO BE GET MSE VALUE BELOW 1

回答(1 个)

Walter Roberson
Walter Roberson 2016-7-5
Your line
img = double (img); % for conv of double to good precision... check the img values in the form 0.000 to 0.9999 by max
should be
img = im2double(img);
to be consistent with your comments. However, it appears that later you rely upon the value range being up to 255, which suggests that perhaps your comment is wrong instead of your code.
====
You construct an edge image, multiply it by 255, and then find the mse difference between that and the original image. That has no chance of having a low mse unless your original image is black and white, which it is not. Consider that you are creating white (255) at each edge position, and you are comparing that pixel to something is (brown and dark brown converted to gray). There are no bright pixels in your original image; your 255 is not going to find any close matches.
  3 个评论
Walter Roberson
Walter Roberson 2016-7-5
Your code reads 10.jpg but the image you posted is 2.jpg ?
When I read the image you posted and use rgb2gray() on it, the values range from 1 to 242. When you build your edge detected image, each location in edge_final will be either 0 or 255. For any given original pixel gray level G, the lowest mean-squared contribution from G to the constructed (0 or 255) will be if the edge_final value is 0 if G < 128 and edge_final of 255 if G >= 128. Regardless of how good the edge reconstruction is, the lowest possible mse would then be if
edge_image = 255 * (img >= 128);
and if you then calculate
mean2((double(img) - double(edge_image)).^2)
you will get a result larger than 8800.
Until you change your code so that locations in edge_image are no longer confined to being either 0 or 255, then the minimum possible mse you can get is going to be over 8800.
If you do use im2double() to make the range of values for img 0 to 1 instead of 0 to 255, and if you also remove the "255" from
edge_final = uint8(T_res.*255);
leaving
edge_final = uint8(T_res);
then at any one location (double(img) - double(edge_image) would be between -1 to +1, so the squared contribution would be between 0 and 1. In such a situation it would not be possible for the mse to exceed 1, no matter how badly you constructed your edge image. And like before, you can calculate the edge_image that would have the lowest possible mse: it would be
edge_image = (img >= 0.5); %assuming you used im2double() in creating img. For your posted image 2.jpg that would result in a mse of 0.13608065839222
.... All of which is to say that mse comparing a binary edge image to the original image is a bad measure of success in edge detection.

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by