资源简介
java图像处理方法:灰度化,二值化,降噪,切割,裁剪,找相似等
代码片段和文件信息
package com.bdfus.piper.common.util.image;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import javax.imageio.ImageIO;
public class BinaryTest {
/**
*
* @param sourcePath 全路径,包括文件名
* @param targetDir 目标目录,不包括文件名,最后面不要斜杠(数字+binary)
* @throws Exception
*/
public static void binaryAndCutImage(String sourcePath String targetDir) throws Exception{
File f = new File(sourcePath);
BufferedImage input = ImageIO.read(f);
input = grayImage(input);
input = binaryImage(input);
input = denoise(input);
String formatName = sourcePath.substring(sourcePath.lastIndexOf(“.“)+1 sourcePath.length());
File destF = new File(targetDir);
if (!destF.exists())
{
destF.mkdirs();
}
String targetPath = targetDir+File.separator+“binary.“+formatName;
ImageIO.write(input formatName new File(targetPath));
int[] xpro = CutImg.xpro(input input.getWidth() input.getHeight());
Rectangle[] r = CutImg.xproSegment(input xpro 4);
for(int i=0; i int[] ypro = CutImg.ypro(input r[i]);
Rectangle y = CutImg.yproSegment(ypro r[i] 2);
ImageCutUtil imageCut = new ImageCutUtil(targetPath targetDir+File.separator+i+“.“+formatName (int)y.getX() (int)y.getY() (int)y.getWidth() (int)y.getHeight());
imageCut.cut(imageCut.getSrcpath() imageCut.getSubpath());
}
}
public static void grayAndBinaryImage(String sourcePath String targetPath) throws Exception {
BufferedImage input = ImageIO.read(new FileInputStream(sourcePath));
input = grayImage(input);
input = binaryImage(input);
input = denoise(input);
String formatName = sourcePath.substring(sourcePath.lastIndexOf(“.“)+1 sourcePath.length());
ImageIO.write(input formatName new File(targetPath));
}
/**
* 图片灰化(参考:http://www.codeceo.com/article/java-image-gray.html)
*
* @param bufferedImage
* 待处理图片
* @return
* @throws Exception
*/
public static BufferedImage grayImage(BufferedImage bufferedImage) throws Exception {
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
BufferedImage grayBufferedImage = new BufferedImage(width height bufferedImage.getType());
for (int i = 0; i < bufferedImage.getWidth(); i++) {
for (int j = 0; j < bufferedImage.getHeight(); j++) {
final int color = bufferedImage.getRGB(i j);
final int r = (color >> 16) & 0xff;
final int g = (color >> 8) & 0xff;
final int b = color & 0xff;
int gray = (int) (0.3 * r + 0.59 * g + 0.11 * b);
int newPixel = colorToRGB(255 gray gray gray);
grayBufferedImage.setRGB(i j newPixel);
}
}
return grayBufferedImage;
}
/**
* 颜色分量转换为RGB值
*
* @param alpha
* @param red
* @param green
* @param blue
* @return
*/
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 11462 2017-11-13 11:57 image-util\BinaryTest.java
文件 5474 2018-01-31 14:43 image-util\ChartGraphics.java
文件 18094 2017-11-13 11:57 image-util\CutImg.java
文件 6389 2018-03-29 16:16 image-util\ImageCut.java
文件 6401 2018-03-29 16:16 image-util\ImageCutUtil.java
文件 2767 2017-11-13 11:57 image-util\ImageHelper.java
文件 8737 2017-11-13 11:57 image-util\SimilarImageSearch.java
目录 0 2018-03-29 16:17 image-util
----------- --------- ---------- ----- ----
59324 8
评论
共有 条评论