Java实现图片去噪和灰度的类

用于实现对图片去噪和灰度化。

package org.eye;

import java.awt.Color;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.File;  
import java.io.IOException;  
import javax.imageio.ImageIO;  

public class ClearImageHelper {
	/**
	 * 
	 * @param sfile
	 *            需要去噪的图像
	 * @param destDir
	 *            去噪后的图像保存地址
	 * @throws IOException
	 */
	public static void cleanImage(File sfile, String destDir)throws IOException{
		File destF = new File(destDir);
		if (!destF.exists()){
			destF.mkdirs();
		}

		BufferedImage bufferedImage = ImageIO.read(sfile);
		int h = bufferedImage.getHeight();
		int w = bufferedImage.getWidth();

		// 灰度化
		int[][] gray = new int[w][h];
		for (int x = 0; x < w; x++){
			for (int y = 0; y < h; y++){
				int argb = bufferedImage.getRGB(x, y);
				// 图像加亮(调整亮度识别率非常高)
				int r = (int) (((argb >> 16) & 0xFF) * 1.1 + 30);
				int g = (int) (((argb >> 8) & 0xFF) * 1.1 + 30);
				int b = (int) (((argb >> 0) & 0xFF) * 1.1 + 30);
				if (r >= 255){
					r = 255;
				}
				if (g >= 255){
					g = 255;
				}
				if (b >= 255){
					b = 255;
				}
				gray[x][y] = (int) Math.pow((Math.pow(r, 2.2) * 0.2973 + Math.pow(g, 2.2)* 0.6274 + Math.pow(b, 2.2) * 0.0753), 1 / 2.2);
			}
		}

		// 二值化
		int threshold = ostu(gray, w, h);
		BufferedImage binaryBufferedImage = new BufferedImage(w, h,BufferedImage.TYPE_BYTE_BINARY);
		for (int x = 0; x < w; x++){
			for (int y = 0; y < h; y++){
				if (gray[x][y] > threshold){
					gray[x][y] |= 0x00FFFF;
				} else{
					gray[x][y] &= 0xFF0000;
				}
				binaryBufferedImage.setRGB(x, y, gray[x][y]);
			}
		}

		// 矩阵打印
		for (int y = 0; y < h; y++){
			for (int x = 0; x < w; x++){
				if (isBlack(binaryBufferedImage.getRGB(x, y))){
					System.out.print("*");
				} else{
					System.out.print(" ");
				}
			}
			System.out.println();
		}
		ImageIO.write(binaryBufferedImage, "jpg", new File(destDir, sfile.getName()));
	}

	public static boolean isBlack(int colorInt){
		Color color = new Color(colorInt);
		if (color.getRed() + color.getGreen() + color.getBlue() <= 300){
			return true;
		}
		return false;
	}

	public static boolean isWhite(int colorInt){
		Color color = new Color(colorInt);
		if (color.getRed() + color.getGreen() + color.getBlue() > 300){
			return true;
		}
		return false;
	}

	public static int isBlackOrWhite(int colorInt){
		if (getColorBright(colorInt) < 30 || getColorBright(colorInt) > 730){
			return 1;
		}
		return 0;
	}

	public static int getColorBright(int colorInt){
		Color color = new Color(colorInt);
		return color.getRed() + color.getGreen() + color.getBlue();
	}

	public static int ostu(int[][] gray, int w, int h){
		int[] histData = new int[w * h];
		// Calculate histogram
		for (int x = 0; x < w; x++){
			for (int y = 0; y < h; y++){
				int red = 0xFF & gray[x][y];
				histData[red]++;
			}
		}

		// Total number of pixels
		int total = w * h;

		float sum = 0;
		for (int t = 0; t < 256; t++)
			sum += t * histData[t];

		float sumB = 0;
		int wB = 0;
		int wF = 0;

		float varMax = 0;
		int threshold = 0;

		for (int t = 0; t < 256; t++){
			wB += histData[t]; // Weight Background
			if (wB == 0)
				continue;

			wF = total - wB; // Weight Foreground
			if (wF == 0)
				break;

			sumB += (float) (t * histData[t]);

			float mB = sumB / wB; // Mean Background
			float mF = (sum - sumB) / wF; // Mean Foreground

			// Calculate Between Class Variance
			float varBetween = (float) wB * (float) wF * (mB - mF) * (mB - mF);

			// Check if new maximum found
			if (varBetween > varMax){
				varMax = varBetween;
				threshold = t;
			}
		}

		return threshold;
	}
	//图片灰度,黑白
	public static void gray(String srcImageFile, String destImageFile) {
        try {
            BufferedImage src = ImageIO.read(new File(srcImageFile));
            ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
            ColorConvertOp op = new ColorConvertOp(cs, null);
            src = op.filter(src, null);
            ImageIO.write(src, "JPEG", new File(destImageFile));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
	
	public static void main(String[] args) throws IOException{
		/*File testDataDir = new File("testdata");
		final String destDir = testDataDir.getAbsolutePath()+"/tmp";
		for (File file : testDataDir.listFiles()){
			cleanImage(file, destDir);
		}*/
		File testDataDir = new File("D:\\tmp\\3VG5B.jpg");//去噪
		String destDir ="D:\\tmp\\tmp";
		cleanImage(testDataDir, destDir);
		//gray("D:\\tmp\\3VG5B.jpg","D:\\tmp\\3VG5B1.jpg");//灰度化
	}
}


  • 5
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
在淘宝中根据图片图片是一种基于图像识别技术的功能,它能够通过用户提供的图片来搜索相似或相同的商品图片实现这一功能的基本思路是利用Java语言结合图像处理和特征提取算法。 首先,用户需通过程序在淘宝平台上传待搜索的图片Java语言提供了一系列图像处理库,如OpenCV、JavaCV等,使得开发者可以对图像进行处理与分析。程序可以通过读取图片的像素信息并对其进行预处理,包括灰度化、去噪、增强对比度等操作。 接下来,基于图像的特征提取算法对预处理后的图片进行特征提取。特征提取是根据图像的某些特征进行数值化的过程,例如颜色特征、纹理特征、形状特征等。常用的特征提取算法有SIFT、SURF、HOG等。这些算法可以将图像中的关键特征提取出来,用于后续的相似度比较。 当用户上传待搜索图片后,程序会对该图片进行特征提取,并与数据库中的商品图片进行相似度比较。这里需要建立一个商品图片库,通过爬取淘宝平台商品图片构建数据库。相似度比较可以使用欧氏距离或余弦相似度等方式进行,通过计算两个图片特征之间的距离或相似度,得到最相似的商品图片。 最后,程序会根据相似度的排序结果,将与用户待搜索图片最相似的商品图片返回给用户。用户可以通过比对返回的商品图片与待搜索图片来确定是否是想要找的商品。 这样,基于Java语言的图像识别技术可以实现淘宝中根据图片图片的功能。基于图像处理和特征提取算法,程序通过对用户上传的图片进行处理与分析,并与商品图片库进行相似度比较,最终将最相似的商品图片返回给用户。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值