gpt4 book ai didi

java图像卷积

转载 作者:行者123 更新时间:2023-11-30 07:25:12 24 4
gpt4 key购买 nike

我正在尝试对图像应用平滑过滤器。但是我得到了这个错误:

java.awt.image.ImagingOpException: Unable to convolve src image
at java.awt.image.ConvolveOp.filter(ConvolveOp.java:180)
at ocr.Resolution.smoothing(Resolution.java:102)
at ocr.Interface$ButtonListener.actionPerformed(Interface.java:332)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)

我已经研究过但找不到任何确切的解决方案。要查看问题的根源,我将图像加载为图标。他们都还好。这个问题的原因可能是图片加载较晚,所以无法应用滤镜?

此外,我还将应用细化算法和其他一些过滤器。你认为如果用 Processing 代替 Java 更好吗?感谢您的帮助。

        filter = new float[] { 1.0f/121.0f,  2.0f/121.0f,   3.0f/121.0f,   2.0f/121.0f,   1.0f/121.0f,
2.0f/121.0f, 7.0f/121.0f, 11.0f/121.0f, 7.0f/121.0f, 2.0f/121.0f,
3.0f/121.0f, 11.0f/121.0f, 17.0f/121.0f, 11.0f/121.0f, 3.0f/121.0f,
2.0f/121.0f, 7.0f/121.0f, 11.0f/121.0f, 7.0f/121.0f, 2.0f/121.0f,
1.0f/121.0f, 2.0f/121.0f, 3.0f/121.0f, 2.0f/121.0f, 1.0f/121.0f};
kernelWidth = 5;
kernelHeight = 5;
BufferedImageOp bufOp ;
BufferedImage bufImg;
Image img;

img = Toolkit.getDefaultToolkit().getImage(Interface.picPath); //load image
ImageSize size = new ImageSize(img);// instance to get image dimensions
bufImg = new BufferedImage (size.getwidth(),size.getheight(),BufferedImage.TYPE_INT_RGB);
try {
bufImg = ImageIO.read(new File(Interface.picPath) );

} catch (IOException ex) {
Logger.getLogger(Resolution.class.getName()).log(Level.SEVERE, null, ex);
}

kernel = new Kernel( kernelWidth, kernelHeight, filter);
bufOp = new ConvolveOp(kernel);
bufImg = bufOp.filter(bufImg, null);

最佳答案

我找到了解决办法。我没有使用 URl 创建 BufferedImage(bufImg),而是将图像本身 (img) 转换为 BufferedImage,它现在运行了。

public void smoothing(){
filter = new float[] { 1.0f/121.0f, 2.0f/121.0f, 3.0f/121.0f, 2.0f/121.0f, 1.0f/121.0f,
2.0f/121.0f, 7.0f/121.0f, 11.0f/121.0f, 7.0f/121.0f, 2.0f/121.0f,
3.0f/121.0f, 11.0f/121.0f, 17.0f/121.0f, 11.0f/121.0f, 3.0f/121.0f,
2.0f/121.0f, 7.0f/121.0f, 11.0f/121.0f, 7.0f/121.0f, 2.0f/121.0f,
1.0f/121.0f, 2.0f/121.0f, 3.0f/121.0f, 2.0f/121.0f, 1.0f/121.0f};
kernelWidth = 5;
kernelHeight = 5;
kernel = new Kernel( kernelWidth, kernelHeight, filter);
op = new ConvolveOp(kernel);

img = Toolkit.getDefaultToolkit().getImage(Interface.picPath);
imageToBufferedImage(img);
bufImg = op.filter(bufImg, null);

icon = new ImageIcon(img.getScaledInstance(175, 175, Image.SCALE_DEFAULT));
icon2 = new ImageIcon(img.getScaledInstance(300, 300, Image.SCALE_DEFAULT));
Interface.label3.setIcon(icon);
Interface.label8.setIcon(icon2);

}

public void imageToBufferedImage(Image im) {
ImageSize size = new ImageSize(im);
bufImg = new BufferedImage (size.getwidth(), size.getheight(),BufferedImage.TYPE_INT_RGB);
Graphics graph = bufImg.getGraphics();
graph.drawImage(im, 0, 0, null);
graph.dispose();

关于java图像卷积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11071735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com