Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Import Numpy As NP

The document defines functions for calculating image descriptors from neighborhoods of pixels in an image. It loads an image, defines functions to extract neighborhoods from the image, and calculates statistics like mean, standard deviation and entropy for pixel values in neighborhoods. A descriptor is defined as a vector of these statistics from different neighborhoods. A function is defined to calculate descriptors for an array of points in the image and return an array of descriptors. The functions are tested on an example image.

Uploaded by

daouibouhafs6
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Import Numpy As NP

The document defines functions for calculating image descriptors from neighborhoods of pixels in an image. It loads an image, defines functions to extract neighborhoods from the image, and calculates statistics like mean, standard deviation and entropy for pixel values in neighborhoods. A descriptor is defined as a vector of these statistics from different neighborhoods. A function is defined to calculate descriptors for an array of points in the image and return an array of descriptors. The functions are tested on an example image.

Uploaded by

daouibouhafs6
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

import numpy as np

import cv2

from scipy.stats import entropy

from math import log, e

import pandas as pd

import timeit

from numpy import array

from numpy.core.numeric import count_nonzero

from numpy.core.multiarray import zeros

img=cv2.imread("enter link to your picture",0)

print ("shape de img:")

img.shape

print("checked for shape".format(img.shape))

mat1=img[0:10,0:10]

print(mat1)

mat2=img[100:110,110:120]

print(mat2)

"""

moyenne1 = np.mean(mat1)

ecart1 = np.nanstd(mat1)

print (moyenne1)

print (ecart1)

moyenne2 = np.mean(mat2)

ecart2 = np.nanstd(mat2)
print (moyenne2)

print (ecart2)

"""

def entropy(labels, base=None):

""" Computes entropy of label distribution. """

n_labels = len(labels)

if n_labels <= 1:

return 0

value,counts = np.unique(labels, return_counts=True)

probs = counts / n_labels

n_classes = np.count_nonzero(probs)

if n_classes <= 1:

return 0

ent = 0.

# Compute entropy

base = e if base is None else base

for i in probs:

ent -= i * log(i, base)

return ent

entropie1 = entropy(mat1)

print ("ent1 :",entropie1)

entropie2 = entropy(mat2)

print ("ent2 :",entropie2)

def voisinage1(x,y,l,image):

""" Calcule le voisinage supérieur 1"""


V = image[x-l:x , y:y+l]

return V

def voisinage2(x,y,l,image):

""" Calcule le voisinage inférieur 2"""

V = image[x:x+l , y:y+l]

return V

def voisinage3(x,y,l,image):

""" Calcule le voisinage inferieur 3"""

V = image[x:x+l , y-l:y]

return V

def voisinage4(x,y,l,image):

""" Calcule le voisinage supérieur 4"""

V = image[x-l:x , y-l:y]

return V

def voisinageC(x,y,l,image):

""" Calcule le voisinage centré"""

V = image[x-l:x+l , y-l:y+l]

return V

mat1 = voisinage1 (10,10,5,img)

print (mat1)

moyenne1 = np.mean(mat1)

ecart1 = np.nanstd(mat1)

entropie1 = entropy(mat1)
print (moyenne1)

print (ecart1)

print (entropie1)

mat2= voisinageC (10,10,5,img)

print (mat2)

moyenne2 = np.mean(mat2)

ecart2 =np.nanstd(mat2)

entropie2 = entropy(mat2)

print (moyenne2)

print (ecart2)

print (entropie2)

def CalculeDescripteur(x,y,image):

""" Calcule le descripteur """

# fenetre 15

mat = voisinage1 (x,y,15,img)

moyenne1 = np.mean(mat)

ecart1 = np.nanstd(mat)

entropie1 = entropy(mat)

mat = voisinage2 (x,y,15,img)

moyenne2 = np.mean(mat)

ecart2 = np.nanstd(mat)

entropie2 = entropy(mat)

mat = voisinage3 (x,y,15,img)


moyenne3 = np.mean(mat)

ecart3 = np.nanstd(mat)

entropie3 = entropy(mat)

mat = voisinage4 (x,y,15,img)

moyenne4 = np.mean(mat)

ecart4 = np.nanstd(mat)

entropie4 = entropy(mat)

Descripteur =
[moyenne1,ecart1,entropie1,moyenne2,ecart2,entropie2,moyenne3,ecart3,entropie3,moyenne4,ec
art4,entropie4]

# fenetre 13

mat = voisinage1 (x,y,13,img)

moyenne5 = np.mean(mat)

ecart5 = np.nanstd(mat)

entropie5 = entropy(mat)

mat = voisinage2 (x,y,13,img)

moyenne6 = np.mean(mat)

ecart6 = np.nanstd(mat)

entropie6 = entropy(mat)

mat = voisinage3 (x,y,13,img)

moyenne7 = np.mean(mat)

ecart7 = np.nanstd(mat)

entropie7 = entropy(mat)
mat = voisinage4 (x,y,13,img)

moyenne8 = np.mean(mat)

ecart8 = np.nanstd(mat)

entropie8 = entropy(mat)

# fenetres Centrées

mat = voisinageC (x,y,5,img)

moyenne9 = np.mean(mat)

ecart9 = np.nanstd(mat)

entropie9 = entropy(mat)

mat = voisinageC (x,y,3,img)

moyenne10 = np.mean(mat)

ecart10 = np.nanstd(mat)

entropie10 = entropy(mat)

Descripteur =
[moyenne1,ecart1,entropie1,moyenne2,ecart2,entropie2,moyenne3,ecart3,entropie3,moyenne4,ec
art4,entropie4,moyenne5,ecart5,entropie5,moyenne6,ecart6,entropie6,moyenne7,ecart7,entropie7,
moyenne8,ecart8,entropie8,moyenne9,ecart9,entropie9,moyenne10,ecart10,entropie10]

return Descripteur

descripteur = CalculeDescripteur(50,50,img)

print("descripteur :",descripteur)

def CalculeTabDescripteur (tab,img):

TabDescripteur=[]

for i in range (0,5):

for j in range (0,5):

descripteur = CalculeDescripteur(tab[i][0],tab[j][1],img)
print("descripteur :",descripteur)

TabDescripteur.append(descripteur)

return TabDescripteur

pointsCles01=np.array([[50,30],[80,35],[120,80],[120,110],[76,140]])

tab = pointsCles01

TabDescripteurs01 = CalculeTabDescripteur(tab,img)

print("TabDescripteurs01 :",TabDescripteurs01)

You might also like