/** * 二阶微分算子 laplacian算子方法 * @param srcPath 图片的存储位置 * @param distPath 图像要保存的存储位置 * @param formatName 图像要保存的存储位置 */ public static void laplacian0(String srcPath, String distPath, String formatName) { BufferedImage img = readImg(srcPath); int w = img.getWidth(); int h = img.getHeight(); int pix[] = new int[w*h]; pix= img.getRGB(0, 0, w, h, pix, 0, w); pix = laplacian0(pix, w, h); img.setRGB(0, 0, w, h, pix, 0, w); writeImg(img, formatName, distPath); } /** * 二阶微分算子 laplacian算子方法 * @param pix 像素矩阵数组 * @param w 矩阵的宽 * @param h 矩阵的高 * @return 处理后的数组 */ public static int[] laplacian0(int[] pix, int w, int h) { int[] newpix = new int[w*h]; ColorModel cm = ColorModel.getRGBdefault(); int r; for(int y=0; y<h; y++) { for(int x=0; x<w; x++) { if(x!=0 && x!=w-1 && y!=0 && y!=h-1) { //G = 4f(x,y) - f(x-1,y) - f(x+1,y) - f(x,y-1) - f(x,y+1) r = 4*cm.getRed(pix[x+(y)*w]) - cm.getRed(pix[x-1+(y)*w]) - cm.getRed(pix[x+1+(y)*w]) - cm.getRed(pix[x+(y-1)*w]) - cm.getRed(pix[x+(y+1)*w]); newpix[x+y*w] = 255<<24 | r<<16 | r<<8 | r; } } } int temp = findMinInt(newpix); for(int i=0; i<newpix.length; i++) { newpix[i] = newpix[i] + temp; } return newpix; }
还有几种laplacian算子的变形,如下
对应的效果如下:
L1 L2 L3
从以上几个效果可以看出,L1与L0算子的效果基本相同,肉眼很难分辨;L2获得的细节信息较弱;L3处理的结果是图像的纹里比原图更清晰。