【iOS开发】图像处理之获取RGB

截取自雾霾检测机
-(float) returnGrayVale:(UInt8*)buffer withBytesPerRow:(size_t)bytesPerRow withX:(int)x withY:(int)y{
      UInt8*   tmp;
      tmp = buffer + y * bytesPerRow + x * 4; // RGBAの4つ値をもっているので、1ピクセルごとに*4してずらす
      
      // 获取图像的RGB
      UInt8 red,green,blue;
      red = *(tmp + 0);
      green = *(tmp + 1);
      blue = *(tmp + 2);
      
      //获取灰度值
      float grayValue = [self returnGrayValueWithRGB:red withGreen:green withBlue:blue];
      return grayValue;
}


-(float)ClearIndexBySobel:(UIImage*)sourceImage fromX:(int)Xpoint fromY:(int)Ypoint zoneWidth:(int)Width zoneHeihgt:(int)Height{
      
      int imageWidth = sourceImage.size.width;
      int imageHeight = sourceImage.size.height;
      
      //防止越界处理
      if (Xpoint<=0 || Xpoint>=imageWidth-1 || Ypoint<=0 || Ypoint>=imageHeight-1) {
            return -1;
      }
      
      
      //获取ref对象
      CGImageRef   imageRef;
      imageRef = sourceImage.CGImage;
      
      // 一行有多少个字节
      size_t                           bytesPerRow;
      bytesPerRow = CGImageGetBytesPerRow(imageRef);
      
      CGDataProviderRef    dataProvider;
      dataProvider = CGImageGetDataProvider(imageRef);
      
      CFDataRef    data;
      UInt8*         buffer;
      data = CGDataProviderCopyData(dataProvider);
      buffer = (UInt8*)CFDataGetBytePtr(data);

      //循环计算区域像素点获得总的梯度值
      float sum_grad = 0;
      int count = 0;
      for (int y = Ypoint; y < Ypoint + Height; y++) {
            for (int x = Xpoint; x < Xpoint + Width; x++) {
                  
                  //计算周边8个点的灰度值
                  float gray_00 = [self returnGrayVale:buffer withBytesPerRow:bytesPerRow withX:x-1 withY:y-1];
                  float gray_01 = [self returnGrayVale:buffer withBytesPerRow:bytesPerRow withX:x-1 withY:y];
                  float gray_02 = [self returnGrayVale:buffer withBytesPerRow:bytesPerRow withX:x-1 withY:y+1];
                  
                  float gray_10 = [self returnGrayVale:buffer withBytesPerRow:bytesPerRow withX:x withY:y-1];
                  float gray_12 = [self returnGrayVale:buffer withBytesPerRow:bytesPerRow withX:x withY:y+1];
                  
                  float gray_20 = [self returnGrayVale:buffer withBytesPerRow:bytesPerRow withX:x+1 withY:y-1];
                  float gray_21 = [self returnGrayVale:buffer withBytesPerRow:bytesPerRow withX:x+1 withY:y];
                  float gray_22 = [self returnGrayVale:buffer withBytesPerRow:bytesPerRow withX:x+1 withY:y+1];
                  

                  //计算梯度值
                  float grad_x = gray_00-gray_02+2*gray_10-2*gray_12+gray_20-gray_22;
                  float grad_y = gray_00+2*gray_01+gray_02-gray_20-2*gray_21-gray_22;
                  
                  float grad=sqrt(grad_x*grad_x+grad_y*grad_y);
                  sum_grad +=grad;
                  count++;
            }
      }
      
      if(count<=0){
            return 0;
      }
      
      float clearIndex = sum_grad / count;
      
      dataProvider = nil;
      data = nil;
      buffer = nil;
      
      return clearIndex;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值