调整高 BufferedImage 的大小时如何保留/偏爱某些像素值?

发布于 2024-09-11 03:11:31 字数 819 浏览 2 评论 0原文

我有一个 BufferedImage,它相当高(大约 1100 像素高度),但不是特别宽(可能是 200-300 像素)。该图像由数据图组成,但大多数像素表示并不是很有趣的背景颜色/值。我的问题是,当图像的大小从 1100 像素的高度调整到 200-300 像素之类的高度时,许多实际包含有趣数据的像素会被背景颜色像素破坏。有什么方法可以告诉 Java 2D 重新缩放算法在重新缩放时我更喜欢某些值的像素(或者更好的是,我想降低背景颜色的优先级)?我使用以下代码片段来重新调整图像的大小:

BufferedImage resized = new BufferedImage(width, height, imageType);
Graphics2D graphics2D = resized.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); //Has worked best in my case
graphics2D.drawImage(source, 0, 0, width, height, null);
graphics2D.dispose();

我确信在 Java 中存在更有效的调整图像大小的方法,但这并不有趣(至少现在不是)。

一件无聊的事情是,我之前使用过一个库(将不再使用),它实际上设法保留了代表像素的许多有价值的数据。

请注意,我手头有原始像素数据(2D int 数组),如果这有任何帮助的话。该应用程序将处理许多不同尺寸的图像,因此我不确定高度是否一定是 1100 像素。

谢谢。

I have a BufferedImage which is quite tall (something like 1100 pixels height) and not especially wide (maybe 200-300 pixels). The image consists of a data plot, but most of the pixels represents a background color/value that's not really interesting. My problem is that when the image is resized from a height of 1100 px to something like 200-300 px, many of the pixels that actually contained interesting data is trashed by background color pixels. Is there any way to tell the Java 2D rescaling algorithms that I prefer pixels of certain values (or better, that I want to down-prioritize the background color) while rescaling? I use the following code snippet to rescale the image:

BufferedImage resized = new BufferedImage(width, height, imageType);
Graphics2D graphics2D = resized.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); //Has worked best in my case
graphics2D.drawImage(source, 0, 0, width, height, null);
graphics2D.dispose();

I'm sure that there's more effective ways of resizing images in Java, but that's not interesting (at least not for now).

A kind of boring thing is that I used a library before (which will not be used anymore) that actually managed to keep much of the valuable data representing pixels.

Note that I have the raw pixel data at hand (a 2D int array) if that is of any help. The application will handle many images of different sizes, so I don't know for sure that the height necessarily is 1100 pixels.

Thanks.

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

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

发布评论

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

评论(1

甜味拾荒者 2024-09-18 03:11:31

您是否尝试过在渲染提示中打开抗锯齿功能?这可能有帮助。

如果没有,您可以很容易地编写自己的缩放函数。这实际上非常简单 - 您只需要:

  • 循环遍历目标 BufferedImage 中的所有点
  • 对于原始像素数据中相关点的每个循环(这将是由缩放因子确定的矩形区域)
  • 计算目标根据原始像素值使用您选择的公式进行颜色
  • 使用 BufferedImage.setRGB(...) 设置目标像素的颜色

正常抗锯齿会使用源像素值的平均值之类的东西作为公式,它听起来你想要一些优先考虑特定值的东西(例如,如果背景像素的颜色值较低,则采用最大像素值)

Have you tried turning on anti-aliasing in the rendering hints? That might help.

If not, you could pretty easily write your own own scaling function. This is actually pretty simple - you just need to:

  • Loop over all the points in your target BufferedImage
  • For each of these loop over the relevant points in your raw pixel data (this will be a rectangular zone determined by your scaling factor)
  • Calculate the target colour using a formula of your choice based on the raw pixel values
  • Use BufferedImage.setRGB(...) to set the colour of the target pixel

Normal anti-aliasing would use something like an average of the source pixel values as a formula, it sounds like you want something that gives priority to particular values (e.g. take the maximum pixel value if your background pixels have low colour values)

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