图像模糊算法实现

本文探讨了图像模糊的基本原理,通过计算每个像素与其相邻像素的RGB平均值来实现。内容涉及边界点、内部点和顶点的不同处理方式,并提到多次模糊的效果。还提供了Pixel类的核心算法代码,同时附带了原图与五次模糊处理后的对比示例。
摘要由CSDN通过智能技术生成

将一幅图像看成是由像素组成的矩形,每个像素由R(Red),G(Green), B(Blue)三个值组成,图像模糊的原理就是算出每个像素相对于它相邻的多个元素的平均R,G,B值(每个R,G,B值分别处理), 其中边界点有六个相邻元素,内部点有9个相邻元素,顶点有4个相邻元素。可以对图像进行多次模糊。下面是核心算法实现:

 public PixImage boxBlur(int numIterations) {
    if(numIterations <= 0){
    	return this;
    }
    pixImages = new PixImage[numIterations + 1];
    pixImages[0] = this;
    for(int iteration = 1; iteration <= numIterations; iteration++){
    	pixImages[iteration] = new PixImage(this.getWidth(), this.getHeight());
    	for(int hIndex = 0; hIndex < this.getHeight(); hIndex++){
    		for(int wIndex = 0; wIndex < this.getWidth(); wIndex++){
    			short red = pixImages[iteration - 1].getPixel(wIndex, hIndex).getRed();
				short green = pixImages[iteration - 1].getPixel(wIndex, hIndex).getGreen();
				short blue = pixImages[iteration - 1].getPixel(wIndex, hIndex).getBlue();
				// deal with the 4 corners and 4 edges
    			if(hIndex == 0 || hIndex == this.getHeight() - 1 || wIndex == 0 || wIndex == this.getWidth() - 1){
    				//deal with the left-up corner
    				if(hIndex == 0 && wIndex == 0){
    					short red1 = pixImages[iteration - 1].getPixel(wIndex + 1, hIndex).getRed();
    					short green1 = pixImages[iteration - 1].getPixel(wIndex + 1, hIndex).getGreen();
    					short blue1 = pixImages[iteration - 1].getPixel(wIndex + 1, hIndex).getBlue();
    					short red2 = pixImages[iteration - 1].getPixel(wIndex + 1, hIndex + 1).getRed();
    					short green2 = pixImages[iteration - 1].getPixel(wIndex + 1, hIndex + 1).getGreen();
    					short blue2 = pixImages[iteration - 1].getPixel(wIndex + 1, hIndex + 1).getBlue();
    					short red3 = pixImages[iteration - 1].getPixel(wIndex, hIndex + 1).getRed(); 
    					short green3 = pixImages[iteration - 1].getPixel(wIndex, hIndex + 1).getGreen();
    					short blue3 = pixImages[iteration - 1].getPixel(wIndex, hIndex + 1).getBlue();
    					short aveRed = (short) ((red + red1 + red2 + red3) / 4);
    					short aveGreen = (short) ((green + green1 + green2 +green3) / 4);
    					short aveBlue = (short) ((blue + blue1 + blue2 + blue3) / 4);
    					pixImages[iteration].getPixel(wIndex, hIndex).setRed(aveRed);
    					pixImages[iteration].getPixel(wIndex, hIndex).setGreen(aveGreen);
    					pixImages[iteration].getPixel(wIndex, hIndex).setBlue(aveBlue);
    				}
    				//deal with the right-up corner
    				if(hIndex == 0 && wIndex == this.getWidth() - 1){
    					short red1 = pixImages[iteration - 1].getPixel(wIndex, hIndex + 1).getRed();
    					short green1 = pixImages[iteration - 1].getPixel(wIndex, hIndex + 1).getGreen();
    					short blue1 = pixImages[iteration - 1].getPixel(wIndex, hIndex + 1).getBlue();
    					short red2 = pixImages[iteration - 1].getPixel(wIndex - 1, hIndex + 1).getRed();
    					short green2 = pixImages[iteration - 1].getPixel(wIndex - 1, hIndex + 1).getGreen();
    					short blue2 = pixImages[iteration - 1].getPixel(wIndex - 1, hIndex + 1).getBlue();
    					short red3 = pixImages[iteration - 1].getPixel(wIndex - 1, hIndex).getRed(); 
    					short green3 = pixImages[iteration - 1].getPixel(wIndex - 1, hIndex).getGreen();
    					short blue3 = pixImages[iteration - 1].getPixel(wIndex - 1, hIndex).getBlue();
    					short aveRed = (short) ((red + red1 + red2 + red3) / 4);
    					short aveGreen = (short) ((green + green1 + green2 +green3) / 4);
    					short aveBlue = (short) ((blue + blue1 + blue2 + blue3) / 4);
    					pixImages[iteration].getPixel(wIndex, hIndex).setRed(av
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值