直方图生成,区分暗红色和浅红色

时间:2015-09-03 15:30:05

标签: java

我有一个现有的java代码,我必须对其进行一些更改。它是一个图像处理应用程序该程序从线图像中提取红色,橙色和绿色的像素数。我有以下代码。我的理解是生成直方图并计算红色,橙色和绿色的像素值。

// Color space transform and histogram generation


    WritableRaster raster = image.getRaster();
    WritableRaster rasterMask = mask.getRaster();

    int[] pixel = new int[3];
    int[] pixelMask = new int[3];

    float[] hsv = new float[3];

    int[] quant = new int[3];

    int[][][] histogram = new int[30][8][8];

    for (int x = 0; x < raster.getWidth(); x++) { 
        for (int y = 0; y < raster.getHeight(); y++) {
            rasterMask.getPixel(x, y, pixelMask);

            if(pixelMask[0] != 0 && pixelMask[1] != 0 && pixelMask[2] != 0)      {
                raster.getPixel(x, y, pixel);
                Color.RGBtoHSB(pixel[0], pixel[1], pixel[2], hsv);

                hsv[0] = hsv[0] * 360;

                quant[0] = (int)((float)hsv[0] / 12f);
                quant[1] = (int)((float)((float)hsv[1] / 0.125f));
                quant[2] = (int)((float)((float)hsv[2] / 0.125f));

                if(quant[0] == 30) { quant[0] = 29; }
                if(quant[1] == 8) { quant[1] = 7; }
                if(quant[2] == 8) { quant[2] = 7; }

                histogram[quant[0]][quant[1]][quant[2]]++;

            }
        }
    }

for(int i=4; i<8; i++) {
        for(int j=4; j<8; j++) {
            // red
            colorRed[2] += histogram[0][i][j];
            colorRed[2] += histogram[1][i][j];

            // orange
            colorOrange[1] += histogram[2][i][j];

            // green
            colorGreen[0] += histogram[7][i][j];
            colorGreen[0] += histogram[8][i][j];
            colorGreen[0] += histogram[9][i][j];
        }
    }

我的目的是区分暗红色和浅红色。即获得暗红色像素数和浅红色像素数。我无能为力。谁能请帮忙。谢谢

0 个答案:

没有答案