2014-04-29 121 views
3

我正在做一个图像处理iOS应用程序,我们有一个大的图像(例如:大小将是2000x2000)。假设图像是完全黑色的,除了图像的一部分是不同的颜色(可以说该区域的大小是200x200)。如何在iOS上的图像中找到某个特定颜色的区域?

SI想要计算该不同颜色区域的开始和结束位置。我怎样才能做到这一点?

+0

使用opencv http://opencv.org/ – iphonic

+1

也许这可能会帮助你。使用OpenCV。 http://stackoverflow.com/questions/8667818/opencv-c-obj-c-detecting-a-sheet-of-paper-square-detection –

+0

你可以检查[this](https://github.com/BradLarson/GPUImage)。 – mownier

回答

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); 
} 
相关问题