2011-05-13 41 views
1

問:我正在尋找使用iPhone相機拍攝照片,然後用另一張照片替換該照片中的綠色屏幕。iPhone App綠色屏幕替換

潛入此的最佳方法是什麼?我無法在網上找到許多資源。

在此先感謝!

回答

2

從概念上講,所有你需要做的是循環通過手機拍攝的照片的像素數據,並且對於不在某個綠色範圍內的每個像素,將該像素複製到背景圖像上的相同位置。

這裏是一個例子,我從keremic的回答到another stackoverflow問題的修改。 注意:這是未經測試,只是爲了給你的技術的思想,將工作

//Get data into C array 
CGImageRef image = [UIImage CGImage]; 
NSUInteger width = CGImageGetWidth(image); 
NSUInteger height = CGImageGetHeight(image); 
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
NSUInteger bytesPerPixel = 4; 
NSUInteger bytesPerRow = bytesPerPixel_ * width; 
NSUInteger bitsPerComponent = 8; 
unsigned char *data = malloc(height * width * bytesPerPixel); 
// you will need to copy your background image into resulting_image_data. 
// which I am not showing here 
unsigned char *resulting_image_data = malloc(height * width * bytesPerPixel); 
CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 
CGColorSpaceRelease(colorSpace); 

CGContextDrawImage(context, CGRectMake(0, 0, width, height)); 
CGContextRelease(context); 

//loop through each pixel 
for(int row = 0; row < height; row++){ 
    for(int col = 0; col < width*bytesPerPixel; col=col+4){ 
    red = data[col]; 
    green = data[col + 1]; 
    blue = data[col + 2]; 
    alpha = data[col + 3]; 
    // if the pixel is within a shade of green 
    if(!(green > 250 && red < 10 && blue < 10)){ 
     //copy them over to the background image 
     resulting_image_data[row*col] = red; 
     resulting_image_data[row*col+1] = green; 
     resulting_image_data[row*col+2] = blue; 
     resulting_image_data[row*col+3] = alpha; 
    } 
    } 
} 

//covert resulting_image_data into a UIImage 
+2

如果你做這種方式,那麼你可以通過在周圍像素找你循環通過它做一些改進。如果大部分周圍的像素都是綠色的,但當前的像素不是綠色的,它可能是綠色屏幕的一部分(但由於任何原因像素看起來不綠)。此外,使用類似的技術,你可能想要平滑任何檢測到的綠色屏幕部件的邊緣 - 它可能看起來更好一些:) – 2011-05-14 04:08:20

+0

+1喬丹。好主意! – 2011-05-14 19:39:06

0

看看編譯OpenCV for iPhone - 不是一件容易的事,但它可以讓你訪問一個真正偉大的圖像處理工具的整個庫。

我正在使用openCV開發我現在正在開發的應用程序(並非所有與您不同的應用程序) - 對於您要做的事情,openCV將是一個很好的解決方案,儘管它需要一點點學習等等。一旦你有OpenCV的工作,去除綠色的實際任務不應該太難。

編輯:這個鏈接將是一個有用的資源,如果你決定使用OpenCV的:Compiling OpenCV for iOS