如何在iOS上查找图像中特定颜色的区域?

3
我正在开发一款图像处理的iOS应用程序,其中我们有一张大图片(例如大小为2000x2000)。假设这张图片完全是黑色的,除了图片的某个部分是不同的颜色(比如说该区域的大小是200x200)。
我想计算出那个不同颜色区域的起始和结束位置。我该如何实现?

使用OpenCV http://opencv.org/ - iphonic
1
也许这可以帮助你。使用OpenCV。https://dev59.com/aWoy5IYBdhLWcg3wQr5i - Pushpak Narasimhan
你可以查看这个 - mownier
1个回答

0
这是一种简单的方法,允许CPU从UIImage获取像素值。步骤如下:
  • 为像素分配一个缓冲区
  • 使用缓冲区作为备份存储器创建位图内存上下文
  • 将图像绘制到上下文中(将像素写入缓冲区)
  • 检查缓冲区中的像素
  • 释放缓冲区和相关资源

- (void)processImage:(UIImage *)input
{
    int width  = input.size.width;
    int height = input.size.height;

    // allocate the pixel buffer
    uint32_t *pixelBuffer = calloc( width * height, sizeof(uint32_t) );

    // create a context with RGBA pixels
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate( pixelBuffer, width, height, 8, width * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast );

    // invert the y-axis, so that increasing y is down
    CGContextScaleCTM( context, 1.0, -1.0 );
    CGContextTranslateCTM( context, 0, -height );

    // draw the image into the pixel buffer
    UIGraphicsPushContext( context );
    [input drawAtPoint:CGPointZero];
    UIGraphicsPopContext();

    // scan the image
    int x, y;
    uint8_t r, g, b, a;
    uint8_t *pixel = (uint8_t *)pixelBuffer;

    for ( y = 0; y < height; y++ )
        for ( x = 0; x < height; x++ )
        {
            r = pixel[0];
            g = pixel[1];
            b = pixel[2];
            a = pixel[3];

            // do something with the pixel value here

            pixel += 4;
        }

    // release the resources
    CGContextRelease( context );
    CGColorSpaceRelease( colorSpace );
    free( pixelBuffer );
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接