如何使用颜色图案剪切图像?

发布于 2024-08-31 17:57:32 字数 240 浏览 3 评论 0原文

我正在开发一个小程序,可以按颜色剪切图像。

使用此示例图像最容易解释:

black with a Purple splotch of color

我想创建一个新图像只是紫色的形式,没有黑框。

有人有什么想法吗?我正在使用 Java 2D,所以我认为我需要使用第一张图像的紫色区域创建一个对象“形状”。

I am developing a small program which cuts images by the color.

That's will be easiest to explain using this example image:

black with a purple splotch of color

And I want to create a new image just with the purple form, without the black frame.

Does anyone have any ideas? I am using Java 2D so I think that I need to create an Object "Shape" with the purple area of the first image.

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

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

发布评论

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

评论(2

如何视而不见 2024-09-07 17:57:32

如果图像确实像您显示的那样,您可以:

  • 将图像加载到 BufferedImage(使用 ImageIO.read())
  • 创建一个相同大小的新 BufferedImage,确保它具有 alpha 层(例如将其类型设置为 BufferedImage .TYPE_4BYTE_ABGR)
  • “手动”依次遍历加载的 BufferedImage 中的每个像素,使用 getRGB() 获取像素颜色并检查它是否为黑色,
  • 如果颜色为黑色,则在新图像中将相应像素设置为透明,否则设置为第一个图像的原始颜色(请参阅 setRGB() 方法)
  • 保存新图像(使用 ImageIO.write())

有更奇特的方法,但这种简单的方法很好且易于理解,并且适用于您显示的类型的图像。

If the image is literally like the one you show, you could:

  • load the image into a BufferedImage (with ImageIO.read())
  • create a new BufferedImage of the same size, ensuring it has an alpha layer (e.g. set its type to BufferedImage.TYPE_4BYTE_ABGR)
  • "manually" go through each pixel in turn in the loaded BufferedImage, getting the pixel colour with getRGB() and checking if it's black
  • if the colour is black, set the corresponding pixel to transparent in the new image, else to the original colour from the first image (see setRGB() method)
  • save the new image (with ImageIO.write())

There are fancier ways, but this simple method is nice and understandable and will work fine for images of the type you showed.

善良天后 2024-09-07 17:57:32

您需要使用一些洪水填充算法来找到紫色区域的边界:

维基百科上有一个页面,其中包含出色的伪代码和动画。

http://en.wikipedia.org/wiki/Flood_fill

You need to use some flood-fill algorithm that finds the boundries of the purple area:

Wikipedia has a page on it with excellent pseudo code and animations.

http://en.wikipedia.org/wiki/Flood_fill

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