(Digital Image Processing MATLAB+Python) Chapter 11 Image Description and Analysis - Sections 7 and 8: Texture Description and Other Descriptions

1: Texture description

Texture description : is a technique used to describe the surface appearance of a 3D model. It maps two-dimensional images or patterns onto the surface of a three-dimensional model by applying texture mapping to give the model a more realistic and rich appearance. Texture descriptions can contain a variety of information, such as color, lighting, reflection, transparency, etc. By defining texture coordinates on the vertices or polygons of the model and mapping the texture to the corresponding position, the model can display the effect of the texture during the rendering process. Common texture description formats include:

  • Bitmap Texture : Using common image file formats (such as JPEG, PNG, etc.) as textures, you can directly obtain photos or images in the real world
  • Vector Texture : Texture described using vector graphics, which can be scaled and edited losslessly, suitable for situations where good clarity needs to be maintained
  • Normal Map : By modifying the normals of the model surface, it simulates the effect of the micro-concave-convex structure on the surface of the object to enhance the detail of the model.
  • Bump Map : simulates the effect of the bumpy structure on the surface of an object by changing the normal direction of the model surface.
  • Environment Map : used to simulate the reflection and refraction effects of the environment around an object to increase the realism of the model

(1) Joint probability matrix method

A: Definition

Joint probability matrix method : is a statistical method used for data analysis and pattern recognition . It is mainly used to analyze the joint probability distribution between multiple variables and describe the relationship between these variables by constructing a joint probability matrix . This method is based on the idea of ​​joint probability distribution and converts multidimensional data into a two-dimensional matrix. Each element in the matrix represents the joint probability value of the corresponding variable. By observing and analyzing this matrix, information such as correlations, dependencies, and patterns between different variables can be revealed. Specific steps are as follows

  • Collect sample data : First, you need to collect a sample data set containing multiple variables, such as observational data or experimental data
  • Construct a joint probability matrix : Based on the sample data, calculate the joint probability matrix between each variable. The rows and columns of the matrix represent different variables respectively, and each element in the matrix represents the joint probability of the corresponding variable value combination.
  • Analyze the matrix : By observing and analyzing the joint probability matrix, you can discover information such as patterns, correlations, and dependencies. Statistical indicators, visualization tools, or other analytical methods can be used to further interpret the matrix

Specifically, take the midpoint of the image (x, y) (x,y)(x,y ) and another point deviating from it( x + ∇ x , y + ∇ y ) (x+\nabla x,y+\nabla y)(x+x,y+y ) , let the gray value of this point pair be( f 1 , f 2 ) (f_{1},f_{2})(f1,f2),令点( x , y ) (x,y)(x,y ) moves across the entire screen to get various(f 1, f 2) (f_{1},f_{2})(f1,f2) value. Let the gray value level beLLL , thenf 1 f_{1}f1with f 2 f_{2}f2There are a total of L 2 L^{2} combinationsL2 types. For the entire screen, count each value( f 1 , f 2 ) (f_{1},f_{2})(f1,f2), then arrange it into a square matrix, and then use(f 1, f 2) (f_{1},f_{2})(f1,f2) , normalize them to the probability of occurrencep ( f 1 , f 2 ) p(f_{1},f_{2})p(f1,f2) , the square matrix is ​​called the joint probability matrix

Insert image description here

Insert image description here

B: Features based on joint probability matrix

Second moment of angle :

A S M = ∑ f 1 ∑ f 2 [ p ( f 1 , f 2 ) ] 2 A S M=\sum_{f_{1}} \sum_{f_{2}}\left[p\left(f_{1}, f_{2}\right)\right]^{2} ASM=f1f2[p(f1,f2)]2

Correlation coefficient :

C O R = ∑ f 1 ∑ f 2 ( f 1 − μ f 1 ) ( f 2 − μ f 2 ) p ( f 1 , f 2 ) σ f 1 σ f 2 μ f 1 = ∑ f 1 f 1 ∑ f 2 p ( f 1 , f 2 ) μ f 2 = ∑ f 2 f 2 ∑ f 1 p ( f 1 , f 2 ) σ f 1 2 = ∑ f 1 ( f 1 − μ f 1 ) 2 ∑ f 2 p ( f 1 , f 2 ) σ f 2 2 = ∑ f 2 ( f 2 − μ f 2 ) 2 ∑ f 1 p ( f 1 , f 2 ) \begin{array}{c}C O R=\frac{\sum_{f_{1}} \sum_{f_{2}}\left(f_{1}-\mu_{f_{1}}\right)\left(f_{2}-\mu_{f_{2}}\right) p\left(f_{1}, f_{2}\right)}{\sigma_{f_{1}} \sigma_{f_{2}}} \\\mu_{f_{1}}=\sum_{f_{1}} f_{1} \sum_{f_{2}} p\left(f_{1}, f_{2}\right) \quad \mu_{f_{2}}=\sum_{f_{2}} f_{2} \sum_{f_{1}} p\left(f_{1}, f_{2}\right) \\\sigma_{f_{1}}^{2}=\sum_{f_{1}}\left(f_{1}-\mu_{f_{1}}\right)^{2} \sum_{f_{2}} p\left(f_{1}, f_{2}\right) \sigma_{f_{2}}^{2}=\sum_{f_{2}}\left(f_{2}-\mu_{f_{2}}\right)^{2} \sum_{f_{1}} p\left(f_{1}, f_{2}\right)\end{array} COR=pf1pf2f1f2(f1mf1)(f2mf2)p(f1,f2)mf1=f1f1f2p(f1,f2)mf2=f2f2f1p(f1,f2)pf12=f1(f1mf1)2f2p(f1,f2)pf22=f2(f2mf2)2f1p(f1,f2)

Contrast :

C O N = ∑ k k 2 [ ∑ f 1 f 2 k ∈ ∣ f 1 − f 2 ∣ p ( f 1 , f 2 ) ] C O N=\sum_{k} k^{2}\left[\sum_{\substack{f_{1} \\ f_{2} \\ k \in\left|f_{1}-f_{2}\right|}} p\left(f_{1}, f_{2}\right)\right] CON=kk2 f1f2kf1f2p(f1,f2)

Reciprocal differential moment :

I D M = ∑ f 1 ∑ f 2 p ( f 1 , f 2 ) 1 + ∣ f 1 − f 2 ∣ I D M=\sum_{f_{1}} \sum_{f_{2}} \frac{p\left(f_{1}, f_{2}\right)}{1+\left|f_{1}-f_{2}\right|} IDM=f1f21+f1f2p(f1,f2)

Entropy :

E N T = − ∑ f 1 ∑ f 2 p ( f 1 , f 2 ) log ⁡ 2 p ( f 1 , f 2 ) E N T=-\sum_{f_{1}} \sum_{f_{2}} p\left(f_{1}, f_{2}\right) \log _{2} p\left(f_{1}, f_{2}\right) ENT=f1f2p(f1,f2)log2p(f1,f2)

C: program

As follows, open a grayscale image, generate a joint probability matrix and calculate parameters. It can be found that after smoothing, the image contrast is reduced, the autocorrelation is enhanced, and the angular second-order moment and reciprocal difference moment are increased.

Insert image description here


matlab

clear,clc,close all;
Iorigin=imread('morphplane.jpg');
Iorigin=255-Iorigin;
Irotate=imrotate(Iorigin,15,'bilinear');
Iresize=imresize(Iorigin,0.4,'bilinear');
Imirror=fliplr(Iorigin);

bwo=im2bw(Iorigin);
bwr=im2bw(Irotate);
bws=im2bw(Iresize);
bwm=im2bw(Imirror);

huo=invmoments(bwo);
hur=invmoments(bwr);
hus=invmoments(bws);
hum=invmoments(bwm);   

    
function Hu=invmoments(bw)
[N,M]=size(bw);
M00=0;
M10=0;
M01=0;
for x=1:M
    for y=1:N
        M00=M00+bw(y,x);
        M10=M10+x*bw(y,x);
        M01=M01+y*bw(y,x);
    end
end
centerx=M10/M00;
centery=M01/M00;
u02=0;u20=0;u11=0;u21=0;u12=0;u30=0;u03=0;
for x=1:M
    for y=1:N
        u20=u20+bw(y,x)*(x-centerx)^2;
        u02=u02+bw(y,x)*(y-centery)^2;
        u11=u11+bw(y,x)*(x-centerx)*(y-centery);
        u30=u30+bw(y,x)*(x-centerx)^3;
        u03=u03+bw(y,x)*(y-centery)^3;
        u21=u21+bw(y,x)*(x-centerx)^2*(y-centery);
        u12=u12+bw(y,x)*(y-centery)^2*(x-centerx);
    end
end
n20=u20/(M00^2);n02=u02/(M00^2);n11=u11/(M00^2);
n21=u21/(M00^2.5);n12=u12/(M00^2.5);
n03=u03/(M00^2.5);n30=u30/(M00^2.5);
Hu(1)=n20+n02;
Hu(2)=(n20-n02)^2+4*n11^2;
Hu(3)=(n30-3*n12)^2+(3*n21-n03)^2;
Hu(4)=(n30+n12)^2+(n21+n03)^2;
Hu(5)=(n30-3*n12)*(n30+n12)*((n30+n12)^2-3*(n21+n03)^2)...
    +(3*n21-n03)*(n21+n03)*(3*(n30+n12)^2-(n21+n03)^2);
Hu(6)=(n20-n02)*((n03+n12)^2-(n21+n03)^2)+4*n11*(n30+n12)*(n21+n03);
Hu(7)=(3*n21-n03)*(n30+n12)*((n30+n12)^2-3*(n21+n03)^2)...
    -(n30-3*n12)*(n21+n03)*(3*(n30+n12)^2-(n21+n03)^2);
end


python

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

def invmoments(bw):
    N, M = bw.shape
    M00 = np.sum(bw)
    M10 = np.sum(np.multiply(np.arange(M), bw))
    M01 = np.sum(np.multiply(np.arange(N).reshape(N, 1), bw))

    centerx = M10 / M00
    centery = M01 / M00

    u02 = 0
    u20 = 0
    u11 = 0
    u21 = 0
    u12 = 0
    u30 = 0
    u03 = 0

    for x in range(M):
        for y in range(N):
            u20 += bw[y, x] * (x - centerx) ** 2
            u02 += bw[y, x] * (y - centery) ** 2
            u11 += bw[y, x] * (x - centerx) * (y - centery)
            u30 += bw[y, x] * (x - centerx) ** 3
            u03 += bw[y, x] * (y - centery) ** 3
            u21 += bw[y, x] * (x - centerx) ** 2 * (y - centery)
            u12 += bw[y, x] * (y - centery) ** 2 * (x - centerx)

    n20 = u20 / (M00 ** 2)
    n02 = u02 / (M00 ** 2)
    n11 = u11 / (M00 ** 2)
    n21 = u21 / (M00 ** 2.5)
    n12 = u12 / (M00 ** 2.5)
    n03 = u03 / (M00 ** 2.5)
    n30 = u30 / (M00 ** 2.5)

    Hu = np.zeros(7)
    Hu[0] = n20 + n02
    Hu[1] = (n20 - n02) ** 2 + 4 * n11 ** 2
    Hu[2] = (n30 - 3 * n12) ** 2 + (3 * n21 - n03) ** 2
    Hu[3] = (n30 + n12) ** 2 + (n21 + n03) ** 2
    Hu[4] = (n30 - 3 * n12) * (n30 + n12) * ((n30 + n12) ** 2 - 3 * (n21 + n03) ** 2) + \
            (3 * n21 - n03) * (n21 + n03) * (3 * (n30 + n12) ** 2 - (n21 + n03) ** 2)
    Hu[5] = (n20 - n02) * ((n03 + n12) ** 2 - (n21 + n03) ** 2) + \
            4 * n11 * (n30 + n12) * (n21 + n03)
    Hu[6] = (3 * n21 - n03) * (n30 + n12) * ((n30 + n12) ** 2 - 3 * (n21 + n03) ** 2) - \
            (n30 - 3 * n12) * (n21 + n03) * (3 * (n30 + n12) ** 2 - (n21 + n03) ** 2)

    return Hu

Iorigin = np.array(Image.open('morphplane.jpg').convert('L'))
Iorigin = 255 - Iorigin
Irotate = Image.fromarray(Iorigin).rotate(15, resample=Image.BILINEAR)
Iresize = Image.fromarray(Iorigin).resize((int(Iorigin.shape[1] * 0.4), int(Iorigin.shape[0] * 0.4)),
                                         resample=Image.BILINEAR)
Imirror = np.fliplr(Iorigin)

bwo = np.array(Image.fromarray(Iorigin).convert('1'))
bwr = np.array(Irotate.convert('1'))
bws = np.array(Iresize.convert('1'))
bwm = np.array(Image.fromarray(Imirror).convert('1'))

huo = invmoments(bwo)
hur = invmoments(bwr)
hus = invmoments(bws)
hum = invmoments(bwm)

plt.figure(figsize=(12, 4))

plt.subplot(141)
plt.imshow(Iorigin, cmap='gray')
plt.title('Original)

(2) Grayscale difference statistics method

A: Definition

Grayscale difference statistics method : It is a method used for image texture analysis. It describes the texture characteristics of the image by calculating the grayscale difference between pixels in the image. This method is widely used in fields such as image processing, image classification and target recognition. Specific steps are as follows

  • Determine the window size : First, you need to determine the window size. The size of the window will affect the accuracy and calculation amount of the calculation results.
  • Calculate the grayscale difference matrix : For each pixel in the image, calculate the difference between it and neighboring pixels based on the grayscale value of the pixels in the surrounding window. Commonly used differential methods include horizontal differential, vertical differential, diagonal differential, etc.
  • Statistical gray-scale difference matrix : count the frequency or probability of each gray-scale difference value in the gray-scale difference matrix, and obtain the gray-scale difference statistical histogram
  • Extract texture features : Based on the grayscale difference statistical histogram, a series of texture features can be extracted, such as mean, variance, energy, contrast, correlation, etc.
  • Classification and recognition : Use extracted texture features for classification and recognition tasks. Common methods include support vector machine (SVM), artificial neural network (ANN), etc.

B: Parameters describing image features

Contrast :

C O N = ∑ i i 2 p g ( i ) C O N=\sum_{i} i^{2} p_{g}(i) CON=ii2pg(i)

Second moment in angular direction :

A S M = ∑ [ p g ( i ) ] 2 A S M=\sum\left[p_{g}(i)\right]^{2} ASM=[pg(i)]2

Entropy :

E N T = − ∑ i p g ( i ) lg ⁡ p g ( i ) E N T=-\sum_{i} p_{g}(i) \lg p_{g}(i) ENT=ipg(i)lgpg(i)

Average :

M E A N = 1 m ∑ i i p g ( i ) M E A N=\frac{1}{m} \sum_{i} i p_{g}(i) MEAN=m1iipg(i)

(3) Stroke length statistical method

A: Definition

Run-length statistical method : It is a commonly used lossless data compression algorithm and can also be used for texture feature analysis in image processing. In run-length statistics, consecutive sequences of identical pixel values ​​are encoded to reduce data storage space or transmission bandwidth. Specific steps are as follows

  • Data scanning : Scan the image line by line or column by column, and record the location of each change in pixel value.
  • Run-length encoding : Encode a continuous sequence of pixel values ​​according to the position where the pixel value changes, and record the starting position and length of each continuous sequence.
  • Coding result storage : The coding results are stored in a certain format, usually using a two-dimensional array or list to save the starting position and length of each continuous sequence.
  • Decoding process : Decode according to the encoding result, and restore the original image by reconstructing a continuous sequence of pixel values

For example, in the figure below, 4 gray levels, for 2 directions (0°, 45°), define the corresponding stroke length matrix MRL M_{RL}MRL

Insert image description here

B: Features using grayscale run length

Short stroke compensation :

S R E = ∑ f ∑ n ( M R L ( f , n ) n 2 ) ∑ f ∑ n M R L ( f , n ) S R E=\frac{\sum_{f} \sum_{n}\left(\frac{M_{R L}(f, n)}{n^{2}}\right)}{\sum_{f} \sum_{n} M_{R L}(f, n)} SRE=fnMRL(f,n)fn(n2MRL(f,n))

Long stroke compensation :

L R E = ∑ f ∑ n ( M R L ( f , n ) n 2 ) ∑ f ∑ n M R L ( f , n ) L R E=\frac{\sum_{f} \sum_{n}\left(M_{R L}(f, n) n^{2}\right)}{\sum_{f} \sum_{n} M_{R L}(f, n)} L RE=fnMRL(f,n)fn(MRL(f,n)n2)

Gray level non-uniformity :

G L D = ∑ f [ ∑ n ( M R L ( f , n ) ) ] 2 ∑ f ∑ n M R L ( f , n ) G L D=\frac{\sum_{f}\left[\sum_{n}\left(M_{R L}(f, n)\right)\right]^{2}}{\sum_{f} \sum_{n} M_{R L}(f, n)} GLD=fnMRL(f,n)f[n(MRL(f,n))]2

Stroke length non-uniformity :

R L D = ∑ n [ ∑ f ( M R L ( f , n ) ) ] 2 ∑ f ∑ n M R L ( f , n ) R L D=\frac{\sum_{n}\left[\sum_{f}\left(M_{R L}(f, n)\right)\right]^{2}}{\sum_{f} \sum_{n} M_{R L}(f, n)} RLD=fnMRL(f,n)n[f(MRL(f,n))]2

Travel percentage :

R P = ∑ f ∑ n M R L ( f , n ) N R P=\frac{\sum_{f} \sum_{n} M_{R L}(f, n)}{N} RP=NfnMRL(f,n)

C: program

As follows, program to calculate the 45° direction travel length matrix and parameters of the bark image

Insert image description here

Insert image description here


matlab

% f=[0 1 2 3 0 1 2;1 2 3 0 1 2 3;2 3 0 1 2 3 0;3 0 1 2 3 0 1;0 1 2 3 0 1 2;1 2 3 0 1 2 3;2 3 0 1 2 3 0];
% f=[0 1 2 3 0 1 2;1 2 3 0 1 2 3;2 3 0 1 2 3 0;3 0 1 2 3 0 1];
% f=[0 1 2 3; 1 2 3 0 ;2 3 0 1 ;3 0 1 2;0 1 2 3;1 2 3 0;2 3 0 1];
f=rgb2gray(imread('texture1.bmp'));
% f=rgb2gray(imread('smoothtexture.jpg'));
f=f+1;
top=max(f(:));
[h,w]=size(f);
N=min(h,w);
MRL=zeros(top,N);
length=1;
for x=1:w
    newx=x;
    newy=1;
    for y=1:min(h,x)
        oldx=newx;
        oldy=newy;
        newx=newx-1;
        newy=y+1;
        if newx>0 && newy<=h && f(newy,newx)==f(oldy,oldx)
            length=length+1;
        else
            MRL(f(oldy,oldx),length)=MRL(f(oldy,oldx),length)+1;
            length=1;
        end
    end
end
for y=2:h
    newx=w;
    newy=y;
    for x=w:-1:1
        oldx=newx;
        oldy=newy;
        newx=x-1;
        newy=oldy+1;
        if newx>0 && newy<=h && f(newy,newx)==f(oldy,oldx)
            length=length+1;
        else
            MRL(f(oldy,oldx),length)=MRL(f(oldy,oldx),length)+1;
            length=1;
            break;
        end
    end
end
SRE=0;LRE=0;GLD=0;RLD=0;RP=0;total=0;GLDp=0;
for n=1:N
    RLDp=0;    
    for f=1:top
        if MRL(f,n)~=0
            total=total+MRL(f,n);
            SRE=SRE+MRL(f,n)/(n^2);
            LRE=LRE+MRL(f,n)*(n^2);
            RLDp=RLDp+MRL(f,n);
            RP=RP+MRL(f,n);
        end
    end
    RLD=RLD+RLDp^2;
end
for f=1:top
    GLDp=0;
    for n=1:N
        if MRL(f,n)~=0
            GLDp=GLDp+MRL(f,n);
        end
    end
    GLD=GLD+GLDp^2;
end
SRE=SRE/total;
LRE=LRE/total;
RLD=RLD/total;
RP=RP/(h*w);
GLD=GLD/total;

python

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt

# 读取图像
image = Image.open('morphplane.jpg')
Iorigin = np.array(image)

# 转换为灰度图像
f = np.array(image.convert('L'))

f = f + 1
top = np.max(f)
h, w = f.shape
N = min(h, w)
MRL = np.zeros((top, N))
length = 1

for x in range(w):
    newx = x
    newy = 0
    for y in range(min(h, x+1)):
        oldx = newx
        oldy = newy
        newx -= 1
        newy += 1
        if newx >= 0 and newy < h and f[newy, newx] == f[oldy, oldx]:
            length += 1
        else:
            MRL[f[oldy, oldx], length] += 1
            length = 1

for y in range(1, h):
    newx = w
    newy = y
    for x in range(w-1, -1, -1):
        oldx = newx
        oldy = newy
        newx -= 1
        newy += 1
        if newx >= 0 and newy < h and f[newy, newx] == f[oldy, oldx]:
            length += 1
        else:
            MRL[f[oldy, oldx], length] += 1
            length = 1
            break

SRE = 0
LRE = 0
GLD = 0
RLD = 0
RP = 0
total = 0
GLDp = 0

for n in range(N):
    RLDp = 0
    for i in range(1, top+1):
        if MRL[i, n] != 0:
            total += MRL[i, n]
            SRE += MRL[i, n] / (n**2)
            LRE += MRL[i, n] * (n**2)
            RLDp += MRL[i, n]
            RP += MRL[i, n]
    RLD += RLDp**2

for i in range(1, top+1):
    GLDp = 0
    for n in range(N):
        if MRL[i, n] != 0:
            GLDp += MRL[i, n]
    GLD += GLDp**2

SRE /= total
LRE /= total
RLD /= total
RP /= (h * w)
GLD /= total

# 图像展示部分
plt.subplot(121)
plt.imshow(Iorigin)
plt.title('Original Image')
plt.axis('off')

plt.subplot(122)
plt.imshow(f, cmap='gray')
plt.title('Grayscale Image')
plt.axis('off')

plt.show()

(4) LBP characteristics

A: Definition

LBP feature : is a method used for image texture description and feature extraction. It defines a local neighborhood on each pixel of the image , compares the pixels in the neighborhood with the center pixel, and generates a binary code based on the comparison result. The LBP feature can capture the frequency and distribution of different texture patterns in the image. The calculation steps are as follows

  • Select a pixel as the center pixel
  • Determine a neighborhood area centered on the central pixel, usually a square or circular area centered on the central pixel
  • Compare the gray value of each pixel in the neighborhood with the center pixel. If the pixel value is greater than or equal to the gray value of the center pixel, the position is marked as 1, otherwise it is marked as 0.
  • Convert the binary encoding to a decimal number to obtain the LBP value of the center pixel
  • Repeat steps 1-4 through each pixel of the image to calculate the LBP features of the entire image.

Through the extraction of LBP features, we can obtain the following information

  • Texture mode : LBP features can describe different texture modes in the image, such as smooth, rough, delicate, etc.
  • Texture structure : LBP features can reflect the distribution and arrangement of textures in the image, such as grid, spot, etc.
  • Texture statistics : By counting the frequency and distribution of LBP values ​​in the entire image or local area, some texture statistics can be obtained, such as histogram, mean, variance, etc.

The LBP feature has the following advantages

  • Simple and effective : The calculation of LBP features is simple and fast, and has strong robustness to image illumination changes and noise.
  • Does not rely on color information : LBP features are calculated using only grayscale values, are not affected by color changes, and are suitable for both color and grayscale images.
  • Widely applicable : LBP features can be used in various application fields, such as image classification, texture recognition, face recognition, pedestrian detection, etc.

B:Program

As follows, calculate the LBP feature map of the image below

Insert image description here


matlab

clear,clc,close all;
image=imread('lena.bmp');
[N,M]=size(image);
lbp=zeros(N,M);
for j=2:N-1
    for i=2:M-1
        neighbor=[j-1 i-1;j-1 i;j-1 i+1;j i+1;j+1 i+1;j+1 i;j+1 i-1;j i-1];
        count=0;
        for k=1:8
            if image(neighbor(k,1),neighbor(k,2))>image(j,i)
                count=count+2^(8-k);
            end
        end
        lbp(j,i)=count;
    end
end
lbp=uint8(lbp);
figure,imshow(lbp),title('LBP特征图');
% imwrite(lbp,'lenalbp.jpg');
subim=lbp(1:8,1:8);
imhist(subim),title('第一个子区域直方图');
                

python

import cv2
import numpy as np
import matplotlib.pyplot as plt

# 读取图像
image = cv2.imread('lena.bmp', 0)

N, M = image.shape
lbp = np.zeros((N, M), dtype=np.uint8)

for j in range(1, N-1):
    for i in range(1, M-1):
        neighbor = [
            [j-1, i-1], [j-1, i], [j-1, i+1],
            [j, i+1], [j+1, i+1], [j+1, i],
            [j+1, i-1], [j, i-1]
        ]
        count = 0
        for k in range(8):
            if image[neighbor[k][0], neighbor[k][1]] > image[j, i]:
                count += 2 ** (7 - k)
        lbp[j, i] = count

plt.imshow(lbp, cmap='gray')
plt.title('LBP特征图')
plt.axis('off')
plt.show()

subim = lbp[:8, :8]
plt.hist(subim.flatten(), bins=256, range=[0, 256])
plt.title('第一个子区域直方图')
plt.show()

2: Other descriptions

(1) Gradient direction histogram

A: Definition

Gradient line histogram : It is a method for image feature description, mainly used in computer vision tasks such as image classification, object recognition and target detection. It calculates the gradient direction of each pixel in the image, counts the distribution of the gradient direction , and constructs a histogram to represent the characteristics of the image. The calculation steps are as follows

  • Convert the image to grayscale if it is not grayscale
  • Smooth the image, such as using a Gaussian filter, to reduce the impact of noise
  • Calculate the horizontal (x direction) and vertical (y direction) gradient of each pixel. Operators such as Sobel and Prewitt can be used for gradient calculation.
  • For each pixel, calculate the gradient magnitude and gradient direction based on its horizontal and vertical gradients.
  • Divide the image into several areas or blocks, and perform statistics on the gradient direction of the pixels in each area.
  • Construct a gradient direction histogram based on the statistical results. Generally, the gradient direction space is divided into several fixed angle ranges, for example, 0°~180° is divided into 8 intervals.
  • The number of gradient directions in each area is used as the value of the histogram to obtain the final gradient direction histogram.

The gradient direction histogram can extract edge and texture information in the image, and encode and represent this information. By counting the distribution of different gradient directions, the structure and texture characteristics of the image can be described. In image classification and object recognition tasks, gradient direction histograms are usually used as image features, and then machine learning algorithms are used for training and classification. It has the following advantages

  • Not affected by illumination changes: the gradient direction histogram only relies on the gradient information of the pixel and is not affected by illumination changes
  • Sensitive to local changes: The gradient direction histogram counts the gradient direction distribution in a local area, so it is very sensitive to the detection of local texture features.
  • Controllable feature dimensions: By adjusting the number of intervals that divide the gradient direction space, the feature dimensions of the gradient direction histogram can be controlled to adapt to the needs of different tasks.

B:Program

As follows, the HOG features of the statistical image

Insert image description here


matlab

clear,clc,close all;
Image=double(imread('lena.bmp'));
[N,M]=size(Image);
Image=sqrt(Image);
Hy=[-1 0 1];       
Hx=Hy';            
Gy=imfilter(Image,Hy,'replicate');    
Gx=imfilter(Image,Hx,'replicate');   
Grad=sqrt(Gx.^2+Gy.^2);             
Phase=zeros(N,M);   
Eps=0.0001;
for i=1:M
     for j=1:N 
         if abs(Gx(j,i))<Eps && abs(Gy(j,i))<Eps  
             Phase(j,i)=270;
         elseif abs(Gx(j,i))<Eps && abs(Gy(j,i))>Eps 
             Phase(j,i)=90;
         else
             Phase(j,i)=atan(Gy(j,i)/Gx(j,i))*180/pi; 
             if Phase(j,i)<0                                        
                 Phase(j,i)=Phase(j,i)+180; 
             end             
         end
     end
end
step=8;               
K=9;               
angle=180/K;       
Cell=cell(1,1);              
Celli=1;                      
Cellj=1;
for i=1:step:M         
    Cellj=1;
    for j=1:step:N     
        Gtmp=Grad(j:j+step-1,i:i+step-1);
        Gtmp=Gtmp/sum(sum(Gtmp));       
        Hist=zeros(1,K);              
        for x=1:step
            for y=1:step
                ang=Phase(j+y-1,i+x-1); 
                if ang<=180
                    Hist(floor(ang/angle)+1)=Hist(floor(ang/angle)+1)+Gtmp(y,x);   
                end
            end
        end
        Cell{
    
    Cellj,Celli}=Hist;      
        Cellj=Cellj+1;               
    end
    Celli=Celli+1;                   
end

[CellN,CellM]=size(Cell);
feature=cell(1,(CellM-1)*(CellN-1));
for i=1:CellM-1
   for j=1:CellN-1           
        f=[];
        f=[f Cell{
    
    j,i}(:)' Cell{j,i+1}(:)' Cell{
    
    j+1,i}(:)' Cell{j+1,i+1}(:)'];
        f=f./sum(f);
        feature{
    
    (i-1)*(CellN-1)+j}=f;
   end
end

python

clear,clc,close all;
Image=double(imread('lena.bmp'));
[N,M]=size(Image);
Image=sqrt(Image);
Hy=[-1 0 1];       
Hx=Hy';            
Gy=imfilter(Image,Hy,'replicate');    
Gx=imfilter(Image,Hx,'replicate');   
Grad=sqrt(Gx.^2+Gy.^2);             
Phase=zeros(N,M);   
Eps=0.0001;
for i=1:M
     for j=1:N 
         if abs(Gx(j,i))<Eps && abs(Gy(j,i))<Eps  
             Phase(j,i)=270;
         elseif abs(Gx(j,i))<Eps && abs(Gy(j,i))>Eps 
             Phase(j,i)=90;
         else
             Phase(j,i)=atan(Gy(j,i)/Gx(j,i))*180/pi; 
             if Phase(j,i)<0                                        
                 Phase(j,i)=Phase(j,i)+180; 
             end             
         end
     end
end
step=8;               
K=9;               
angle=180/K;       
Cell=cell(1,1);              
Celli=1;                      
Cellj=1;
for i=1:step:M         
    Cellj=1;
    for j=1:step:N     
        Gtmp=Grad(j:j+step-1,i:i+step-1);
        Gtmp=Gtmp/sum(sum(Gtmp));       
        Hist=zeros(1,K);              
        for x=1:step
            for y=1:step
                ang=Phase(j+y-1,i+x-1); 
                if ang<=180
                    Hist(floor(ang/angle)+1)=Hist(floor(ang/angle)+1)+Gtmp(y,x);   
                end
            end
        end
        Cell{
    
    Cellj,Celli}=Hist;      
        Cellj=Cellj+1;               
    end
    Celli=Celli+1;                   
end

[CellN,CellM]=size(Cell);
feature=cell(1,(CellM-1)*(CellN-1));
for i=1:CellM-1
   for j=1:CellN-1           
        f=[];
        f=[f Cell{
    
    j,i}(:)' Cell{j,i+1}(:)' Cell{
    
    j+1,i}(:)' Cell{j+1,i+1}(:)'];
        f=f./sum(f);
        feature{
    
    (i-1)*(CellN-1)+j}=f;
   end
end

(2) Haar-like features

A: Definition

Haar-like feature : It is a feature description method used for image classification and target detection . It was first proposed in the Viola-Jones face detection algorithm and has been widely used. It is based on the gray value difference of the image to represent the characteristics of the local image area. The calculation steps are as follows

  • A template that defines a rectangle that can be a contiguous area horizontally, vertically, or diagonally.
  • The template is placed at different positions and scales of the image, and the difference in grayscale values ​​of the white (highlight) and black (shadow) areas within the template is calculated.
  • Calculate eigenvalues ​​based on gray value differences. Common Haar-like features include: the difference between two adjacent rectangles, the sum of three adjacent rectangles, etc.

The main idea of ​​Haar-like features is to capture the local texture and edge information in the image by calculating the difference in gray value of different rectangular areas. Specifically, the difference in gray value between bright and dark areas can be used to represent edges in the image, while the difference in gray value between adjacent areas can be used to represent texture in the image. Its advantages are as follows

  • Simple : Haar-like feature calculation is simple and efficient, suitable for real-time applications.
  • Translation invariance : Haar-like features are invariant to image translation, that is, the feature can detect the same pattern at any position in the image.
  • Multi-scale adaptability : Haar-like features can adapt to targets of different sizes by changing the size of the template

Its disadvantages are as follows

  • Limitations : Haar-like features are mainly suitable for describing low-level visual features such as edges and textures, and are difficult to effectively represent complex shapes and texture structures.
  • Poor invariance : Haar-like features have poor invariance to factors such as lighting, rotation, and scale changes.

B:Program

as follows

Insert image description here


matlab

clear,clc,close all;
Image=double(imread('carphone.jpg'));
[N,M]=size(Image);
win=24;

python

import cv2
import matplotlib.pyplot as plt

# Read image
image = cv2.imread('carphone.jpg', cv2.IMREAD_GRAYSCALE)
image = image.astype(float)

# Get image size
N, M = image.shape

# Define window size
win = 24

# Display image
plt.imshow(image, cmap='gray')
plt.axis('off')
plt.show()

Guess you like

Origin blog.csdn.net/qq_39183034/article/details/132656689