2014-04-29 53 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); 
} 
相關問題