Extracting co-occurrence matrix features

Source: Internet
Author: User
Calculate the gray level co-occurrence matrix




  
The co-occurrence matrix is defined by the joint probability density of pixels in two locations. It not only reflects the brightness distribution, but also reflects the location distribution between pixels with the same brightness or close to the brightness, is a second-order statistical feature related to image brightness changes. It is the basis for defining a set of texture features.

  
The gray level co-occurrence matrix reflects the comprehensive information of the gray level of the image on the direction, adjacent interval, and change amplitude. If f (x, y) is a two-dimensional digital image with the size of m × n and the gray level of NG, the gray level co-occurrence matrix meeting a certain spatial relationship is:

  
P (I, j) = # {(x1, Y1), (X2, Y2) ε m × n | f (x1, Y1) = I, F (X2, Y2) = J}

  
Where # (x) indicates the number of elements in set X. Obviously, P is a ng × ng matrix. If the distance between (x1, Y1) and (X2, Y2) is D, if the angle between the two and the X-axis is θ, a gray-scale co-occurrence matrix P (I, j, D, θ) with various spacing and angles can be obtained ).


Class cglcm <br/>{< br/> Public: <br/> cglcm (void); <br/> virtual ~ Cglcm (void); <br/> Public: <br/> int * m_pmat1; <br/> int * m_pmat2; <br/> int * m_pmat3; <br/> int * m_pmat4; <br/> protected: <br/> int * m_pline [4] [256]; <br/> int m_nmin; <br/> int m_nmax; <br/> int m_nsum; <br/> Public: <br/> // calculate the co-occurrence matrix <br/> // parameter: <br/> // 1. pimagedata: Image Data Pointer, single channel, 8 bits. <Br/> // 2. nleft, ntop, nwidth, and nheight: Calculate the region of the co-occurrence matrix. <Br/> // 3. nwidthstep: Row offset. <Br/> // 4. nscale: scale. <Br/> // 5. n0000ction: grayscale compression. <Br/> bool calglcm (unsigned char * pimagedata, int nleft, int ntop, int nwidth, int nheight, int nwidthstep, int nscale, int nreduction); <br/> }; <br/> // GLCM <br/> cglcm: cglcm (void) <br/>: m_pmat1 (null) <br/>, m_pmat2 (null) <br/>, m_pmat3 (null) <br/>, m_pmat4 (null) <br/>, m_nmin (0) <br/>, m_nmax (0) <br/>, m_nsum (0) <br/> {<br/> int I, j; <br/> unsigned int nsize = sizeof (INT) * 256*256; <br/> // create Create a symbiotic matrix <br/> m_pmat1 = (int *) malloc (nsize); <br/> m_pmat2 = (int *) malloc (nsize ); <br/> m_pmat3 = (int *) malloc (nsize); <br/> m_pmat4 = (int *) malloc (nsize ); <br/> If (m_pmat1 & m_pmat2 & m_pmat3 & m_pmat4) <br/>{< br/> for (I = 0, j = 0; I <256; I ++, J ++ = 256) <br/>{< br/> m_pline [0] [I] = m_pmat1 + J; <br/> m_pline [1] [I] = m_pmat2 + J; <br/> m_pline [2] [I] = m_pmat3 + J; <br/> m_pline [3] [I] = m_pmat4 + J; <br/>}< br/> cglcm ::~ Cglcm (void) <br/>{< br/> // release the symbiotic matrix <br/> If (m_pmat1) Free (m_pmat1); <br/> If (m_pmat2) free (m_pmat2); <br/> If (m_pmat3) Free (m_pmat3); <br/> If (m_pmat4) Free (m_pmat4 ); <br/>}< br/> // calculate the co-occurrence matrix <br/> bool cglcm: calglcm (unsigned char * pimagedata, int nleft, int ntop, int nwidth, int nheight, int nwidthstep, int nscale, int nreduction) <br/>{< br/> bool bresult = false; <br/> int x0, x1, x2; <br/> int y0, Y1, Y2; <br/> int ngray; <br/> unsigned int nsize; <br/> unsigned char * pline [3]; <br/> If (pimagedata) <br/>{< br/> // grayscale value <br/> m_nmin = 0xff; <br/> m_nmax = 0; <br/> pline [1] = pimagedata + nwidthstep * ntop + nleft; <br/> for (Y1 = 0; Y1 <nheight; Y1 ++) <br/> {<br/> for (x1 = 0; X1 <nwidth; X1 ++) <br/> {<br/> // grayscale compression <br/> If (nction> 0) <br/>{< br/> pline [1] [X1] = pline [1] [X1]> nction; <br/>}< br/> ngray = pline [1] [X1]; <br/> If (ngray <m_nmin) <br/>{< br/> m_nmin = ngray; <br/>}< br/> else if (ngray> m_nmax) <br/>{< br/> m_nmax = ngray; <br/>}< br/> pline [1] + = nwidthstep; <br/>}< br/> // accumulate and <br/> m_nsum = nwidth * nheight * 2; <br/> If (m_nmax> = m_nmin) <br/> {<br/> // clear the memory <br/> nsize = sizeof (INT) * (m_nmax-m_nmin + 1 ); <br/> for (Y1 = m_nmin; Y1 <= m_nmax; Y1 ++) <br/>{< br/> memset (& m_pline [0] [Y1] [m_nmin], 0, nsize ); <br/> memset (& m_pline [1] [Y1] [m_nmin], 0, nsize ); <br/> memset (& m_pline [2] [Y1] [m_nmin], 0, nsize ); <br/> memset (& m_pline [3] [Y1] [m_nmin], 0, nsize ); <br/>}< br/> // calculate the co-occurrence matrix <br/> pline [1] = pimagedata + nwidthstep * ntop + nleft; <br/> for (Y0 =-nscale, Y1 = 0, y2 = nscale; Y1 <nheight; y0 ++, Y1 ++, Y2 ++) <br/> {<br/> pline [0] = pline [1]-nwidthstep; <br/> pline [2] = pline [1] + nwidthstep; <br/> for (X0 =-nscale, X1 = 0, X2 = nscale; X1 <nwidth; x0 ++, X1 ++, X2 ++) <br/> {<br/> ngray = pline [1] [X1]; <br/> // 0 degree <br/> If (X2 <nwidth) <br/> {<br/> m_pline [0] [ngray] [pline [1] [X2] ++; <br/>}< br/> // 45 degrees <br/> If (y0> 0 & X2 <nwidth) <br/> {<br/> m_pline [1] [ngray] [pline [0] [X2] ++; <br/>}< br/> // 90 degrees <br/> If (y0> 0) <br/> {<br/> m_pline [2] [ngray] [pline [0] [X1] ++; <br/>}< br/> // 135 degrees <br/> If (y0> 0 & x0> 0) <br/> {<br/> m_pline [3] [ngray] [pline [0] [x0] ++; <br/>}< br/> // 180 degrees <br/> If (x0> 0) <br/> {<br/> m_pline [0] [ngray] [pline [1] [x0] ++; <br/>}< br/> // 225 degrees <br/> If (x0> 0 & Y2 <nheight) <br/> {<br/> m_pline [1] [ngray] [pline [2] [x0] ++; <br/>}< br/> // 270 degrees <br/> If (Y2 <nheight) <br/> {<br/> m_pline [2] [ngray] [pline [2] [X1] ++; <br/>}< br/> // 315 degrees <br/> If (Y2 <nheight & X2 <nwidth) <br/> {<br/> m_pline [3] [ngray] [pline [2] [X2] ++; <br/>}< br/> pline [1] + = nwidthstep; <br/>}< br/> bresult = true; <br/>}< br/> return bresult; <br/>}< br/>
Extracting co-occurrence matrix features

 

To more intuitively describe texture conditions using a co-occurrence matrix, some parameters that reflect the matrix conditions are derived from the co-occurrence matrix. The following are typical examples:

  1. Corner
    Second Moment (ASM): it is the sum of squares of element values in the gray-scale co-occurrence matrix, so it is also called energy. It reflects the degree of uniformity of the gray scale distribution and the coarse fineness of the texture. If all values in the co-occurrence matrix are equal, then the ASM Value
    Small; on the contrary, if some of the values are large and others are small, the ASM value is large. When elements in the symbiotic matrix are centrally distributed, the ASM value is large. The ASM value is large, indicating a texture pattern with uniform and regular changes.

  2. Entropy (ENT): a measure of the amount of information in an image. Texture Information is also a measure of randomness, when all elements in the symbiosis matrix have the largest randomness and all values in the space symbiosis matrix are almost the same, the entropy is large when the elements in the symbiosis matrix are dispersed. It indicates the degree of uneven or complex texture in the image.
  3. Contrast (CON): it reflects the definition of the image and the depth of the texture. The deeper the texture groove, the larger the contrast, the clearer the visual effect. If the contrast is small, the groove is light and the effect is blurred. The gray difference indicates that the greater the contrast, the greater the number of pixels. The larger the element value of the gray-scale common matrix, the larger the con value.
  4. Inverse gap (HOM): it reflects the homogeneity of the Image Texture and measures the local variation of the image texture. If the value is large, the image texture does not change between different regions, and the local texture is very even.
  5. Phase
    Correlation (CoR): measures the similarity of gray-scale co-occurrence matrix elements in the row or column direction. Therefore, the correlation value reflects the local gray-scale correlation in the image. When the matrix element values are even and equal, the correlation value is large;
    On the contrary, if the matrix element values differ greatly, the correlation value is small. If the image has a horizontal texture, the CoR value of the horizontal direction matrix is greater than the CoR value of the other matrices.

# Include <math. h> <br/> // symbiotic matrix feature <br/> struct glcmfeature <br/> {<br/> double ASM [4]; // second moment/energy of the angle <br/> double ent [4]; // entropy <br/> double con [4]; // contrast <br/> double hom [4]; // deficit moment/Homogeneity <br/> double cor [4]; // correlation <br/> }; <br/> // GLCM <br/> class cglcm <br/> {<br/> Public: <br/> cglcm (void ); <br/> virtual ~ Cglcm (void); <br/> Public: <br/> int * m_pmat1; <br/> int * m_pmat2; <br/> int * m_pmat3; <br/> int * m_pmat4; <br/> protected: <br/> int * m_pline [4] [256]; <br/> int m_nmin; <br/> int m_nmax; <br/> int m_nsum; <br/> Public: <br/> // calculate the co-occurrence matrix <br/> // parameter: <br/> // 1. pimagedata: Image Data Pointer, single channel, 8 bits. <Br/> // 2. nleft, ntop, nwidth, and nheight: Calculate the region of the co-occurrence matrix. <Br/> // 3. nwidthstep: Row offset. <Br/> // 4. nscale: scale. <Br/> // 5. n0000ction: grayscale compression. <Br/> bool calglcm (unsigned char * pimagedata, int nleft, int ntop, int nwidth, int nheight, int nwidthstep, int nscale, int nreduction ); <br/> // symbiotic matrix feature <br/> // parameter: <br/> // 1. feature: output the features of the symbiotic matrix. <Br/> void getfeature (glcmfeature & feature); <br/>}; <br/> // symbiotic matrix feature <br/> void cglcm :: getfeature (glcmfeature & feature) <br/>{< br/> int X, Y; <br/> int ntheta; <br/> int nvalue; <br/> int ntemp; <br/> double dvalue; <br/> double dmean, dstddev; <br/> double dsum [1, 256]; <br/> // clear memory <br/> memset (& feature, 0, sizeof (feature )); <br/> // direction loop <br/> for (ntheta = 0; ntheta <4; ntheta ++) <br/>{< br/> dmean = 0; <br/> Dstddev = 0; <br/> // clear the memory <br/> memset (dsum, 0, sizeof (dsum); <br/> for (y = m_nmin; Y <= m_nmax; y ++) <br/>{< br/> for (x = m_nmin; x <= m_nmax; X ++) <br/>{< br/> nvalue = m_pline [ntheta] [y] [X]; <br/> If (nvalue! = 0) <br/>{< br/> // normalized symbiotic matrix <br/> dvalue = (double) nvalue/(double) m_nsum; <br/> ntemp = (x-y) * (x-y); <br/> // second moment/energy of the angle <br/> feature. ASM [ntheta] + = (dvalue * dvalue); <br/> // entropy <br/> feature. ent [ntheta]-= (dvalue * log (dvalue); <br/> // contrast <br/> feature. CON [ntheta] + = (ntemp * dvalue); <br/> // deficit moment/Homogeneity <br/> feature. HOM [ntheta] + = (dvalue/(1 + ntemp); <br/> // correlation <br/> feature. cor [ntheta] + = (x * y * dvalue); <br/> dsum [y] + = dvalue; <br/>}< br/> for (y = m_nmin; y <= m_nmax; y ++) <br/>{< br/> dmean + = (y * dsum [y]); <br/>}< br/> for (y = m_nmin; Y <= m_nmax; y ++) <br/>{< br/> dstddev + = (Y-dmean) * dsum [y]); <br/>}< br/> // correlation <br/> If (ABS (dstddev)> 1e-15) <br/>{< br/> feature. cor [ntheta] = (feature. cor [ntheta]-dmean * dmean)/dstddev; <br/>}< br/> else <br/>{< br/> feature. cor [ntheta] = 0; <br/>}< br/>}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.