iPhone应用程序绿屏更换

发布于 2024-11-07 00:13:04 字数 100 浏览 2 评论 0原文

问:我想使用 iPhone 相机拍摄一张照片,然后用另一张照片替换该照片中的绿屏。

深入研究这个问题的最佳方法是什么?我在网上找不到很多资源。

提前致谢!

Q: I'm looking to use the iPhone camera to take a photo and then replace the green screen in that photo with another photo.

What's the best way to dive into this? I couldn't find many resources online.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

未央 2024-11-14 00:13:04

从概念上讲,您需要做的就是循环遍历手机拍摄的照片的像素数据,对于不在特定绿色范围内的每个像素,将像素复制到背景图像上的相同位置。

这是我从 keremic 的答案修改为 另一个 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

Conceptually, all that you need to do is loop through the pixel data of the photo taken by the phone, and for each pixel that is not within a certain range of green, copy the pixel into the same location on your background image.

Here is an example I modified from keremic's answer to another stackoverflow question.
NOTE: This is untested and just intended to give you an idea of a technique that will work

//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
各空 2024-11-14 00:13:04

看看为 iPhone 编译 OpenCV - 这不是一件容易的事,但它使您可以访问整个库,其中包含非常出色的图像处理工具。

我正在使用 openCV 来开发我目前正在开发的应用程序(与您的应用程序并不完全不同) - 对于您想要做的事情,openCV 将是一个很好的解决方案,尽管它需要一些学习等。你已经让 OpenCV 工作了,去除绿色的实际任务应该不会太难。

编辑:如果您决定使用 OpenCV,此链接将是有用的资源:为 iOS 编译 OpenCV

Have a look at compiling OpenCV for iPhone - not an easy task, but it gives you access to a whole library of really great image processing tools.

I'm using openCV for an app I'm developing at the moment (not all that dissimilar to yours) - for what you're trying to do, openCV would be a great solution, although it requires a bit of learning etc. Once you've got OpenCV working, the actual task of removing green shouldn't be too hard.

Edit: This link will be a helpful resource if you do decide to use OpenCV: Compiling OpenCV for iOS

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文