IS this right or not about calculation of Total Variation (TV) for image y?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
mmm ssss
le 14 Jan 2012
Commenté : Image Analyst
le 19 Nov 2017
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 ?
2 commentaires
Image Analyst
le 19 Nov 2017
Yes, i and j range over the image dimensions. So i = 1 : rows and j = 1 : columns.
Réponse acceptée
Image Analyst
le 17 Jan 2012
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 commentaires
Image Analyst
le 18 Jan 2012
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.
Plus de réponses (2)
David Young
le 14 Jan 2012
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.)
0 commentaires
Image Analyst
le 14 Jan 2012
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.
0 commentaires
Voir également
Catégories
En savoir plus sur Image Processing Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!