|
From: Gustavo P. B. <gb...@us...> - 2005-04-02 20:14:20
|
Update of /cvsroot/kimageprocess/kimageprocess/src/methods/hsvcm In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16771 Modified Files: hsvcm.cpp hsvcm.h Log Message: - Implemented the HSV Co-Occurrence calculation. This is a big step in our efforts to use color textures to classify images. Index: hsvcm.cpp =================================================================== RCS file: /cvsroot/kimageprocess/kimageprocess/src/methods/hsvcm/hsvcm.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- hsvcm.cpp 2 Apr 2005 18:17:27 -0000 1.2 +++ hsvcm.cpp 2 Apr 2005 20:14:09 -0000 1.3 @@ -41,10 +41,16 @@ //check dependencies m_methodName = "hsvcm"; - m_longName = i18n("Hue Saturation and Value Co-Occurrence Matrix"); + m_longName = i18n("HSV Co-Occurrence Matrix"); //defaults + m_quantH = 8; + m_quantS = 4; + m_quantV = 4; + m_sizeH = 45; // 360 / 8 + m_sizeS = 64; // 256 / 4 + m_sizeV = 64; // 256 / 4 } @@ -55,19 +61,188 @@ void KTHSVCM::calculate(KTImage *img, KTClassifBackend *backend, int imgClass) { m_img = img; + + // create matrices + for (int d=0; d < 9; d++) + { + m_cm[d] = new float**[m_sizeV]; + for (int i=0;i < m_sizeV; i++) + { + m_cm[d][i] = new float*[m_sizeH]; + for (int j=0; j < m_sizeH; j++) + m_cm[d][i][j] = new float[m_sizeS]; + } + } + calculateHSVCM(); + + + //delete the GLCM's after calculating + for (int d=0; d < 9; d++) + { + for (int i=0;i < m_sizeV; i++) + { + for (int j=0;j < m_sizeH; j++) + delete [] m_cm[d][i][j]; + delete [] m_cm[d][i]; + } + delete m_cm[d]; + } } void KTHSVCM::calculateHSVCM() { QColor color; - for (int i = 0; i < m_img->width(); ++i) - for (int j = 0; j < m_img->height(); ++j) + + //initialize the matrices with zeros + for (int d=0; d < 9; d++) + for (int i=0; i < m_sizeV; i++) + for (int j=0; j < m_sizeH; j++) + for (int k=0; k < m_sizeS; k++) + m_cm[d][i][j][k] = 0; + + int width = m_img->width(); + int height = m_img->height(); + + for (int x = 0; x < width; ++x) + for (int y = 0; y < height; ++y) { - int H, S, V; - color.setRgb(m_img->pixel(i, j)); - color.getHsv(&H,&S,&V); - kdDebug() << "H = " << H << " S = " << S << " V = " << V << endl; + //get the H component of the central pixel + int H, S, V, tmpH, tmpS, tmpV; + color.setRgb(m_img->pixel(x, y)); + color.getHsv(&H,&tmpS,&tmpV); + H = H / m_quantH; + + //nine directions + for (int d = 0; d < 9; d++) + { + int xs,ys,xv,yv; + switch (d) + { + case 0: + /** + * o -> H,S,V + * + * The pixels from the three planes are all in the same direction + */ + xs = xv = x; + ys = yv = y; + break; + case 1: + /** + * S + * / + * H + * / + * V + */ + xv = (x == 0 ? width-1 : x-1); + yv = (y == height-1 ? 0 : y+1); + + xs = (x == width-1 ? 0 : x+1); + ys = (y == 0 ? height-1 : y-1); + break; + case 2: + /** + * S + * | + * H + * | + * V + */ + xs = xv = x; + yv = (y == height-1 ? 0 : y+1); + ys = (y == 0 ? height-1 : y-1); + break; + case 3: + /** + * S + * \ + * H + * \ + * V + */ + xv = (x == width-1 ? 0 : x+1); + yv = (y == height-1 ? 0 : y+1); + + xs = (x == 0 ? width-1 : x-1); + ys = (y == 0 ? height-1 : y-1); + break; + case 4: + /** + * + * S - H - V + * + */ + yv = ys = y; + xv = (x == width-1 ? 0 : x+1); + xs = (x == 0 ? width-1 : x-1); + break; + case 5: + /** + * V + * / + * H + * / + * S + */ + xv = (x == width-1 ? 0 : x+1); + yv = (y == 0 ? height-1 : y-1); + + xs = (x == 0 ? width-1 : x-1); + ys = (y == height-1 ? 0 : y+1); + break; + case 6: + /** + * V + * | + * H + * | + * S + */ + xs = xv = x; + yv = (y == 0 ? height-1 : y-1); + ys = (y == height-1 ? 0 : y+1); + break; + case 7: + /** + * V + * \ + * H + * \ + * S + */ + xv = (x == 0 ? width-1 : x-1); + yv = (y == 0 ? height-1 : y-1); + + xs = (x == width-1 ? 0 : x+1); + ys = (y == height-1 ? 0 : y+1); + break; + case 8: + /** + * + * V - H - S + * + */ + yv = ys = y; + xv = (x == 0 ? width-1 : x-1); + xs = (x == width-1 ? 0 : x+1); + break; + } + //get the V component of the direction pixel + color.setRgb(m_img->pixel(xv,yv)); + color.getHsv(&tmpH, &tmpS, &V); + V = V / m_quantV; + + //get the S component of the direction pixel + color.setRgb(m_img->pixel(xs,ys)); + color.getHsv(&tmpH, &S, &tmpV); + S = S / m_quantS; + + m_cm[d][V][H][S]++; + kdDebug() << "Got H,S,V = " << H << " " << S << " " << V << endl; + } + } } Index: hsvcm.h =================================================================== RCS file: /cvsroot/kimageprocess/kimageprocess/src/methods/hsvcm/hsvcm.h,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- hsvcm.h 2 Apr 2005 18:17:27 -0000 1.2 +++ hsvcm.h 2 Apr 2005 20:14:09 -0000 1.3 @@ -62,10 +62,15 @@ int m_sizeS; int m_sizeV; - float m_quantH; - float m_quantS; - float m_quantV; - + int m_quantH; + int m_quantS; + int m_quantV; + /** Co-occurrence matrix + * i -> V plane + * j -> H plane + * k -> S plane + */ + float ***m_cm[9]; }; |