将JPEG转换为Mat(8位和3个通道)---缓冲区溢出异常

时间:2017-07-20 17:02:45

标签: java opencv processing buffer-overflow mat

我正在尝试在Processing中使用OpenCV的过滤器功能,但在此之前,我必须将我的图像转换为8位和3个通道的垫子。但每当我运行它时,我都会遇到“缓冲区溢出异常”,我无法弄清楚为什么会出现这个错误以及如何让转换工作。

import gab.opencv.*;
import java.nio.*;
import org.opencv.imgproc.Imgproc;
import org.opencv.core.Mat;
import org.opencv.core.CvType;
import org.opencv.core.Core; 
import java.awt.image.BufferedImage; 
import java.awt.image.DataBufferInt; 

OpenCV opencv;
Imgproc imgproc;

PImage src, out;
PImage before, snap;
Mat one, two;
double a = 35.0;
double b = 20.0;

void setup() {
   src = loadImage("cat.jpg");
   size( 429, 360);

  System.loadLibrary(Core.NATIVE_LIBRARY_NAME);  
  one = new Mat( width, height, CvType.CV_8UC3);
  two = new Mat( width, height, CvType.CV_8UC3);

  one = toMat(src);
  imgproc.pyrMeanShiftFiltering( one, two, a, b);
  out = toPImage(two);
}

void draw() {
  image(out, 0, 0, width, height);
}

Mat toMat(PImage image) {  
  int w = image.width;  
  int h = image.height;  
  Mat mat = new Mat(h, w, CvType.CV_8UC3);  
  byte[] data8 = new byte[w*h*3];  
  int[] data32 = new int[w*h];  
  arrayCopy(image.pixels, data32);  
  ByteBuffer bBuf = ByteBuffer.allocate(w*h*3);  
  IntBuffer iBuf = bBuf.asIntBuffer();  
  iBuf.put(data32); // ERROR -- BufferOverflowException 
  bBuf.get(data8);  
  mat.put(0, 0, data8);  
  return mat;
}  

PImage toPImage(Mat mat) { 
  int w = mat.width(); 
  int h = mat.height(); 
  PImage image = createImage(w, h, RGB); 
  byte[] data8 = new byte[w*h*3]; 
  int[] data32 = new int[w*h]; 
  mat.get(0, 0, data8); 
  ByteBuffer.wrap(data8).asIntBuffer().get(data32); 
  arrayCopy(data32, image.pixels); 
 }

0 个答案:

没有答案