//運(yùn)行以下程序即可
站在用戶(hù)的角度思考問(wèn)題,與客戶(hù)深入溝通,找到祿勸網(wǎng)站設(shè)計(jì)與祿勸網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶(hù)體驗(yàn)好的作品,建站類(lèi)型包括:網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名與空間、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋祿勸地區(qū)。
public?class?ImageInit?{
BufferedImage?image;
private?int?iw,?ih;
private?int[]?pixels;
public?ImageInit(BufferedImage?image)?{
this.image?=?image;
iw?=?image.getWidth();
ih?=?image.getHeight();
pixels?=?new?int[iw?*?ih];
}
public?BufferedImage?changeGrey()?{
PixelGrabber?pg?=?new?PixelGrabber(image.getSource(),?0,?0,?iw,?ih,
pixels,?0,?iw);
try?{
pg.grabPixels();
}?catch?(InterruptedException?e)?{
e.printStackTrace();
}
//?設(shè)定二值化的域值,默認(rèn)值為100
int?grey?=?100;
//?對(duì)圖像進(jìn)行二值化處理,Alpha值保持不變
ColorModel?cm?=?ColorModel.getRGBdefault();
for?(int?i?=?0;?i??iw?*?ih;?i++)?{
int?red,?green,?blue;
int?alpha?=?cm.getAlpha(pixels[i]);
if?(cm.getRed(pixels[i])??grey)?{
red?=?255;
}?else?{
red?=?0;
}
if?(cm.getGreen(pixels[i])??grey)?{
green?=?255;
}?else?{
green?=?0;
}
if?(cm.getBlue(pixels[i])??grey)?{
blue?=?255;
}?else?{
blue?=?0;
}
pixels[i]?=?alpha??24?|?red??16?|?green??8?|?blue;?//?通過(guò)移位重新構(gòu)成某一點(diǎn)像素的RGB值
}
//?將數(shù)組中的象素產(chǎn)生一個(gè)圖像
Image?tempImg?=?Toolkit.getDefaultToolkit().createImage(
new?MemoryImageSource(iw,?ih,?pixels,?0,?iw));
image?=?new?BufferedImage(tempImg.getWidth(null),
tempImg.getHeight(null),?BufferedImage.TYPE_INT_BGR);
image.createGraphics().drawImage(tempImg,?0,?0,?null);
return?image;
}
public?BufferedImage?getMedian()?{
PixelGrabber?pg?=?new?PixelGrabber(image.getSource(),?0,?0,?iw,?ih,
pixels,?0,?iw);
try?{
pg.grabPixels();
}?catch?(InterruptedException?e)?{
e.printStackTrace();
}
//?對(duì)圖像進(jìn)行中值濾波,Alpha值保持不變
ColorModel?cm?=?ColorModel.getRGBdefault();
for?(int?i?=?1;?i??ih?-?1;?i++)?{
for?(int?j?=?1;?j??iw?-?1;?j++)?{
int?red,?green,?blue;
int?alpha?=?cm.getAlpha(pixels[i?*?iw?+?j]);
//?int?red2?=?cm.getRed(pixels[(i?-?1)?*?iw?+?j]);
int?red4?=?cm.getRed(pixels[i?*?iw?+?j?-?1]);
int?red5?=?cm.getRed(pixels[i?*?iw?+?j]);
int?red6?=?cm.getRed(pixels[i?*?iw?+?j?+?1]);
//?int?red8?=?cm.getRed(pixels[(i?+?1)?*?iw?+?j]);
//?水平方向進(jìn)行中值濾波
if?(red4?=?red5)?{
if?(red5?=?red6)?{
red?=?red5;
}?else?{
if?(red4?=?red6)?{
red?=?red6;
}?else?{
red?=?red4;
}
}
}?else?{
if?(red4??red6)?{
red?=?red4;
}?else?{
if?(red5??red6)?{
red?=?red6;
}?else?{
red?=?red5;
}
}
}
int?green4?=?cm.getGreen(pixels[i?*?iw?+?j?-?1]);
int?green5?=?cm.getGreen(pixels[i?*?iw?+?j]);
int?green6?=?cm.getGreen(pixels[i?*?iw?+?j?+?1]);
//?水平方向進(jìn)行中值濾波
if?(green4?=?green5)?{
if?(green5?=?green6)?{
green?=?green5;
}?else?{
if?(green4?=?green6)?{
green?=?green6;
}?else?{
green?=?green4;
}
}
}?else?{
if?(green4??green6)?{
green?=?green4;
}?else?{
if?(green5??green6)?{
green?=?green6;
}?else?{
green?=?green5;
}
}
}
//?int?blue2?=?cm.getBlue(pixels[(i?-?1)?*?iw?+?j]);
int?blue4?=?cm.getBlue(pixels[i?*?iw?+?j?-?1]);
int?blue5?=?cm.getBlue(pixels[i?*?iw?+?j]);
int?blue6?=?cm.getBlue(pixels[i?*?iw?+?j?+?1]);
//?int?blue8?=?cm.getBlue(pixels[(i?+?1)?*?iw?+?j]);
//?水平方向進(jìn)行中值濾波
if?(blue4?=?blue5)?{
if?(blue5?=?blue6)?{
blue?=?blue5;
}?else?{
if?(blue4?=?blue6)?{
blue?=?blue6;
}?else?{
blue?=?blue4;
}
}
}?else?{
if?(blue4??blue6)?{
blue?=?blue4;
}?else?{
if?(blue5??blue6)?{
blue?=?blue6;
}?else?{
blue?=?blue5;
}
}
}
pixels[i?*?iw?+?j]?=?alpha??24?|?red??16?|?green??8
|?blue;
}
}
//?將數(shù)組中的象素產(chǎn)生一個(gè)圖像
Image?tempImg?=?Toolkit.getDefaultToolkit().createImage(
new?MemoryImageSource(iw,?ih,?pixels,?0,?iw));
image?=?new?BufferedImage(tempImg.getWidth(null),
tempImg.getHeight(null),?BufferedImage.TYPE_INT_BGR);
image.createGraphics().drawImage(tempImg,?0,?0,?null);
return?image;
}
public?BufferedImage?getGrey()?{
ColorConvertOp?ccp?=?new?ColorConvertOp(
ColorSpace.getInstance(ColorSpace.CS_GRAY),?null);
return?image?=?ccp.filter(image,?null);
}
//?Brighten?using?a?linear?formula?that?increases?all?color?values
public?BufferedImage?getBrighten()?{
RescaleOp?rop?=?new?RescaleOp(1.25f,?0,?null);
return?image?=?rop.filter(image,?null);
}
//?Blur?by?"convolving"?the?image?with?a?matrix
public?BufferedImage?getBlur()?{
float[]?data?=?{?.1111f,?.1111f,?.1111f,?.1111f,?.1111f,?.1111f,
.1111f,?.1111f,?.1111f,?};
ConvolveOp?cop?=?new?ConvolveOp(new?Kernel(3,?3,?data));
return?image?=?cop.filter(image,?null);
}
//?Sharpen?by?using?a?different?matrix
public?BufferedImage?getSharpen()?{
float[]?data?=?{?0.0f,?-0.75f,?0.0f,?-0.75f,?4.0f,?-0.75f,?0.0f,
-0.75f,?0.0f?};
ConvolveOp?cop?=?new?ConvolveOp(new?Kernel(3,?3,?data));
return?image?=?cop.filter(image,?null);
}
//?11)?Rotate?the?image?180?degrees?about?its?center?point
public?BufferedImage?getRotate()?{
AffineTransformOp?atop?=?new?AffineTransformOp(
AffineTransform.getRotateInstance(Math.PI,
image.getWidth()?/?2,?image.getHeight()?/?2),
AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
return?image?=?atop.filter(image,?null);
}
public?BufferedImage?getProcessedImg()?{
return?image;
}
public?static?void?main(String[]?args)?throws?IOException?{
String?filePath="F:/k7qp5.png";
FileInputStream?fin?=?new?FileInputStream(filePath);
BufferedImage?bi?=?ImageIO.read(fin);
ImageInit?flt?=?new?ImageInit(bi);
flt.changeGrey();
flt.getGrey();
flt.getBrighten();
bi?=?flt.getProcessedImg();
String?pname?=?filePath.substring(0,?filePath.lastIndexOf("."));
File?file?=?new?File(pname?+?".jpg");
ImageIO.write(bi,?"jpg",?file);
}
}
較為常用的圖像二值化方法有:1)全局固定閾值;2)局部自適應(yīng)閾值;3)OTSU等。
局部自適應(yīng)閾值則是根據(jù)像素的鄰域塊的像素值分布來(lái)確定該像素位置上的二值化閾值。
這樣做的好處在于每個(gè)像素位置處的二值化閾值不是固定不變的,而是由其周?chē)徲蛳袼氐姆植紒?lái)決定的。
亮度較高的圖像區(qū)域的二值化閾值通常會(huì)較高,而亮度較低的圖像區(qū)域的二值化閾值則會(huì)相適應(yīng)地變小。不同亮度、對(duì)比度、紋理的局部圖像區(qū)域?qū)?huì)擁有相對(duì)應(yīng)的局部二值化閾值。
常用的局部自適應(yīng)閾值有:
1)局部鄰域塊的均值;
2)局部鄰域塊的高斯加權(quán)和。
椒鹽噪聲的話(huà)一般可以用中值濾波器去除, 中值濾波器很容易實(shí)現(xiàn), 依此遍歷圖像中每個(gè)像素點(diǎn), 每個(gè)像素點(diǎn)與其周?chē)?個(gè)點(diǎn)像素值做一下排序操作, 找到這九個(gè)點(diǎn)中的中值點(diǎn)賦給當(dāng)前遍歷點(diǎn)的像素就可以了, 算法很簡(jiǎn)單吧. 我這有c++的源碼, 樓主要想要的話(huà)發(fā)郵件到我的郵箱769569350@qq.com我可以把程序發(fā)給你.
網(wǎng)站名稱(chēng):二值化代碼java 二值化python
鏈接地址:http://bm7419.com/article46/dohjghg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、網(wǎng)站制作、App設(shè)計(jì)、全網(wǎng)營(yíng)銷(xiāo)推廣、用戶(hù)體驗(yàn)、小程序開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)