Texture Feature Description and Matlab Realization

The texture feature describes the surface properties of the scene corresponding to the image or the image area, and is a value calculated in the image, which quantifies the characteristics of the area change. Texture analysis is the process of extracting texture features through certain image processing techniques to obtain quantitative or qualitative descriptions of textures.

Gray difference statistical method

The gray histogram of the texture area is used as the texture feature, and the image histogram is used to extract various features such as mean value, variance, energy and entropy to describe the texture. Let (x,y) be a point in the image, and the gray difference between this point and the point (x+▲x,y+▲y) with only a small distance from it is: g ▲ (x,y) = g(x , y ) - g(x+▲x,y+▲y),
where g is called grayscale difference. Assuming that all possible values ​​of the gray level difference are m levels, let the point (x, y) move on the entire screen, and count the number of times g takes each value, and thus the histogram of g ▲ can be made. From the histogram, we can get the value probability p(k) of g ▲. When the frequency p(k) of the smaller interpolation value k is larger, it reflects the roughness of the texture. When the histogram is flat, the texture is finer .
Related texture features:
insert image description here

When p(k) is relatively flat, the entropy is larger and the energy is smaller, and the more p(k) is distributed near the origin, the smaller the mean.
Calculate the statistical characteristics of the gray difference method:

>> I = imread('E:\persional\matlab\images\house.tif');
>> A = double(I);
>> [m,n] = size(A);
>> B = A;
>> C = zeros(m,n);%新建全零矩阵
>> for i=1:m-1
		for j=1:n-1
			B(i,j) = A(i+1,j+1);
			C(i,j) = abs(round(A(i,j)-B(i,j)));
		end
	end
>> h = imhist(mat2gray(C))/(m*n);%归一化灰度直方图
>> mean=0;con=0;ent=0;%均值,对比度和熵初始值为0
>> for i=1:256
		mean = mean+(i*h(i))/256;
		con = con+i*i*h(i);
		if(h(i)>0)
			ent = ent-h(i)*log2(h(i));
		end
	end
>> subplot(121),imshow(I),title('原图');
>> subplot(122),imshow(C),title('灰度差分');
>> mean

mean =

    0.0382

>> con

con =

  327.4435

>> ent

ent =

    3.6956

insert image description here

autocorrelation function method

Texture is often used to describe roughness. A texture measure with a small value indicates a fine texture, and a measure with a large value indicates a coarse texture. An autocorrelation function is usually used as a texture detection. Let the image be f(x, y), and the definition of autocorrelation function is:
insert image description here
Generally, the correlation of coarse texture area to a given deviation (ε, η) is higher than that of fine texture, so the rough shape of texture should be consistent with the expansion of autocorrelation function Proportional.

Define a custom function:

function [epsilon,eta,C] = zxcor(f,D,m,n)
for epsilon=1:D
    for eta=1:D
        temp = 0;
        fp = 0;
        for x=1:m
            for y=1:n
                if(x+epsilon-1)>m | (y+eta-1)>n
                    f1 = 0;
                else 
                    f1 = f(x,y)*f(x+epsilon-1,y+eta-1);
                end
                temp=f1+temp;
                fp = f(x,y)*f(x,y)+fp;
            end
        end
        f2(epsilon,eta)=temp;
        f3(epsilon,eta)=fp;
        C(epsilon,eta)=f2(epsilon,eta)/f3(epsilon,eta);
    end
end
epsilon=0:(D-1);%ε方向取值范围
eta=0:(D-1);%η方向取值范围

Call the zxcor() function:

>> I = imread('E:\persional\matlab\images\0.jpg');
>> J = imread("E:\persional\matlab\images\2.jpg");
>> subplot(121),imshow(I),title('图一');
>> subplot(122),imshow(J),title('图二');
>> f1 = rgb2gray(I);
>> f2 = rgb2gray(J);
>> f1 = double(f1);
>> f2 = double(f2);
>> [m1,n1] = size(f1);
>> [m2,n2] = size(f2);
>> D=20;%偏移量
>> [epsilon1,eta1,C1] = zxcor(f1,D,m1,n1);
>> [epsilon2,eta2,C2] = zxcor(f2,D,m2,n2);
>> subplot(121),mesh(epsilon1,eta1,C1);
>> xlabel('epsilon');ylabel('eta');
>> subplot(122),mesh(epsilon2,eta2,C2);
>> xlabel('epsilon');ylabel('eta');

insert image description here
insert image description here
With the increase of ε and η, it can be seen that Figure 1 changes rapidly, and its surface texture is relatively rough, while Figure 2 tends to be stable. It can be said that its texture is smooth. The larger the surface roughness, the faster the curve changes, and the smaller the roughness. The curve changes slowly.

gray level co-occurrence matrix

Since the texture is formed by the repeated appearance of the gray level distribution in the spatial position, there will be a certain gray level relationship between the pixels separated by a certain distance in the image space, that is, the spatial characteristic of the gray level in the image. Gray level co-occurrence matrix is ​​a common method to describe texture by studying the spatial correlation characteristics of gray level.
The gray histogram is the statistical result of a single pixel on the image having a certain gray level, while the gray level co-occurrence matrix is ​​obtained by counting the conditions that two pixels with a certain distance on the image have a certain gray level respectively. The gray level co-occurrence matrix of an image can reflect the comprehensive information about the direction, adjacent interval and variation range of the image gray level, and it is the basis for analyzing the local patterns of the image and their arrangement rules.
Suppose f(x,y) is a two-dimensional digital image, S is a set of pixel pairs with specific spatial continuity in the target region R, then the gray level co-occurrence matrix P satisfying a certain spatial relationship is: the molecule on the right side of the equal sign has a
insert image description here
certain The spatial relationship and the number of pixel pairs whose gray values ​​are g1 and g2 respectively, and the denominator is the total number of pixel pairs (# represents the number). The P obtained in this way is normalized. If different distances and angles are taken, different gray-level co-occurrence matrices may be obtained. During the solution process, the selected distance remains unchanged, and different angles are used, such as 0°, 45°, 90° and 135° grayscale co-occurrence matrix.
If the image is composed of pixel blocks with similar gray values, the diagonal elements of the gray-level co-occurrence matrix will have relatively large values; if the image pixel values ​​change locally, then the elements that deviate from the diagonal will have relatively large values. value.

energy:

The sum of the squares of the element values ​​of the gray level co-occurrence matrix reflects the uniformity of the image gray level distribution and the thickness of the texture. If all values ​​of the gray level co-occurrence matrix are equal, the energy is smaller; conversely, if some values ​​are large and other values ​​are small, the energy value is large. When the elements in the co-occurrence matrix are distributed intensively, the energy value is large at this time, and the large energy value represents a texture pattern with uniform and regular transformation.
insert image description here

Contrast:

It reflects the clarity of the image and the depth of the texture groove. The deeper the texture groove, the greater the contrast, and the clearer the visual effect. On the contrary, the smaller the contrast, the blurrier the visual effect. The gray scale difference, that is, the more pixel pairs with high contrast, the larger the value. The greater the value of the element away from the diagonal in the gray level co-occurrence matrix, the greater the contrast.
insert image description here

Related:

It measures the similarity of the elements of the spatial grayscale matrix in the row and column, so the correlation value reflects the local grayscale correlation in the image. When the matrix element values ​​are evenly equal, the correlation value is large; if the matrix element values ​​differ greatly, the correlation value is small. If there is a horizontal direction texture in the image, the correlation value of the horizontal direction matrix is ​​larger than that of the remaining matrices.
insert image description here

entropy:

The measure of the amount of information an image has, texture information is also information belonging to the image, and is a measure of randomness. When all elements in the co-occurrence matrix have the greatest randomness and all values ​​in the space co-occurrence matrix are almost equal, the greater the entropy. It represents the degree of non-uniformity or complexity of the texture in the image.
insert image description here

Evenness:

Reflects the roughness of the texture, the uniformity of the coarse texture is larger, and the uniformity of the fine texture is smaller.
insert image description here

Co-occurrence matrix function graycomatrix()

'GrayLimits': a vector of two elements, representing the grayscale mapping range in the image, if set to [ ], the grayscale co-occurrence matrix will use the maximum and minimum values ​​of the image I; 'NumLevels': the grayscale in the
image The normalized range determines the size of the gray-level co-occurrence matrix;
'Offset': a P×2 integer matrix;
D: the distance between the current pixel and its neighbors, 0°: [0 D], 45°: [-DD], 90°: [-D 0] and 135°: [-D -D].

>> I = imread('E:\persional\matlab\images\house.tif');
>> glcm = graycomatrix(I,'Offset',[0 2]);
>> glcm

The results show that the value of the diagonal elements of the gray-scale co-occurrence matrix of the original image is larger, indicating that there are more pixel blocks with similar gray-scale values ​​in the image.
insert image description here

Texture feature statistics function garycoprops():

stats = graycoprops(glcm,{'contrast','correlation','energy','homogeneity'}), the return value is the static property of the gray co-occurrence matrix; glcm: gray co-occurrence matrix; 'Contrast': contrast
,
return The brightness contrast between a pixel in the entire image and its adjacent pixels, the range of values ​​is [0 ~ (row number of glcm-1) 2 ];
'Correlation': correlation, how the pixel in the image is related to its adjacent pixels The metric value, the value range is [-1,1];
'Energy': energy, the square sum of pixels, the value range [0,1];
'Homogemeity': homogeneity, the distribution of elements in glcm to the diagonal The tightness of the line, the value range is [0,1], and the homogeneity of the diagonal matrix is ​​1.

Statistics of texture features of remote sensing images based on co-occurrence matrix:

>> I = imread('map1.tif');
>> HSV = rgb2hsv(I);
>> Hgray = rgb2gray(HSV);
>> glcms1 = graycomatrix(Hgray,'numlevels',64,'offset',[0 1;-1 1;-1 0;-1 -1]);%纹理特征统计值(对比度、相关性、熵、平稳度二阶矩)
>> stars = graycoprops(glcms1,{
    
    'contrast','correlation','energy','homogeneity'});
>> ga1 = glcms1(:,:,1);%0°
>> ga2 = glcms1(:,:,2);%45°
>> ga3 = glcms1(:,:,3);%90°
>> ga4 = glcms1(:,:,4);%145°
>> energya1 = 0;energya2 = 0;energya3=0;energya4=0;
>> for i=1:64
for j=1:64
energya1 = energya1+sum(ga1(i,j)^2);
energya2 = energya2+sum(ga2(i,j)^2);
energya3 = energya3+sum(ga3(i,j)^2);
energya4 = energya4+sum(ga4(i,j)^2);
j=j+1;
end
i=i+1;
end
>> s1=0;s2=0;s3=0;s4=0;s5=0;%对比度
>>> for m=1:4
s1 = stars.Contrast(1,m)+s1;
end
>>  for m=1:4
s2 = stars.Correlation(1,m)+s2;%相关性
end
>>  for m=1:4
s3 = stars.Energy(1,m)+s3;%熵
end
>>  for m=1:4
s4 = stars.Homogeneity(1,m)+s4;%平稳度
end
>> s5 = 0.000001*(energya1+energya2+energya3+energya4);%二阶矩

insert image description here
insert image description here
It can be seen from the figure that the contrast of mountains is much higher than that of cities and typhoons; the correlation between mountains and cities is close, and the correlation of typhoons is negative; the entropy value of typhoons is closer to that of cities than mountains; the contrast and stationarity parameters of mountains and towns have certain The gap can be used to distinguish mountains from oceans.

Guess you like

Origin blog.csdn.net/weixin_56260304/article/details/127885485