IS this right or not about calculation of Total Variation (TV) for image y?

5 次查看(过去 30 天)
for a natural image it is difficult to calculate SNR so total variation (TV) will be calculated as an evaluation to noise.
for y(i,j)
TV=(sqrt(y(i,j)-y(i-1,j))^2+(y(i,j)-y(i+1,j))^2+(y(i,j)-y(i,j-1))^2+(y(i,j)-y(i,j+1))^2);
how can this implemented for a whole image ?

采纳的回答

Image Analyst
Image Analyst 2012-1-17
You can calculate your TV image, and its mean and sum, like this:
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Compute the differences between the middle pixel and the
% 4-connected neighbors.
kernel = [-1 1 0];
g = double(grayImage);
diffImageLeft = imfilter(g, kernel);
kernel = [0 1 -1];
diffImageRight = imfilter(g, kernel);
kernel = [-1 1 0]';
diffImageTop = imfilter(g, kernel);
kernel = [0 1 -1]';
diffImageBottom = imfilter(g, kernel);
TV_Image = sqrt(diffImageLeft.^2 + diffImageRight.^2 + diffImageTop.^2 + diffImageBottom.^2);
% Display the original gray scale image.
subplot(2, 2, 2);
imshow(TV_Image, []);
title('TV Image', 'FontSize', fontSize);
meanTV = mean(TV_Image(:));
sumTV = sum(TV_Image(:));
message = sprintf('The sum of the TV image pixels is %.1f\nThe mean of the TV pixels is %.5f',...
sumTV, meanTV);
msgbox(message);
  2 个评论
mmm ssss
mmm ssss 2012-1-18
can i kmow why you use these kernels , the calculation of the TV_image should be done to all pixels in grayImage and all results will be sum then.
also , i didnot understand this line
'The sum of the TV image pixels is %.1f\nThe mean of the TV pixels is %.5f',...
what this means?
Thanks
Image Analyst
Image Analyst 2012-1-18
I'm using imfilter to compute the difference between each pixel and the 4 neighbors, like you asked for. This creates another image of course since every x,y value will have a difference value. Now, we have 4 of those since you have 4 neighbors you're concerned about. It can't be done in one single call to imfilter because you're going to square them and add them together. Then I square those, sum them, and take the square root. This also gives another image. Then I add them all together or take the mean or sum.
The lines you asked about are simply stuffing the numerical values into a string. The '...' means the statement is continued on the next line. See sprintf() help for more info.

请先登录,再进行评论。

更多回答(2 个)

David Young
David Young 2012-1-14
I don't know, but an easy initial check is to see whether it gives the same answer as
var(y(:))
or possibly
var(y(:), 1)
(See the documentation for var.)

Image Analyst
Image Analyst 2012-1-14
That's just the sum of the squares of a differences of a pixel value with its 3 nearest neighbors values plus the difference of one pair not squared. How is that a variance? Who says the center pixel is the mean of the 4 neighbors? Plus you didn't divide by (N-1) even if it were the mean. And I don't know what "Total" Variance is - how does it differ from the regular variance that everyone is familiar with? And your first term:
sqrt(y(i,j)-y(i-1,j))^2
is identical to y(i,j)-y(i-1,j) since you're just taking the square root and squaring it. So you're adding a linear term to two squared terms.
I think you need to think this through some more.

Community Treasure Hunt

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

Start Hunting!

Translated by