以编程方式使图像的背景透明或白色 [英] Make an image's background transparent or white programmatically

查看:203
本文介绍了以编程方式使图像的背景透明或白色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果正在制作一个应用程序,其中一个人想要使图像的背景透明或白色,有没有办法做到这一点?

If one is creating an application in which one would like to make the "background" of an image transparent or white, is there a way to do this?

例如:

在图片中, http://upload.wikimedia.org/wikipedia/commons/b/b9/Bronze_Statuette_of_a_Veiled_and_Masked_Dancer_1.jpg ,我想以编程方式转换图像,这样只有雕像仍然存在,背景(即图像的其余部分)全是白色或透明。

In the image, http://upload.wikimedia.org/wikipedia/commons/b/b9/Bronze_Statuette_of_a_Veiled_and_Masked_Dancer_1.jpg , I would like to transform the image programmatically, such that only the statue remains, and the background (i.e. the rest of the image) is all white, or transparent.

此外,用户可能会指出图像的一部分应该保留,其余应该是白色或透明的。

Also, the user might point out to the part of the image that should "remain", and the rest should be all white or transparent.

如何做到这一点?此外,如果有一个合适的java库或一段代码,它会有所帮助。

How can this be done? Also, if there is a suitable java library or piece of code, it would be helpful.

问候

推荐答案

对于该特定图像,有几种方法可以解决部分问题。也许如果你结合多种方法提供用户交互选择,你可以在相当短的时间内开发出一个好的软件。我会用它!

For that particular image there are several methods that could solve part of the problem. Maybe if you combine several methods and offer the user interactive choices you could develop a good software in a reasonably short period of time. I'd use it!

你和Misha已经讨论了前两项:

You and Misha already discussed the first two items:


  1. 使用Canny或其他方法进行边缘检测。我建议您应该使用原始边缘强度图像,而不是对边缘强度进行阈值处理以生成二值化图像。请注意,在这种情况下,图形处于清晰焦点,并且大部分背景都没有聚焦。虽然这不是一般解决方案,但对于此特定图像,您可以过滤出(a)属于线条的边缘(使用Hough或RANSAC),但(b)具有低于阈值陡度的渐变。

  2. 洪水填充。 Misha已经提供了链接。填充静态值的洪水不应太难实现(例如,相对于所点击的像素填充值+/- N的所有相邻像素)。实现动态泛光填充可以解决由于光照和3D曲率引起的梯度问题。 。 。哎哟!

  3. 平均移位聚类。这可能甚至可以作为帮助将相同HSV值的像素聚集在一起的第一步。然而,只要注意图像,前景图形的色调和大部分背景的色调是相似的。 OpenCV实现了均值漂移。有一个相关CAMSHIFT算法的视频正在运行: http://www.youtube.com/watch? v = iBOlbs8i7Og

  4. 边缘跟随强边缘。如果用户单击靠近边缘,则标识最近的强边,然后使用轮廓跟踪(或轮廓跟踪)算法。基本轮廓跟踪算法适用于二值图像;您可以调整算法以尝试遵循RGB空间中的强大边缘。 Tricky!

  5. 由于您正在为用户编写软件,因此请将一些时间用于软件可用性,而不是仅仅尝试解决一般的图像处理问题。

  6. 检查背景曲线是否遮挡。确定项目是否是背景的一部分的另一种方法是确定它是否被前景对象遮挡(隐藏)。如果你发现两个线段是共线的并且具有平缓的渐变(即它们没有聚焦),那么它们可能是两个线段而不是一个,因为它们被前景对象遮挡。

  7. 如果你有可能使用相机而不是现有的图像,你可以模仿光场或全光相机(例如Lytro https://www.lytro.com/camera )通过在不同的焦点设置下拍摄多个连续图像。这可以帮助您根据渐变的变化识别不同深度的图形。如果雕像相对靠近相机,当相机对着无限远时,它将会失焦。

  1. Edge detection using Canny or other means. I would suggest that you should work on a "raw" edge strength image rather than thresholding the edge strengths to generate a binarized image. Note that in this case the figure is in sharp focus, and much of the background is out of focus. Although it's not a general solution, for this particular image you could filter out edges that (a) belong to lines (using Hough or RANSAC) but that (b) have gradients that fall below a threshold steepness.
  2. Flood fill. Misha already provided the link. Flood filling a "static" value shouldn't be too hard to implement (e.g. fill all neighboring pixels of value +/- N relative to the clicked pixel). Implementing a dynamic flood fill that could account for gradients due to lighting and 3D curvature into account is, well . . . ouch!
  3. Mean shift clustering. This might work even as a first step to help clump together pixels of the same HSV values. Just eyeballing the image, though, the hue of the foreground figure and the hue of most of the background is similar. OpenCV has an implementation of mean shift. There's a video of the related CAMSHIFT algorithm at work: http://www.youtube.com/watch?v=iBOlbs8i7Og
  4. Edge following for strong edges. If the user clicks close to an edge, you identify the closest strong edge and then use a contour-following (or "contour tracing") algorithm. Basic contour-following algorithms work on binary images; you could adapt your algorithm to try to follow a strong edge in RGB space. Tricky!
  5. Since you're writing software for a user, devote some of your time to software usability rather than just trying to solve the general image processing problem.
  6. Check for occlusion of background curves. Another way to determine if an item is part of the background is to determine if it is occluded (hidden) by a foreground object. If you find two line segments that are colinear and have gentle gradients (i.e. they're out of focus), then they may be two line segments rather than one because they are occluded by a foreground object.
  7. If by any chance you can work with a camera rather than existing images, you can mimic a "light field" or plenoptic camera (such as the Lytro https://www.lytro.com/camera) by taking multiple successive images at different focus settings. This can help you identify figures at different depths based on changes in gradients. If the statue is relatively close to the camera, it will go out of focus as the camera focuses towards infinity.

提高可用性


  1. 假设您可以将图像分割成相当不同的块,提示用户单击属于同一个感兴趣对象的块。每个块可以具有其自己的一组用于边缘强度,颜色接受范围等的调整参数。具有块特定参数可以帮助使软件可用,即使存在阴影,变化的光照等,这给分割带来问题。这样的事情可以通过组合选择在GIMP和Photoshop中完成,但它的可用性不如它。

  1. Assuming you can segment the image into chunks that are reasonably distinct, prompt the user to click on chunks that belong to the same object of interest. Each chunk could have its own set of tweaking parameters for edge strength, acceptance range for color, etc. Having chunk-specific parameters can help make the software usable even if there are shadows, varying lighting, etc., that present problems for segmentation. Something like this can be done in GIMP and Photoshop by combining selections, but it's less usable than it could be.

对于已经识别的块,实现捕捉到边缘功能,帮助用户将发现的边缘曲线移动到真正的边缘曲线上。如果用户抓取一个块状轮廓并将其拖向一个方向,则轮廓可以捕捉到该方向的下一个强边缘。

For the chunks that have been identified, implement a "snap to edge" feature that helps the user moves the discovered edge curve onto the true edge curve. If the user grabs a chunk contour and drags it in one direction, the contour could snap to the next strong edge in that direction.

提供批处理选项。如果用户具有在相同条件下拍摄的一系列照片,则用户选择的第一图像的块可以帮助引导软件设置连续图像的参数。这不是为了解决一般的分割问题,但可能会为用户节省一些图像组的时间和精力。

Offer a batch processing option. If a user has a series of photos taken under the same conditions, then the user-selected chunks for the first image could help guide the software in setting parameters for successive images. This isn't intended to solve the general segmentation problem, but might save the user a bit of time and effort for certain groups of images.

这是一个有趣的问题。祝你好运!

It's a fun problem. Good luck!

这篇关于以编程方式使图像的背景透明或白色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆