欢迎来到天天文库
浏览记录
ID:57631563
大小:53.01 KB
页数:6页
时间:2020-08-29
《java-图像灰度化与二值化.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、java图像灰度化与二值化1:packageorg.chinasb.client;2:3:importjava.awt.Color;4:importjava.awt.image.BufferedImage;5:importjava.io.File;6:importjava.io.IOException;7:8:importjavax.imageio.ImageIO;9:10:publicclassBinaryTest{11:12:publicstaticvoidmain(String[]args)
2、throwsIOException{13:BufferedImagebufferedImage=ImageIO.read(newFile("D:/passCodeAction.jpg"));14:inth=bufferedImage.getHeight();15:intw=bufferedImage.getWidth();16:17://灰度化18:int[][]gray=newint[w][h];19:for(intx=0;x3、ntargb=bufferedImage.getRGB(x,y);22:intr=(argb>>16)&0xFF;23:intg=(argb>>8)&0xFF;24:intb=(argb>>0)&0xFF;25:intgrayPixel=(int)((b*29+g*150+r*77+128)>>8);26:gray[x][y]=grayPixel;27:}28:}29:30://二值化31:intthreshold=ostu(gray,w,h);32:BufferedImagebinaryBuff4、eredImage=newBufferedImage(w,h,BufferedImage.TYPE_BYTE_BINARY);33:for(intx=0;xthreshold){36:gray[x][y]5、=0x00FFFF;37:}else{38:gray[x][y]&=0xFF0000;39:}40:binaryBufferedImage.setRGB(x,y,gray[x][y]);41:}42:6、}43:44://矩阵打印45:for(inty=0;y7、ile("D:/code.jpg"));57:}58:59:publicstaticbooleanisBlack(intcolorInt){60:Colorcolor=newColor(colorInt);61:if(color.getRed()+color.getGreen()+color.getBlue()<=300){62:returntrue;63:}64:returnfalse;65:}66:67:publicstaticbooleanisWhite(intcolorInt){68:Co8、lorcolor=newColor(colorInt);69:if(color.getRed()+color.getGreen()+color.getBlue()>300){70:returntrue;71:}72:returnfalse;73:}74:75:publicstaticintisBlackOrWhite(intcolorInt){76:if(getColorBright(colorInt)<309、10、getColorBright(colorInt)>730){77:return1;7811、:}79:return0;80:}81:82:publicstaticintgetColorBright(intcolorInt){83:Colorcolor=newColor(colorInt);84:returncolor.getRed()+color.getGreen()+color.getBlue();85:}86:87:publicstaticintostu(int[][]gray,intw,inth){88:int[]histData=newint[w*h];89://
3、ntargb=bufferedImage.getRGB(x,y);22:intr=(argb>>16)&0xFF;23:intg=(argb>>8)&0xFF;24:intb=(argb>>0)&0xFF;25:intgrayPixel=(int)((b*29+g*150+r*77+128)>>8);26:gray[x][y]=grayPixel;27:}28:}29:30://二值化31:intthreshold=ostu(gray,w,h);32:BufferedImagebinaryBuff
4、eredImage=newBufferedImage(w,h,BufferedImage.TYPE_BYTE_BINARY);33:for(intx=0;xthreshold){36:gray[x][y]
5、=0x00FFFF;37:}else{38:gray[x][y]&=0xFF0000;39:}40:binaryBufferedImage.setRGB(x,y,gray[x][y]);41:}42:
6、}43:44://矩阵打印45:for(inty=0;y7、ile("D:/code.jpg"));57:}58:59:publicstaticbooleanisBlack(intcolorInt){60:Colorcolor=newColor(colorInt);61:if(color.getRed()+color.getGreen()+color.getBlue()<=300){62:returntrue;63:}64:returnfalse;65:}66:67:publicstaticbooleanisWhite(intcolorInt){68:Co8、lorcolor=newColor(colorInt);69:if(color.getRed()+color.getGreen()+color.getBlue()>300){70:returntrue;71:}72:returnfalse;73:}74:75:publicstaticintisBlackOrWhite(intcolorInt){76:if(getColorBright(colorInt)<309、10、getColorBright(colorInt)>730){77:return1;7811、:}79:return0;80:}81:82:publicstaticintgetColorBright(intcolorInt){83:Colorcolor=newColor(colorInt);84:returncolor.getRed()+color.getGreen()+color.getBlue();85:}86:87:publicstaticintostu(int[][]gray,intw,inth){88:int[]histData=newint[w*h];89://
7、ile("D:/code.jpg"));57:}58:59:publicstaticbooleanisBlack(intcolorInt){60:Colorcolor=newColor(colorInt);61:if(color.getRed()+color.getGreen()+color.getBlue()<=300){62:returntrue;63:}64:returnfalse;65:}66:67:publicstaticbooleanisWhite(intcolorInt){68:Co
8、lorcolor=newColor(colorInt);69:if(color.getRed()+color.getGreen()+color.getBlue()>300){70:returntrue;71:}72:returnfalse;73:}74:75:publicstaticintisBlackOrWhite(intcolorInt){76:if(getColorBright(colorInt)<30
9、
10、getColorBright(colorInt)>730){77:return1;78
11、:}79:return0;80:}81:82:publicstaticintgetColorBright(intcolorInt){83:Colorcolor=newColor(colorInt);84:returncolor.getRed()+color.getGreen()+color.getBlue();85:}86:87:publicstaticintostu(int[][]gray,intw,inth){88:int[]histData=newint[w*h];89://
此文档下载收益归作者所有