我想在应用canny方法后显示图像。图像是一个字节数组,方法也返回一个字节数组,这里是代码:
public byte[] doCanny(byte[] image) {
byte[] buff;
try {
Mat mat = Imgcodecs.imdecode(new MatOfByte(image), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
Mat gray = new Mat();
Mat draw = new Mat();
Mat wide = new Mat();
Imgproc.cvtColor(mat, gray, Imgproc.COLOR_BGR2GRAY);
Imgproc.Canny(gray, wide, 50, 150, 3, false);
wide.convertTo(draw, CvType.CV_8U);
int size = (int) draw.total() * draw.channels();
buff = new byte[size];
draw.put(50, 150, buff);
System.out.println("Should return image");
return buff;
// if (Imgcodecs.imwrite(filename, draw)) {
// System.out.println("edge is detected .......");
// }
} catch (Exception e) {
e.printStackTrace();
}
return image;
}
when I run this method it dose not return any image.
答案 0 :(得分:0)
OpenCV Quick Guide中有一个例子解释了如何将MatOfByte对象转换为字节数组。尝试这样做:
public byte[] doCanny(byte[] image) {
try {
Mat color = Imgcodecs.imdecode(new MatOfByte(image), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
Mat gray = new Mat();
Mat draw = new Mat();
Mat wide = new Mat();
Imgproc.cvtColor(color, gray, Imgproc.COLOR_BGR2GRAY);
Imgproc.Canny(gray, wide, 50, 150, 3, false);
wide.convertTo(draw, CvType.CV_8U);
// Encoding the image
MatOfByte matOfByte = new MatOfByte();
Imgcodecs.imencode(".jpg", draw, matOfByte);
return matOfByte.toArray();
} catch (Exception e) {
e.printStackTrace();
}
return image;
}