本文整理匯總了Java中org.opencv.core.Mat.elemSize方法的典型用法代碼示例。如果您正苦於以下問題:Java Mat.elemSize方法的具體用法?Java Mat.elemSize怎麽用?Java Mat.elemSize使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.opencv.core.Mat
的用法示例。
在下文中一共展示了Mat.elemSize方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: mat2BI
import org.opencv.core.Mat; //導入方法依賴的package包/類
public static BufferedImage mat2BI(Mat matrix) {
final int cols = matrix.cols();
final int rows = matrix.rows();
final int elemSize = (int) matrix.elemSize();
final byte[] data = new byte[cols * rows * elemSize];
int type;
matrix.get(0, 0, data);
switch (matrix.channels()) {
case 1:
type = BufferedImage.TYPE_BYTE_GRAY;
break;
case 3:
type = BufferedImage.TYPE_3BYTE_BGR;
// bgr to rgb
byte b;
for (int i = 0; i < data.length; i = i + 3) {
b = data[i];
data[i] = data[i + 2];
data[i + 2] = b;
}
break;
default:
return null;
}
final BufferedImage image2 = new BufferedImage(cols, rows, type);
image2.getRaster().setDataElements(0, 0, cols, rows, data);
return image2;
}
示例2: getRaster
import org.opencv.core.Mat; //導入方法依賴的package包/類
public static byte[] getRaster(Mat input) {
final Mat gray = new Mat();
Imgproc.cvtColor(input, gray, Imgproc.COLOR_RGB2GRAY);
final int cols = gray.cols();
final int rows = gray.rows();
final int elemSize = (int) gray.elemSize();
final byte[] data = new byte[cols * rows * elemSize];
gray.get(0, 0, data);
return data;
}
示例3: matToJson
import org.opencv.core.Mat; //導入方法依賴的package包/類
public static String matToJson(Mat mat){
JsonObject obj = new JsonObject();
if(mat.isContinuous()){
int cols = mat.cols();
int rows = mat.rows();
int elemSize = (int) mat.elemSize();
byte[] data = new byte[cols * rows * elemSize];
mat.get(0, 0, data);
obj.addProperty("rows", mat.rows());
obj.addProperty("cols", mat.cols());
obj.addProperty("type", mat.type());
// We cannot set binary data to a json object, so:
// Encoding data byte array to Base64.
String dataString = DatatypeConverter.printBase64Binary(data);
// String dataString = new String(Base64.encode(data, Base64.DEFAULT));
obj.addProperty("data", dataString);
Gson gson = new Gson();
String json = gson.toJson(obj);
return json;
} else {
System.err.println("mat not continuous");
}
return "{}";
}
示例4: matToBufferedImage
import org.opencv.core.Mat; //導入方法依賴的package包/類
public static BufferedImage matToBufferedImage(Mat matrix) {
int cols = matrix.cols();
int rows = matrix.rows();
int elemSize = (int) matrix.elemSize();
byte[] data = new byte[cols * rows * elemSize];
int type;
matrix.get(0, 0, data);
switch (matrix.channels()) {
case 1:
type = BufferedImage.TYPE_BYTE_GRAY;
break;
case 3:
type = BufferedImage.TYPE_3BYTE_BGR;
// bgr to rgb
byte b;
for (int i = 0; i < data.length; i = i + 3) {
b = data[i];
data[i] = data[i + 2];
data[i + 2] = b;
}
break;
default:
return null;
}
BufferedImage bimg = new BufferedImage(cols, rows, type);
bimg.getRaster().setDataElements(0, 0, cols, rows, data);
return bimg;
}
示例5: matToJSON
import org.opencv.core.Mat; //導入方法依賴的package包/類
public static String matToJSON(Mat mat){
JsonObject obj = new JsonObject();
int cols = mat.cols();
int rows = mat.rows();
int elemSize = (int) mat.elemSize();
byte[] data = new byte[cols * rows * elemSize];
mat.get(0, 0, data);
obj.addProperty("rows", mat.rows());
obj.addProperty("cols", mat.cols());
obj.addProperty("type", mat.type());
String dataString = new String(Base64.encode(data, Base64.DEFAULT));
obj.addProperty("data", dataString);
Gson gson = new Gson();
return gson.toJson(obj);
}
示例6: matToBufferedImage
import org.opencv.core.Mat; //導入方法依賴的package包/類
public static BufferedImage matToBufferedImage(Mat matrix) {
if (matrix.channels() == 1) {
int cols = matrix.cols();
int rows = matrix.rows();
int elemSize = (int) matrix.elemSize();
byte[] data = new byte[cols * rows * elemSize];
int type;
matrix.get(0, 0, data);
switch (matrix.channels()) {
case 1:
type = BufferedImage.TYPE_BYTE_GRAY;
break;
case 3:
type = BufferedImage.TYPE_3BYTE_BGR;
// bgr to rgb
byte b;
for (int i = 0; i < data.length; i = i + 3) {
b = data[i];
data[i] = data[i + 2];
data[i + 2] = b;
}
break;
default:
return null;
}
BufferedImage image2 = new BufferedImage(cols, rows, type);
image2.getRaster().setDataElements(0, 0, cols, rows, data);
return image2;
}
if (matrix.channels() == 3) {
int width = matrix.width(), height = matrix.height(), channels = matrix.channels();
byte[] sourcePixels = new byte[width * height * channels];
matrix.get(0, 0, sourcePixels);
// create new image and get reference to backing data
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
System.arraycopy(sourcePixels, 0, targetPixels, 0, sourcePixels.length);
return image;
}
return null;
}