调整高 BufferedImage 的大小时如何保留/偏爱某些像素值?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过在渲染提示中打开抗锯齿功能?这可能有帮助。
如果没有,您可以很容易地编写自己的缩放函数。这实际上非常简单 - 您只需要:
正常抗锯齿会使用源像素值的平均值之类的东西作为公式,它听起来你想要一些优先考虑特定值的东西(例如,如果背景像素的颜色值较低,则采用最大像素值)
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:
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)