2

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

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)

我已经研究过,但找不到任何确切的解决方案。要查看问题的根源,我将图像加载为图标。他们都还好。此问题的原因可能是图像稍后加载,因此无法应用过滤器吗?

此外,我还将应用细化算法和其他一些过滤器。你认为如果在处理而不是 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);
4

2 回答 2

4

我找到了解决方案。我没有使用 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();

}

于 2012-06-17T15:17:05.503 回答
0

我只是遇到了“相同”的错误,并深入研究了源代码以找出问题所在。我最终阅读了https://github.com/frohoff/jdk8u-dev-jdk/blob/master/src/share/native/sun/awt/medialib/awt_ImagingLib.c

如您所见,方法 Java_sun_awt_image_ImagingLib_convolveRaster 有十几种返回 0 或 -1 的方式(实际上有 13 种方式)。

每当该方法返回非正数时,这种情况就会以 Ecrin 发布的异常结束,并且没有具体信息是什么出了问题,因为该方法一开始就没有提供任何信息。

你能做的最好的事情就是从一个工作示例开始,就像 Ecrin 提供的那样,希望它也对你有用。然后一步步改。

于 2020-01-06T01:57:22.100 回答