大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
//运行以下程序即可
站在用户的角度思考问题,与客户深入沟通,找到禄劝网站设计与禄劝网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站建设、成都网站设计、企业官网、英文网站、手机端网站、网站推广、域名与空间、网络空间、企业邮箱。业务覆盖禄劝地区。
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();
}
// 设定二值化的域值,默认值为100
int grey = 100;
// 对图像进行二值化处理,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; // 通过移位重新构成某一点像素的RGB值
}
// 将数组中的象素产生一个图像
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();
}
// 对图像进行中值滤波,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]);
// 水平方向进行中值滤波
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]);
// 水平方向进行中值滤波
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]);
// 水平方向进行中值滤波
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;
}
}
// 将数组中的象素产生一个图像
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)局部自适应阈值;3)OTSU等。
局部自适应阈值则是根据像素的邻域块的像素值分布来确定该像素位置上的二值化阈值。
这样做的好处在于每个像素位置处的二值化阈值不是固定不变的,而是由其周围邻域像素的分布来决定的。
亮度较高的图像区域的二值化阈值通常会较高,而亮度较低的图像区域的二值化阈值则会相适应地变小。不同亮度、对比度、纹理的局部图像区域将会拥有相对应的局部二值化阈值。
常用的局部自适应阈值有:
1)局部邻域块的均值;
2)局部邻域块的高斯加权和。
椒盐噪声的话一般可以用中值滤波器去除, 中值滤波器很容易实现, 依此遍历图像中每个像素点, 每个像素点与其周围的8个点像素值做一下排序操作, 找到这九个点中的中值点赋给当前遍历点的像素就可以了, 算法很简单吧. 我这有c++的源码, 楼主要想要的话发邮件到我的邮箱769569350@qq.com我可以把程序发给你.