【发布时间】:2011-05-14 07:38:27
【问题描述】:
问:我希望使用 iPhone 相机拍照,然后用另一张照片替换该照片中的绿屏。
深入研究的最佳方法是什么?我在网上找不到很多资源。
提前致谢!
【问题讨论】:
标签: iphone core-animation core-image
问:我希望使用 iPhone 相机拍照,然后用另一张照片替换该照片中的绿屏。
深入研究的最佳方法是什么?我在网上找不到很多资源。
提前致谢!
【问题讨论】:
标签: iphone core-animation core-image
从概念上讲,您需要做的就是循环遍历手机拍摄的照片的像素数据,对于每个不在绿色特定范围内的像素,将像素复制到背景图像上的相同位置.
这是我根据 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
【讨论】:
看看为 iPhone 编译 OpenCV - 这不是一件容易的事,但它让您可以访问整个非常棒的图像处理工具库。
我正在将 openCV 用于我目前正在开发的应用程序(与您的应用程序并不完全不同) - 对于您正在尝试做的事情,openCV 将是一个很好的解决方案,尽管它需要一些学习等等。一旦你让 OpenCV 工作,去除绿色的实际任务应该不会太难。
编辑:如果您决定使用 OpenCV,此链接将是一个有用的资源:@987654321@
【讨论】: