图像运算(二)——减法(Subtraction)

描述:图像的减法即两幅输入图像同意位置像素相减,得到一个输出图像的过程。

公式描述:

O(i,j) = I1(i,j) - I2(i,j)  OR  O(i,j) = |I1(i,j) - I2(i,j)|

注意1:常值问题,输入图像也可以有一个是常量,即 O(i,j) = I(i,j) - C   OR  O(i,j) =| I(i,,j) - C|

注意2:下限问题,如果两幅图像相减得出的像素低于0,则一律统统归为0.

注意3:循环问题,如果图像格式支持循环问题,则不存在下限问题,即如果输出的像素为-20,则,系统自动把它变成236.


Code:

Similarity, displayed to the kernel function:

 /**
   *Applies the image difference operator on the specified image arrays, with the specified offset and scale value
   *@param src1_1d The first source image as a pixel array
   *@param src2_1d The second source image as a pixel array
   *@param width width of the destination image in pixels
   *@param height height of the destination image in pixels
   *@param oset The offset value
   *@param scale The scale value
   *@param reverse false if image2 - image 1
   *@return A pixel array containing the difference of the two input images
   */

  public int [] imagedifference(int [] src1_1d, int [] src2_1d, int width, int height, 
				float oset, float scale, boolean reverse){
    
    int place1 = -1;
    int place2 = -1;
    int src1rgb = 0;
    int src2rgb = 0;
    int diff = 0;

    d_w = width;
    d_h = height;
   

    dest_1d = new int[d_w*d_h];
   
    boolean firstwider = false;
    boolean secondwider = false;
    int wrap;
    
    if (i1_w > d_w){
      wrap =   ((i1_w + 1) - d_w);
      firstwider = true;
    } else if (i2_w > d_w){
      wrap =    ((i2_w + 1) - d_w);
      secondwider = true;
      
    } else {
      wrap = 0;
    }
   
    if (wrap == 0) {
      for (int i=0; i< dest_1d. length ; i++){
	src2rgb = src2_1d[i] & 0x000000ff;
	src1rgb = src1_1d[i] & 0x000000ff;
	if (!reverse) {
	  diff = (int)  ((scale * ((float) src2rgb - (float) src1rgb )) + oset);
	} else {
	  diff = (int)  ((scale * ((float) src1rgb - (float) src2rgb )) + oset);
	}

	if (diff < 0){
	  diff = 0;
	} else if  (diff > 255){
	  diff = 255;
	}
      

	dest_1d[i ] =  0xff000000 | (diff + (diff << 16) + (diff << 8));
       
      }
   
      return dest_1d;
  
    }
    else {
     
      for (int i=0; i< dest_1d. length ; i++){
   	
      if ((i %d_w  ) == 0 ) {
	if ( i == 0 ){
	  place1 = 0;
	  place2 = 0;
	} else if (secondwider) {
	  place2 = place2 + wrap;
	  place1 ++;
	} else {
	  place1 = place1 + wrap;
	  place2 ++;
	}
      } else{
	place2 ++;
	place1 ++;
      }
    
      src2rgb = src2_1d[place2] & 0x000000ff;
      src1rgb = src1_1d[place1] & 0x000000ff;
      
      if (!reverse) {
	diff = (int)  ((scale * ((float) src2rgb - (float) src1rgb )) + oset);
      } else {
	diff = (int)  ((scale * ((float) src1rgb - (float) src2rgb )) + oset);
      }
      //clip to 0 ... 255
      if (diff < 0){
	diff = 0;
      } else if  (diff > 255){
	diff = 255;
      }
      
      
      dest_1d[i ] =  0xff000000 | (diff + (diff << 16) + (diff << 8));
      }
      return dest_1d;
    }
  }

Input Image




Output Image




总结:减法操作作为图像基本运算中的一员,它的作用也是不容忽视的,特别是特征的提取这一领域,它的作用的巨大的。

你可能感兴趣的:(#,Image,Processing-Base)