zoukankan      html  css  js  c++  java
  • 图像像素加减乘除操作

    主要代码如下:
    package chapter5;

    import chapter4.*;

    import java.awt.image.BufferedImage;
    import java.util.Random;

    /**

    • Created by LENOVO on 18-1-30.
      */
      public class PMMDFilter extends AbstractBufferedImageOp {
      public static final int PLUS = 1;
      public static final int MINUS = 2;
      public static final int MULTIPLE = 4;

      private Random rnd;
      private double range;
      private int type;

      public PMMDFilter(){
      type = MINUS;
      rnd = new Random();
      range =25.0;
      }
      public BufferedImage filter(BufferedImage src,BufferedImage dest){
      int width = src.getWidth();
      int height = src.getHeight();
      if(dest == null){
      dest = creatCompatibleDestImage(src,null);
      }
      int[] inPixels = new int[widthheight];
      int[] outPixels = new int[width
      height];
      getRGB(src,0,0,width,height,inPixels);
      int index = 0;
      int centerX = width/2;
      int centerY = height/2;
      double maxDistance = Math.sqrt(centerXcenterX+centerYcenterY);
      for(int row=0;row<height;row++){
      int ta = 0,tr = 0,tg = 0,tb = 0;
      for(int col=0;col<width;col++){
      index = row*width+col;
      ta = (inPixels[index] >> 24) & 0xff;
      tr = (inPixels[index] >> 16) & 0xff;
      tg = (inPixels[index] >> 8) & 0xff;
      tb = (inPixels[index]) & 0xff;

               int[] rgb = new int[]{tr,tg,tb};
               if(type == PLUS){
                   rgb = plus(rgb);
               }
               if(type == MINUS){
                   int pcol = col-1;//当前像素之前一个像素
                   if(pcol < 0 || pcol >= width){
                        pcol = 0;
                   }
                   int index2 = row*width +pcol;
                   rgb = minus(rgb,inPixels[index2]);
               }
               if(type == MULTIPLE){
                   rgb = multiple(rgb,maxDistance,centerX,centerY,row,col);
               }
               tr = rgb[0];
               tg = rgb[1];
               tb = rgb[2];
               outPixels[index] = (ta <<24 ) | (clamp(tr) <<16) | (clamp(tg) << 8) | clamp(tb);
           }
       }
       setRGB(dest,0,0,width,height,outPixels);
       return  dest;
      

      }
      //像素相加操作
      private int[] plus(int[] rgb){
      rgb[0] = addNoise(rgb[0]);
      rgb[1] = addNoise(rgb[1]);
      rgb[2] = addNoise(rgb[2]);
      return rgb;
      }
      //图像加噪
      private int addNoise(int p){
      boolean valid = false;
      do{
      int ran = (int) Math.round(rnd.nextGaussian()*range);//符合标准正态分布
      int v = p+ran;
      valid = v>=0 && v<=255;//判断像素值是否有效
      if (valid) p=v;
      }while(!valid);
      return p;
      }
      //像素相减操作
      private int[] minus (int[] rgb,int p){
      int tr = (p >> 16) & 0xff;
      int tg = (p >> 8) & 0xff;
      int tb =p & 0xff;
      rgb[0] = clamp(rgb[0]-tr);
      rgb[1] = clamp(rgb[1] -tr);
      rgb[2] = clamp(rgb[2]-tb);
      return rgb;
      }

      //像素相乘操作
      private int[] multiple(int[] rgb,double maxDistance,int cx,int cy,int row,int col){
      double scale = 1.0-getDistance(cx,cy,row,col)/maxDistance;
      scale = scalescale;
      rgb[0] = (int) (scale
      rgb[0]);
      rgb[1] = (int) (scalergb[1]);
      rgb[2] = (int) (scale
      rgb[2]);
      return rgb;

      }
      //获取像素与中心点之间的距离
      private double getDistance(int cx,int cy,int px, int py){
      double xx = (cx-px)(cx-px);
      double yy = (cy-py)
      (cy-py);
      return (int)Math.sqrt(xx+yy);
      }
      }
      测试代码同上。

  • 相关阅读:
    printf输出函数
    死循环的3种编写方案
    volatile 和const 变量的使用
    arm mov 指令
    arm ldr 指令
    arm str 指令
    Ztree-
    端口占用问题:java.net.BindException: Address already in use: bind
    模块和包
    序列化模块:json、pickle、shelve
  • 原文地址:https://www.cnblogs.com/bigdream6/p/8385682.html
Copyright © 2011-2022 走看看