# OpenCV mit Java - Bilder in Graustufenbilder umwandeln



## pa_kr (8. Jul 2014)

Hallo Mitglieder des Java-Forums,

in der Programmierung von *OpenCV mit Java* bin ich noch sehr ungeübt. Als Einstieg wollte ich versuchen ein Programm zu schreiben, dass ein Bild lädt, dieses nach dem Betätigen eines Buttons in ein Graustufen-Bild umwandelt und wieder anzeigt.

Dabei kommt es allerdings immer zum Programmabbruch bei vermutlich folgender Code-Zeile:

```
Mat image = Highgui.imread("picture.JPG",0);
```

mit folgender Fehlermeldung:


Spoiler: Fehlermeldung



Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: org.opencv.highgui.Highgui.imread_0(Ljava/lang/String;I)J
	at org.opencv.highgui.Highgui.imread_0(Native Method)
	at org.opencv.highgui.Highgui.imread(Highgui.java:309)
	at graubild.window$2.actionPerformed(window.java:66)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
	at java.awt.Component.processMouseEvent(Component.java:6505)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
	at java.awt.Component.processEvent(Component.java:6270)
	at java.awt.Container.processEvent(Container.java:2229)
	at java.awt.Component.dispatchEventImpl(Component.java:4861)
	at java.awt.Container.dispatchEventImpl(Container.java:2287)
	at java.awt.Component.dispatchEvent(Component.java:4687)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
	at java.awt.Container.dispatchEventImpl(Container.java:2273)
	at java.awt.Window.dispatchEventImpl(Window.java:2719)
	at java.awt.Component.dispatchEvent(Component.java:4687)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
	at java.awt.EventQueue.access$200(EventQueue.java:103)
	at java.awt.EventQueue$3.run(EventQueue.java:694)
	at java.awt.EventQueue$3.run(EventQueue.java:692)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
	at java.awt.EventQueue$4.run(EventQueue.java:708)
	at java.awt.EventQueue$4.run(EventQueue.java:706)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)



Ich habe auch schon probiert den kompletten Pfad des Bild anzugeben, leider ohne Erfolg.

Zum Schluss schicke ich noch den kompletten Quelltext:


Spoiler: Quelltext





```
package graubild;

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;


public class window extends JFrame {
	
	public ImageIcon img;
	public JPanel panel;

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {	
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					window frame = new window();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
	}

	/**
	 * Create the frame.
	 */
	public window() {
		setTitle("Bild in Graustufen darstellen");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 700, 500);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton btnNewButton = new JButton("Bild umwandeln");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String filename = "C:/Users/Patrick/Pictures/picture.JPG";
				Mat image = Highgui.imread("picture.JPG",0);
				Highgui.imwrite(filename, image);
				try {
					img = new ImageIcon(ImageIO.read(new File(filename)));
					panel.add(new JLabel(img));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				

			}
		});
		btnNewButton.setBounds(10, 11, 664, 23);
		contentPane.add(btnNewButton);
		
		panel = new JPanel();
		panel.setBounds(10, 45, 664, 405);
		contentPane.add(panel);
		
		File imageFile = new File("C:/Users/Patrick/Pictures/picture.JPG");	
		try {
			BufferedImage image = ImageIO.read(imageFile);
			panel.add(new JLabel(new ImageIcon(image)));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	}
}
```




Ich hoffe ihr könnt mir helfen und bedanke mich dementsprechend schon einmal im Vorraus. 

Patrick


----------



## X5-599 (9. Jul 2014)

Versuch es mal hiermit bevor du dein Programm anfängt:


```
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
```

Das steht zumindest auf deren Tutorialseite so. Hier, ziemlich am Ende der Seite.


----------



## pa_kr (17. Jul 2014)

Danke, es funktioniert. :toll:


----------

