2

我正在 j2me 平台上进行图像处理项目。我设法应用了不涉及卷积矩阵的简单效果,例如棕褐色、灰度、负片和色调分离。我什至设法使用卷积矩阵应用模糊效果:

int matrix[][] = {{1,1,1},{1,1,1},{1,1,1}}

但是当我改变转换时。浮雕效果和素描效果的矩阵,我得到了意想不到的结果。下面是我的代码。请让我知道我哪里出错了。我正在使用以下卷积矩阵:

  • 浮雕效果:int matrix[][] = {{-18,-9,0},{-9,9,9},{0,9,18}}
  • 边缘检测:int matrix[][] = {{0,9,0},{9,36,9},{0,9,0}}

我参考了以下链接: http ://beej.us/blog/data/convolution-image-processing/和 http://www.pixastic.com/lib/docs/

public void sketchEffect() {
/*
 * below is the convolution Matrix for blur effect and it works fine
 * int convolutionMatrix[][] = {
 *      {1, 1, 1},
 *      {1, 1, 1},
 *      {1, 1, 1}
 *  };
 *


/*
 * Convolution matrix for edge dtetction(Not working)
 */
int convolutionMatrix[][] = {
    {0, 9, 0},
    {9, -36, 9},
    {0, 9, 0}
};

//Loading image
try {
    image1 = Image.createImage("/image5.jpg");
} catch (IOException ex) {
    ex.printStackTrace();
}

// 1D array for storing RGB values 
//Width = 640px
//Height = 360px
int sourceImageArray[] = new int[640 * 360];
image1.getRGB(sourceImageArray, 0, 640, 0, 0, 640, 360);
int aa, rr, bb, gg;
int a1, r1, g1, b1;
int sumR = 0, sumG = 0, sumB = 0, sumA = 0;
int currentPix;
for (int i = 1; i < 359; i++) {
    for (int j = 1; j < 639; j++) {
        currentPix = sourceImageArray[(i * 640) + j];
        a1 = (currentPix >> 24) & 0xFF;
        r1 = (currentPix >> 16) & 0xFF;
        g1 = (currentPix >> 8) & 0xFF;
        b1 = (currentPix >> 0) & 0xFF;

        /*
         * multiplying neighbouring elements of current pixel to its corresponding
         * element in convolution matrix
         */
        for (int x = -1; x < 2; x++) {
            for (int y = -1; y < 2; y++) {
                rr = (sourceImageArray[((i + x) * 640) + (j + y)] >> 16) & 0xFF;
                gg = (sourceImageArray[((i + x) * 640) + (j + y)] >> 8) & 0xFF;
                bb = (sourceImageArray[((i + x) * 640) + (j + y)] >> 0) & 0xFF;

                sumR += (rr * convolutionMatrix[x + 1][y + 1]);
                sumG += (gg * convolutionMatrix[x + 1][y + 1]);
                sumB += (bb * convolutionMatrix[x + 1][y + 1]);
                rr = gg = bb = 0;
            }

        }
        sumR = sumR / 9;
        sumG = sumG / 9;
        sumB = sumB / 9;
        if (sumR < 0) {
            sumR = 0;
        }
        if (sumG < 0) {
            sumG = 0;
        }
        if (sumB < 0) {
            sumB = 0;
        }
        if (sumR > 255) {
            sumR = 255;
        }
        if (sumG > 255) {
            sumG = 255;
        }
        if (sumB > 255) {
            sumB = 255;
        }
        sourceImageArray[(i * 640) + j] = (a1 << 24) | (sumR << 16)|(sumG << 8)| sumB;
        sumR = sumB = sumG = 0;
        rr = gg = bb = 0;

    }
 }
}
4

0 回答 0