How to find energy of an image
Show older comments
I want to find the energy of an image using the formula given in the attached image. Please help.
4 Comments
anushka huilgol
on 9 Jan 2016
Edited: Walter Roberson
on 9 Jan 2016
i have a doubt not related to this question
i have to find the energy of an image and i have code ready with me but i just quite cant understand the error shown by it in matlab could someone please help me??
code:
clc;
clear all;
close all;
A=imread('peppers.png');
g=rgb2gray(A);
figure, imshow(g);
x=xpy(g);
[p q]=size(g);
for i=1:m
for j=1:n
sen(i)=(p(i)*log(p(i)));
end
sen=sum(sen);
end
anushka huilgol
on 9 Jan 2016
sorry its entropy not energy
Walter Roberson
on 9 Jan 2016
You do not show the error message.
What is xpy?
size() is going to return p and q as scalars, but in your for loop you are using p as a vector. Your "for j" loop also does not refer to j at all, so there is no point in using a loop there if the code is correct.
Image Analyst
on 9 Jan 2016
If "i have a doubt not related to this question" then you should start your own question so we don't bug Dhrubajyoti Das with emails about updates to his question. In that question, I will answer with how you used the size function incorrectly.
Answers (1)
Image Analyst
on 9 May 2014
2 votes
Assuming F is the Fourier Transform, try fft2().
2 Comments
Dhrubajyoti Das
on 9 May 2014
Image Analyst
on 9 May 2014
F = fft2(grayImage) -- did you try it? Did you try anything like the code below:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 24;
%===============================================================================
% Read in a standard MATLAB gray scale demo image.
folder = fileparts(which('cameraman.tif')); % Determine where demo folder is (works with all versions).
baseFileName = 'cameraman.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', 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);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% 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]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Take the Fourier Transform.
F = fft2(grayImage);
realF = log(real(F));
% Display the original gray scale image.
subplot(2, 2, 2);
imshow(realF, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Sum up the values.
magImage = abs(F).^2;
energy = sum(magImage(:))
Categories
Find more on Read, Write, and Modify Image in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!