How to calculate energy of an image using Python?

3.7k Views Asked by At

I am trying to calculate the energy of an image. I want to use python. I got one solution from the website but there is a little bit confusing the person who posted the question is telling output of his program is the wrong to compare to Matlab.

I referred this link I have tried both of this code but giving the same answer.

import cv2
from pywt import dwt2
import pywt
import numpy as np
img=cv2.imread("/home/raviraj/PycharmProjects/Diabetic/SYMPTOMS/1369_right.jpeg")
im = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, (cH, cV, cD) = dwt2(im.T, 'db1')
# a - LL, h - LH, v - HL, d - HH as in matlab
Energy = (cH**2 + cV**2 + cD**2).sum()/im.size

print(Energy)

this code giving output 0.5311041623967175 and next code is

im = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
m,n = im.shape
print(im.shape)
print(m)
print(n)
cA, (cH, cV, cD) = pywt.dwt2(im,'db1')
# a - LL, h - LH, v - HL, d - HH as in matlab
cHsq = [[elem * elem for elem in inner] for inner in cH]
cVsq = [[elem * elem for elem in inner] for inner in cV]
cDsq = [[elem * elem for elem in inner] for inner in cD]
Energy = (np.sum(cHsq) + np.sum(cVsq) + np.sum(cDsq))/(m*n)
print (Energy)

this code is also giving same output 0.5311041623967174.

So I am confused is it right or wrong.

Screenshot of code output Screenshot of code output

Image attempting to calculate energy from Image attempting to calculate energy from

0

There are 0 best solutions below