Faster way to looping pixel by pixel to calculate entropy in an image

2024/11/18 17:47:37

I have been calculating the entropy of an image with a pixel by pixel convolution operation, and it has been working but very slowly, increasing the execution time with the kernel size.

Here is my function code, where first in the code I read an image with gdal and transform it into an array to pass it to the function.

@jit
def convolution (ArrayES, ImArray, rows, cols, kernel, option):for row in prange(rows):for col in prange(cols):Lx=max(0,col-kernel+1)Ux=min(cols,col+kernel+1)Ly=max(0,row-kernel+1)Uy=min(rows,row+kernel+1)mask=ImArray[Ly:Uy,Lx:Ux].flatten()He=0.0lenVet=mask.sizehorList=list(set(mask))if len(horList)==1 and horList.count(0)==1:ArrayES[row,col]=0.0else:T7=time.time()prob=[(mask[mask==i]).size/(lenVet*1.0) for i in horList]for p in prob:if p>0:He += -1.0*p*np.log2(p)if option==0:ArrayES[row,col]=HeN=len(horList)*1.0if N == 1:C=0else:Hmax=np.log2(N)C=He/Hmaxif option==1:ArrayES[row,col]=Cif option==2:SDL=(1-C)*CArrayES[row,col]=SDLif option==3:D = 0.0for p in prob:D += (p-(1/N))**2LMC=D*CArrayES[row,col]=LMCreturn ArrayES

The problem is when the number of kernel is >7. How can I improve it?

Answer

Similar to matlab, the key to speed up operations like this is called "vectorization". basically, removing for-loop and converting your calculations into vector and matrix operations - for each of the step, find a way to group all qualified pixels and operate on them using one call.

read this for more details

https://www.geeksforgeeks.org/vectorization-in-python/

many of the approaches are similar to vectorization in matlab

https://www.mathworks.com/help/matlab/matlab_prog/vectorization.html https://blogs.mathworks.com/videos/2014/06/04/vectorizing-code-in-matlab/

https://en.xdnf.cn/q/118647.html

Related Q&A

Google AppEngine - updating my webapp after deploy

friends! Im fairly new to web app world and I have a question regarding Google AppEngine functions. Ive installed the Launcher on my machine and signed up for the online platform (Python). Ive added …

Extract GPS coordinates from .docx file with python

I have some hectic task to do for which I need some help from python. Please see this word document.I am to extract texts and GPS coordinates from each row. There are currently over 100 coordinates in …

How to Serialize SQL data after a Join using Marshmallow? (flask extension)

I have 2 tables in SQL: class Zoo(db.Model):id = db.Column(db.Integer, primary_key=True)nome = db.Column(db.String(80), unique=True, nullable=False)idade = db.Column(db.Integer, unique=False, nullable=…

Python readline() is not reading a line with single space

I am reading a text file using readline(). My file contains below content with a space at second line:!" # $ % & When I print read values using-print("%s\n" % iso5_char)It prints-!&q…

Bisection search [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Using bisection search to determine I have posted other thread but it did not receive answers thus im trying to provide so…

What am i doing wrong with matplotlibs yticks?

My code is as follows (dont ask for the variable names, im german^^):import matplotlib.pyplot as plt import numpy as np strecke = [] zeit = []daten = open("BewegungBeschleunigung.csv")for i i…

Python- How to implement quick sort when middle element is the pivot?

There are many different versions of quickSort that pick pivot in different ways.Always pick the first element or the last element as the pivot Pick a random element as a pivot. Pick median as the pivo…

How to expose a form when a radio button is checked?

Consider the following sample html template,<html><body><input type="radio" name="x">x</input><br><input type="radio" name="y"&g…

How to generate perlin noise in pygame?

I am trying to make a survival game and I have a problem with perlin noise. My program gives me this:But I want something like islands or rivers. Heres my code: #SetUp# import pygame, sys, random pygam…

Inputting range of ports with nmap optparser

This is the scriptimport nmap import optparsedef nmapScan(tgtHost,tgtPort):nmScan = nmap.PortScanner()nmScan.scan(tgtHost,tgtPort)state=nmScan[tgtHost][tcp][int(tgtPort)][state]print "[*] " +…