OpenCv 可实现的特效

本文介绍了如何在Android中使用OpenCV库进行图像处理,包括灰度化、二值化、边沿轮廓检测、素描、怀旧色、连环画、冰冻和熔铸等滤镜效果的实现。通过静态初始化OpenCV并调用其API,可以在Android应用中实时处理图片,创建各种视觉风格。
摘要由CSDN通过智能技术生成
灰度化方法
        二值化滤镜
边沿轮廓检测
        素描滤镜
怀旧色滤镜
        连环画
冰冻滤镜
        熔铸滤镜
浮雕滤镜

OpenCv  for Android 步骤

去OpenCv 下载android 版SDK   导入Module  

直接静态初始化 

static {
    if(!OpenCVLoader.initDebug()) {
        Log.d("My App", "Unable to load OpenCV");
    } else {
        Log.d("My App", "OpenCV loaded");
    }
}

执行以上步骤后即可使用对应的api   

************************   该操作比较耗时,注意开启子线程进行对象获取   *********************************

详细代码如下:   具体运行效果自行复制粘贴

  //灰度化方法
private   Bitmap RGB2Gray(Bitmap photo) {
      Mat RGBMat = new Mat();
      Bitmap grayBitmap = Bitmap.createBitmap(photo.getWidth(), photo.getHeight(), Bitmap.Config.RGB_565);
      Utils.bitmapToMat(photo, RGBMat);//convert original bitmap to Mat, R G B.
      Imgproc.cvtColor(RGBMat, RGBMat, Imgproc.COLOR_RGB2GRAY);//rgbMat to gray grayMat
      Utils.matToBitmap(RGBMat, grayBitmap);
      return grayBitmap;
  }

  //二值化滤镜
private   Bitmap theshold(Bitmap photo){
      Mat mat = new Mat();
      Bitmap thes = Bitmap.createBitmap(photo.getWidth(), photo.getHeight(), Bitmap.Config.ARGB_8888);
      Utils.bitmapToMat(photo, mat);
      Imgproc.cvtColor(mat,mat,Imgproc.COLOR_RGB2GRAY);
      Core.bitwise_not(mat,mat);
      Imgproc.threshold(mat,mat,100,255,Imgproc.THRESH_BINARY_INV);
      Utils.matToBitmap(mat,thes);
      return thes;
  }


  //轮廓  边沿轮廓检测
 private Bitmap Lunkuo(Bitmap photo){
      Mat mat = new Mat();
      Mat Cmat = new Mat();
      Mat Bmat = new Mat();
      Bitmap cartton = Bitmap.createBitmap(photo.getWidth(), photo.getHeight(), Bitmap.Config.ARGB_8888);
      Utils.bitmapToMat(photo, mat);
      Imgproc.Canny(mat,Cmat,50,100);
      Core.bitwise_not(Cmat,Cmat);
      Utils.matToBitmap(Cmat, cartton);
      return cartton;
  }



  //素描滤镜
  Bitmap SuMiao(Bitmap photo){
      Mat SM = new Mat();
      Mat SM1 = new Mat();
      Bitmap sumiaoMap = Bitmap.createBitmap(photo.getWidth(), photo.getHeight(), Bitmap.Config.ARGB_8888);
      Bitmap SMB = Bitmap.createBitmap(photo.getWidth(), photo.getHeight(), Bitmap.Config.ARGB_8888);
      Bitmap SMB1 = Bitmap.createBitmap(photo.getWidth(), photo.getHeight(), Bitmap.Config.ARGB_8888);
      Utils.bitmapToMat(photo, SM);
      //灰度化
      Imgproc.cvtColor(SM, SM, Imgproc.COLOR_RGB2GRAY);
      //颜色取反
      Core.bitwise_not(SM,SM1);
      //高斯模糊
      Imgproc.GaussianBlur(SM1,SM1,new Size(13,13),0,0);
      Utils.matToBitmap(SM, SMB);
      Utils.matToBitmap(SM1, SMB1);
      for(int i = 0;i<SMB.getWidth();i++){
          for( int j = 0;j<SMB.getHeight();j++){
              int A = SMB.getPixel(i,j);
              int B = SMB1.getPixel(i,j);
              int CR = colordodge(Color.red(A),Color.red(B));
              int CG = colordodge(Color.green(A),Color.red(B));
              int CB = colordodge(Color.blue(A),Color.blue(B));
              sumiaoMap.setPixel(i,j,Color.rgb(CR,CG,CB));
          }
      }
      return sumiaoMap;
  }

  private int colordodge(int A, int B) {
      return  Math.min(A+(A*B)/(255-B+1),255);
  }




  //怀旧色滤镜
private   Bitmap HuaiJiu(Bitmap photo){
      Bitmap huaijiu = Bitmap.createBitmap(photo.getWidth(), photo.getHeight(), Bitmap.Config.ARGB_8888);
      for(int i = 0;i<photo.getWidth();i++){
          for( int j = 0;j<photo.getHeight();j++){
              int A = photo.getPixel(i,j);
              int AR =(int)(0.393*Color.red(A) + 0.769*Color.green(A) + 0.189*Color.blue(A));
              int AG =(int)(0.349*Color.red(A) + 0.686*Color.green(A) + 0.168*Color.blue(A));
              int AB =(int)(0.272*Color.red(A) + 0.534*Color.green(A) + 0.131*Color.blue(A));
              AR = AR > 255 ? 255 : AR;
              AG = AG > 255 ? 255 : AG;
              AB = AB > 255 ? 255 : AB;
              huaijiu.setPixel(i,j,Color.rgb(AR,AG,AB));
          }
      }
      return huaijiu;
  }


  //连环画
  private Bitmap LianHuanHua(Bitmap photo){
      Bitmap lianhuanhua = Bitmap.createBitmap(photo.getWidth(), photo.getHeight(), Bitmap.Config.ARGB_8888);
      for(int i = 0;i<photo.getWidth();i++){
          for( int j = 0;j<photo.getHeight();j++){
              int A = photo.getPixel(i,j);
              int AR =Math.abs(Color.red(A) - Color.blue(A) + Color.green(A)+ Color.green(A)  ) * Color.red(A) / 256;
              int AG =Math.abs(Color.red(A) - Color.green(A) + Color.blue(A) + Color.blue(A)) * Color.red(A) / 256;
              int AB =Math.abs(Color.red(A) - Color.blue(A) + Color.blue(A) + Color.blue(A)) * Color.green(A) / 256;
              AR = AR > 255 ? 255 : AR;
              AG = AG > 255 ? 255 : AG;
              AB = AB > 255 ? 255 : AB;
              lianhuanhua.setPixel(i,j,Color.rgb(AR,AG,AB));
          }
      }
      return lianhuanhua;
  }


  //冰冻滤镜
private   Bitmap BingDong(Bitmap photo){
      Bitmap bingdong  = Bitmap.createBitmap(photo.getWidth(), photo.getHeight(), Bitmap.Config.ARGB_8888);
      for(int i = 0;i<photo.getWidth();i++){
          for( int j = 0;j<photo.getHeight();j++){
              int A = photo.getPixel(i,j);
              int AR =(Color.red(A)-Color.blue(A)-Color.green(A))*3/2;
              int AG =(Color.green(A)-Color.blue(A)-Color.red(A))*3/2;
              int AB =(Color.blue(A)-Color.red(A)-Color.green(A))*3/2;
              AR = AR > 255 ? 255 : AR;
              AG = AG > 255 ? 255 : AG;
              AB = AB > 255 ? 255 : AB;
              bingdong.setPixel(i,j,Color.rgb(AR,AG,AB));
          }
      }
      return bingdong;
  }


  //熔铸滤镜
private   Bitmap RongZhu(Bitmap photo){
      Bitmap rongzhu  = Bitmap.createBitmap(photo.getWidth(), photo.getHeight(), Bitmap.Config.ARGB_8888);
      for(int i = 0;i<photo.getWidth();i++){
          for( int j = 0;j<photo.getHeight();j++){
              int A = photo.getPixel(i,j);
              int AR =Color.red(A)*128/(Color.blue(A)+Color.green(A)+1);
              int AG =Color.green(A)*128/(Color.blue(A)+Color.red(A)+1);
              int AB =Color.blue(A)*128/(Color.red(A)+Color.green(A)+1);
              AR = AR > 255 ? 255 : AR;
              AG = AG > 255 ? 255 : AG;
              AB = AB > 255 ? 255 : AB;
              rongzhu.setPixel(i,j,Color.rgb(AR,AG,AB));
          }
      }
      return rongzhu;
  }


     
  //浮雕滤镜
private   Bitmap FuDiao(Bitmap photo){
      Bitmap bingdong  = Bitmap.createBitmap(photo.getWidth(), photo.getHeight(), Bitmap.Config.ARGB_8888);
      for(int i = 1;i<photo.getWidth()-1;i++){
          for( int j = 1;j<photo.getHeight()-1;j++){
              int A = photo.getPixel(i-1,j-1);
              int B = photo.getPixel(i+1,j+1);
              int AR =Color.red(B)-Color.red(A)+128;
              int AG =Color.green(B)-Color.green(A)+128;
              int AB =Color.blue(B)-Color.blue(A)+128;
              AR = AR > 255 ? 255 : AR;
              AG = AG > 255 ? 255 : AG;
              AB = AB > 255 ? 255 : AB;
              bingdong.setPixel(i,j,Color.rgb(AR,AG,AB));
          }
      }
      return bingdong;
  }

配置路径https://www.cnblogs.com/dingchao823/p/5392777.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值