图像处理之关键代码——Android

一、软件设计的基本原理和采用的主要方法(算法)与技术

算法:图像读入后,把数据放到数组中去。

灰度化用的是最终像素=0.3*R+0.59*B+0.11*G这个公式

反色使用255减去原像素得到最终像素

怀旧效果用的是0.393*R+0.769*G+0.189*B

高斯模糊用的是矩阵的卷积远算,算子是1/16{1,2,1,2,4,2,1,2,1},高斯模糊在模糊的同时有考虑每个像素的比重,所以边缘得以保留下来。

拉普拉斯用的3*3的算子,{1,1,1,1-8,1,1,1,1}

素描效果的实现参考了ps中的图层,第一个图层是灰度图,第二个图层是灰度图反相,然后用最小值滤波,最后是两个图层叠加。

图层的叠加,以灰度图为基色,处理过的图层为混合色,结果色=基色+基色*混合色/(255-混合色),最终会使基色图朝变亮的方向发展,对比度增加。

 

 

四、实现的过程与步骤

 

(用visio画出软件架构并解释)

 

五、遇到的困难与获得的主要成果

困难:Android平台的Bitmap的一些特性不太熟悉,Bitmap在内存中的存贮格式不太清楚,以及canvas的一些属性和ColorMatrix第一次接触。

主要成果:Bitmap加深了了解,Android平台绘图的基础知识有了一个大概的全貌,细节方面还要查api。

六、测试与运行记录

测试效果、结果

左边是ps上做的效果,右边是我的软件实现的效果。

七、结果分析与小结

开发任务基本上达到,图像处理的基础算法有了一个比较深入的了解,特别是大概实现了图像素描的算法,滤镜开发的基础算法也比较熟悉了,总的来说,收获很大。

八、附录(软件配置、含注释的程序模块核心代码)

public staticBitmap oldbmp(Bitmap bmp){
     intwidth=bmp.getWidth();
     intheight=bmp.getHeight();
     
    Config config=bmp.getConfig();
     Bitmap bitmap=bmp.createBitmap(width, height,Bitmap.Config.RGB_565);//创建一个新的bitmap
     intpixColor;
     intpixR,pixG,pixb;
     intnewColor;
     intnewr,newg,newb;
     int[]pixs=new int[width*height];
     bmp.getPixels(pixs, 0, width, 0, 0, width,height);//把bitmap中的像素提取到pixs数组中去
     for(int i=0;i<height;i++){
         
         for(int j=0;j<width;j++){
             pixColor=pixs[i*width+j];//分别得到rgb三个通道
             pixR=Color.red(pixColor);
             pixG=Color.green(pixColor);
             pixb=Color.blue(pixColor);
             newr=(int)(0.393 * pixR + 0.769 * pixG + 0.189* pixb);//怀旧效果的公式
             newb=(int)(0.272* pixR + 0.534 * pixG + 0.131 * pixb);
             newg=(int)(0.349 * pixR + 0.686 * pixG + 0.168* pixb);
             newColor=Color.argb(255, newr>255?255:newr,newg>255?255:newg, newb>255?255:newb);
             pixs[i*width+j]=newColor;
         }
     }
     bitmap.setPixels(pixs, 0, width, 0, 0, width,height);
     return bitmap;
 }
 
 
public staticBitmap gray(Bitmap bmp){
    int width=bmp.getWidth();
    int height=bmp.getHeight();
 Bitmap bitmap=bmp.createBitmap(width, height,Bitmap.Config.RGB_565);
     
     Canvas canvas=new Canvas(bitmap);//创建一个画布
     Paintpaint=new Paint();//创建一个画笔
     float[] data={(float) 0.3,(float) 0.59,(float)0.11,0,0,(float) 0.3,(float) 0.59,(float) 0.11,0,0,(float) 0.3,(float)0.59,(float) 0.11,0,0,(float) 0.3,(float) 0.59,(float) 0.11,0,0};//灰度图的颜色过滤矩阵
     ColorMatrix coma=new ColorMatrix(data);//以data为参数创建颜色过滤矩阵
     ColorMatrixColorFilter filter=new  ColorMatrixColorFilter(coma);//颜色过滤器
     paint.setColorFilter(filter);
     canvas.drawBitmap(bmp, 0, 0, paint);/在绘制bitmap
     return bitmap;
}
 
for(inti=1;i<height-1;i++){
        for(intj=1;j<width-1;j++){
           
            count=0;
            newr=newg=newb=0;
            for(int m=-1;m<2;m++){//高斯模糊的算法,3*3矩阵,用的算子是算子是1/16{1,2,1,2,4,2,1,2,1},高斯模糊在模糊的同时有考虑每个像素的比重,所以边缘得以保留下来
            for(int n=-1;n<2;n++){  
                pixColor=pixs[(i+m)*width+j+n];
            r=Color.red(pixColor);
            g=Color.green(pixColor);
            b=Color.blue(pixColor);
            newr+=r*data[count];//3*3矩阵每一个像素分别相乘
            newb+=b*data[count];
            newg+=g*data[count];
            count++;
            }
            }
            //temp=(int)sum;
            newr/=size;//除以16
            newb/=size;
            newg/=size;
            newr=Math.min(255, Math.max(0, newr));
            newg=Math.min(255, Math.max(0, newg));
            newb=Math.min(255, Math.max(0, newb));
            temp=Color.argb(255, newr, newg, newb);
            newpixs[i*width+j]=temp;
 
 
for(inti=1;i<height-1;i++){
    for(int j=1;j<width-1;j++){
        temp=255;
   
        for(intm=-1;m<2;m++){
            for(int n=-1;n<2;n++){//最小值滤波,3*3矩阵中的最小值  
                pixcolor=pixs[(i+m)*width+j+n];
            temp=temp>=pixcolor?pixcolor:temp;
           
    }
    }
        newpixs[i*width+j]=temp;
    }
}
 
int[]Cdata=new int[width*height];//原像素
int[]fdata=new int[width*height];//反色的像素
int[]temp=new int[width*height];
bmp.getPixels(Cdata,0, width, 0, 0, width, height);
for(inti=0;i<height;i++){
    for(int j=0;j<width;j++){
    pixColor=Cdata[i*width+j];//原像素
    r=Color.red(pixColor);
    g=Color.green(pixColor);
    b=Color.blue(pixColor);
    nr=255-r;//变反
    nb=255-b;
    ng=255-g;
    newColor=Color.argb(255, nr, ng, nb);
    fdata[i*width+j]=newColor;
   
   
    }
}
int minColor;
inttempr,tempb,tempg;
 
for(inti=1;i<height-1;i++){
    for(int j=1;j<width-1;j++){
        tempr=tempb=tempg=255;
        for(intm=-1;m<2;m++){
            for(int n=-1;n<2;n++){
//反色像素用最小值滤波
                pixColor=fdata[(i+m)*width+j+n];
            r=Color.red(pixColor);
    g=Color.green(pixColor);
    b=Color.blue(pixColor);
    tempr=r>tempr?tempr:r;
    tempg=g>tempg?tempg:g;
    tempb=b>tempb?tempb:b;
   
    //反色像素用最小值滤波
   
        }
        }
        pixColor=Cdata[i*width+j];
        r=Color.red(pixColor);
        g=Color.green(pixColor);
        b=Color.blue(pixColor);
        //原图像与反色并且最小值滤波的图像图层叠加,用颜色减淡模式
        tempr=(int) (r+r*tempr/(256-tempr));
        endr=tempr>=255?255:tempr;//颜色减淡的模式,结果色=基色+基色*混合色/(255—混合色)
图层的叠加,以灰度图为基色,处理过的图层为混合色,结果色=基色+基色*混合色/(255-混合色),最终会使基色图朝变亮的方向发展,对比度增加。
        tempg=(int)(g+g*tempg/(256-tempg));
        endg=tempg>=255?255:tempg;
        tempb=(int)(b+b*tempb/(256-tempb));
       endb=tempb>=255?255:tempb;
       
       
        minColor=Color.argb(255,endr, endg, endb);
   
    temp[i*width+j]=minColor;
        //min[Color=Color.argb(255,, green, blue)
       
    }
}   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值