图像处理并获取RGB-创新互联-成都快上网建站

图像处理并获取RGB-创新互联

截取自雾霾检测机

创新互联建站主要从事成都网站制作、做网站、网页设计、企业做网站、公司建网站等业务。立足成都服务宜黄,十载网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575

-(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 p_w_picpathWidth = sourceImage.size.width;

    int p_w_picpathHeight = sourceImage.size.height;

    //防止越界处理

    if (Xpoint<=0 || Xpoint>=p_w_picpathWidth-1 || Ypoint<=0 || Ypoint>=p_w_picpathHeight-1) {

        return -1;

    }

    //获取ref对象

    CGImageRef  p_w_picpathRef;

    p_w_picpathRef = sourceImage.CGImage;

    // 一行有多少个字节

    size_t                  bytesPerRow;

    bytesPerRow = CGImageGetBytesPerRow(p_w_picpathRef);

    CGDataProviderRef   dataProvider;

    dataProvider = CGImageGetDataProvider(p_w_picpathRef);

    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;

}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


文章名称:图像处理并获取RGB-创新互联
路径分享:http://kswjz.com/article/poghe.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流