《java-图像灰度化与二值化.docx》由会员分享,提供在线免费全文阅读可下载,此文档格式为docx,更多相关《java-图像灰度化与二值化.docx》文档请在天天文库搜索。
1、java 图像灰度化与二值化 1: package org.chinasb.client; 2: 3: import java.awt.Color; 4: import java.awt.image.BufferedImage; 5: import java.io.File; 6: import java.io.IOException; 7: 8: import javax.imageio.ImageIO; 9: 10: public class BinaryTest { 11: 12: public static void main(String[] args) throws IOException { 13: BufferedImage bufferedImage = ImageIO.read(new File("D:/passCodeAction.jpg")); 14: int h。
2、 = bufferedImage.getHeight(); 15: int w = bufferedImage.getWidth(); 16: 17: // 灰度化 18: int[][] gray = new int[w][h]; 19: for (int x = 0; x < w; x++) { 20: for (int y = 0; y > 16) & 0xFF; 23: int g = (argb >> 8) & 0xFF; 24: int b = (argb >> 0) & 0xFF; 25: int grayPixel = (int) ((b * 29 + g * 150 + r * 77 + 128) >> 8); 26: gray[x][y] = grayPixel; 27: } 28: } 29: 30: // 二值化 31。
3、: int threshold = ostu(gray, w, h); 32: BufferedImage binaryBufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY); 33: for (int x = 0; x < w; x++) { 34: for (int y = 0; y threshold) { 36: gray[x][y] |= 0x00FFFF; 37: } else { 38: gray[x][y] &= 0xFF0000; 39: } 40: binaryBufferedImage.setRGB(x, y, gray[x][y]); 41: } 42: } 43: 44: // 矩阵打印 45: for (int y = 0; y < h; y++) { 46: f。
4、or (int x = 0; x < w; x++) { 47: if (isBlack(binaryBufferedImage.getRGB(x, y))) { 48: System.out.print("*"); 49: } else { 50: System.out.print(" "); 51: } 52: } 53: System.out.println(); 54: } 55: 56: ImageIO.write(binaryBufferedImage, "jpg", new File("D:/code.jpg")); 57: } 58: 59: public static boolean isBlack(int colorInt) { 60: Color color = new Color(colorInt); 61: if (color.getRed() + colo。
5、r.getGreen() + color.getBlue() 300) { 70: return true; 71: } 72: return false; 73: } 74: 75: public static int isBlackOrWhite(int colorInt) { 76: if (getColorBright(colorInt) 730) { 77: return 1; 78: } 79: return 0; 80: } 81: 82: public static int getColorBright(int colorInt) { 83: Color color = new Color(colorInt); 84: return color.getRed() + color.getGreen() + color.getBlue(); 85: } 86: 87: publ。
6、ic static int ostu(int[][] gray, int w, int h) { 88: int[] histData = new int[w * h]; 89: // Calculate histogram 90: for (int x = 0; x < w; x++) { 91: for (int y = 0; y < h; y++) { 92: int red = 0xFF & gray[x][y]; 93: histData[red]++; 94: } 95: } 96: 97: // Total number of pixels 98: int total = w * h; 99: 100: float sum = 0; 101: for (int t = 0; t < 256; t++) 102: sum += t * histData[t]; 103: 104: float sumB = 0; 105: int wB = 0; 106: int wF = 0; 107: 108: float varMax = 0; 109: int threshold = 0; 110: 111: for (int t = 0; t varMax) { 130: varMax = varBetween; 131: threshold = t; 132: } 133: } 134: 135: return threshold; 136: } 137: } 效果。