OpenCV 图像从 mat 到 BufferedImage 的转换
OpenCV image conversion from mat to BufferedImage
我收到错误
Cannot make a static reference to the non-static method
图像以mat类型加载,然后转换为BufferedImage显示。
中间将使用一些 OpenCV 函数。由于错误 mag,主转换器不能 运行。错误是什么?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.lang.Math;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
public class Tutorial1 {
public static void main(String[] args){
converter();
}
public void converter(){
Mat matImage= Highgui.imread(getClass().getResource("/lena.png").getPath());
BufferedImage bufImage = Mat2BufferedImage(matImage);
displayImage(bufImage);
}
public BufferedImage Mat2BufferedImage(Mat m){
//source: http://answers.opencv.org/question/10344/opencv-java-load-image-to-gui/
//Fastest code
//The output can be assigned either to a BufferedImage or to an Image
int type = BufferedImage.TYPE_BYTE_GRAY;
if ( m.channels() > 1 ) {
type = BufferedImage.TYPE_3BYTE_BGR;
}
int bufferSize = m.channels()*m.cols()*m.rows();
byte [] b = new byte[bufferSize];
m.get(0,0,b); // get all the pixels
BufferedImage image = new BufferedImage(m.cols(),m.rows(), type);
final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
System.arraycopy(b, 0, targetPixels, 0, b.length);
return image;
}
public void displayImage(Image img2)
{
//BufferedImage img=ImageIO.read(new File("/HelloOpenCV/lena.png"));
ImageIcon icon=new ImageIcon(img2);
JFrame frame=new JFrame();
frame.setLayout(new FlowLayout());
frame.setSize(img2.getWidth(null)+50, img2.getHeight(null)+50);
JLabel lbl=new JLabel();
lbl.setIcon(icon);
frame.add(lbl);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
该错误意味着您正在对未声明为静态的方法进行静态引用。因此,例如考虑以下代码:
class Example{
public void doSomething(){
...
}
public static void main(String[] args){
doSomething();
}
}
在这里您看到从 main 方法内部对非静态方法进行了静态引用 doSomething()
这将产生与您现在遇到的相同的错误。
现在解决方案:
要解决此问题,您可以将 static 关键字添加到相关方法中。因此,对于我的示例,这意味着我将更改我的方法:
public void doSomething(){
}
收件人:
public static void doSomething(){
}
另一种选择是使用该方法创建 class 的新实例,然后调用它。这将像这样完成(使用我的示例)。
class Example{
public void doSomething(){
...
}
public static void main(String[] args){
Example example = new Example();
example.doSomething();
}
}
这两种解决方案都可以解决问题,但由程序员决定哪种方式更适合这种情况。
所以你的问题的解决方案,你的 JDK 应该通过用红色下划线来告诉你你正在尝试以错误的方式使用哪种方法。然后您只需应用两种解决方案中的一种。
希望对您有所帮助:)
编辑#1:
在您的情况下,您是从主方法调用转换器,但转换器不是静态的,因此会引发错误。
编辑#2:
因此,为了解决您的问题,您可以将主要方法更改为:
public static void main(String[] args){
new Tutorial1().converter();
}
我收到错误
Cannot make a static reference to the non-static method
图像以mat类型加载,然后转换为BufferedImage显示。 中间将使用一些 OpenCV 函数。由于错误 mag,主转换器不能 运行。错误是什么?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.lang.Math;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
public class Tutorial1 {
public static void main(String[] args){
converter();
}
public void converter(){
Mat matImage= Highgui.imread(getClass().getResource("/lena.png").getPath());
BufferedImage bufImage = Mat2BufferedImage(matImage);
displayImage(bufImage);
}
public BufferedImage Mat2BufferedImage(Mat m){
//source: http://answers.opencv.org/question/10344/opencv-java-load-image-to-gui/
//Fastest code
//The output can be assigned either to a BufferedImage or to an Image
int type = BufferedImage.TYPE_BYTE_GRAY;
if ( m.channels() > 1 ) {
type = BufferedImage.TYPE_3BYTE_BGR;
}
int bufferSize = m.channels()*m.cols()*m.rows();
byte [] b = new byte[bufferSize];
m.get(0,0,b); // get all the pixels
BufferedImage image = new BufferedImage(m.cols(),m.rows(), type);
final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
System.arraycopy(b, 0, targetPixels, 0, b.length);
return image;
}
public void displayImage(Image img2)
{
//BufferedImage img=ImageIO.read(new File("/HelloOpenCV/lena.png"));
ImageIcon icon=new ImageIcon(img2);
JFrame frame=new JFrame();
frame.setLayout(new FlowLayout());
frame.setSize(img2.getWidth(null)+50, img2.getHeight(null)+50);
JLabel lbl=new JLabel();
lbl.setIcon(icon);
frame.add(lbl);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
该错误意味着您正在对未声明为静态的方法进行静态引用。因此,例如考虑以下代码:
class Example{
public void doSomething(){
...
}
public static void main(String[] args){
doSomething();
}
}
在这里您看到从 main 方法内部对非静态方法进行了静态引用 doSomething()
这将产生与您现在遇到的相同的错误。
现在解决方案: 要解决此问题,您可以将 static 关键字添加到相关方法中。因此,对于我的示例,这意味着我将更改我的方法:
public void doSomething(){
}
收件人:
public static void doSomething(){
}
另一种选择是使用该方法创建 class 的新实例,然后调用它。这将像这样完成(使用我的示例)。
class Example{
public void doSomething(){
...
}
public static void main(String[] args){
Example example = new Example();
example.doSomething();
}
}
这两种解决方案都可以解决问题,但由程序员决定哪种方式更适合这种情况。
所以你的问题的解决方案,你的 JDK 应该通过用红色下划线来告诉你你正在尝试以错误的方式使用哪种方法。然后您只需应用两种解决方案中的一种。
希望对您有所帮助:)
编辑#1:
在您的情况下,您是从主方法调用转换器,但转换器不是静态的,因此会引发错误。
编辑#2:
因此,为了解决您的问题,您可以将主要方法更改为:
public static void main(String[] args){
new Tutorial1().converter();
}