Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java OpenCV-如何直接从Mat生成BuffereImage_Java_Opencv_Bufferedimage - Fatal编程技术网

Java OpenCV-如何直接从Mat生成BuffereImage

Java OpenCV-如何直接从Mat生成BuffereImage,java,opencv,bufferedimage,Java,Opencv,Bufferedimage,我怎样才能从这个垫子上得到BuffereImage。可能吗?如果没有,请向我推荐其他方法。是的,有可能: import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.highgui.VideoCapture; public class Main { public static void main(String[] args) { System.out.println("Welcome t

我怎样才能从这个垫子上得到BuffereImage。可能吗?如果没有,请向我推荐其他方法。

是的,有可能:

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.VideoCapture;

public class Main {

  public static void main(String[] args) {
      System.out.println("Welcome to OpenCV " + Core.VERSION);
      System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
      VideoCapture vc = new VideoCapture(0);
      Mat m = new Mat(5,5, 0);
      vc.retrieve(m);
  }

@gmk是您的
Mat
灰色还是RGB?如果您的
Mat
是GARY,您将从该方法中获得灰色。Mat webcam_image=new Mat();VideoCapture网络摄像头=新的VideoCapture(0);显示您的
网络摄像头图像
,查看它是灰色还是RGB。
public static BufferedImage mat2Img(Mat in)
{
    BufferedImage out;
    byte[] data = new byte[320 * 240 * (int)in.elemSize()];
    int type;
    in.get(0, 0, data);

    if(in.channels() == 1)
        type = BufferedImage.TYPE_BYTE_GRAY;
    else
        type = BufferedImage.TYPE_3BYTE_BGR;

    out = new BufferedImage(320, 240, type);

    out.getRaster().setDataElements(0, 0, 320, 240, data);
    return out;
}