Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Convert OpenCV Mat object to BufferedImage (and the javax.imageio package in general) is for reading/writing images from/to file formats. What you have is an array containing "raw" pixels. It's impossible for ImageIO to determine file format from this byte array. Because of this, it will return null.

Instead, you should create a BufferedImage from the bytes directly. I don't know OpenCV that well, but I'm assuming that the result of Imgproc.cvtColor(rgba, rgba, Imgproc.COLOR_RGB2GRAY, 0) will be an image in grayscale (8 bits/sample, 1 sample/pixel). This is the same format as BufferedImage.TYPE_BYTE_GRAY. If this assumption is correct, you should be able to do:

// Read image as before
Mat rgba = Highgui.imread(filePath);
Imgproc.cvtColor(rgba, rgba,
Imgproc.COLOR_RGB2GRAY, 0);

// Create an empty image in matching format
BufferedImage gray = new
BufferedImage(rgba.width(), rgba.height(),

// Get the BufferedImage's backing array and copy
the pixels directly into it
byte[] data = ((DataBufferByte)
rgba.get(0, 0, data);

Doing it this way, saves you one large byte array allocation and one byte array copy as a bonus. :-)

Categories : Java

Related to : Convert OpenCV Mat object to BufferedImage
Opencv: convert RGB matrix to 1d array
Yes it is referenced over there in the documentation. And why don't you see the snippet below: template<int N> void SetPixel(Mat &img, int x, int y, unsigned char newVal) { *( + (y * img.cols + x) * img.channels() + N) = newVal; } int main() { Mat img = Mat::zeros(1000, 1000, CV_8UC4); SetPixel<0>(img, 120); SetPixel<1>(img, 120); SetPixel&l

Categories : Arrays
OpenCV Mat object serialization in java
no, not so easy. the actual data is held inside the c++ native so, so your serialize() won't reach into that. what you can do: Mat mat = ... byte[] bytes = new byte[*mat.elemSize()]; mat.get(0,0,bytes); // now somehow save mat.type(), mat.rows(), mat.cols() and the bytes, later restore it: Mat m2 = new Mat(rows,cols,type); m2.put(0,0, bytes);

Categories : Java
Convert vector to vector file Opencv
yes, there is a reshape function. vector<Mat> vm; // input Mat flat; // output for (size_t i=0; i<vm.size(); i++) { // you said, those would be 1xM, but let's make sure. flat.push_back( vm[i].reshape(1,1) ); } flat = flat.reshape(1,1); // all in one row, 1xN*M now note, that reshape returns a new Mat header, it does not work inplace (common pitfall here!)

Categories : C++
ExtJs 4 - Convert JS object to xml
make a XML string using Json data and convert the XML string to XML object To convert string to XML go through the following link How to convert string to XML object in JavaScript?

Categories : Javascript
Convert into JSON Object
If you can convert this var JsonString = "{"IdKey":"100008000","IdNumber":"50111112","IdType":"Single","IdTitle":"Singel Id","Name":"Nick"}"; with double quotes into single quotes var JsonString = '{"IdKey":"100008000","IdNumber":"50111112","IdType":"Single","IdTitle":"Singel Id","Name":"Nick"}'; Then JSON parse will work correctly without any parsing error. var JsonObject = JSON.parse(JsonS

Categories : Javascript
Recently Add
How to convert this for loop into an enhanced loop
Do-while, try-catch loop error
JavaFx use String with Double on table column
Equal Spacing In print statements
Android Poor Image Quality When Saving Image From takePicture callback
Why ConcurrentHashMap put() return null but the item is put successfully?
Executing the java program from command prompt
Query multiple Entity attributes with List of values
Android Collections.sort - doesn't work properly
Gdx.files.internal(...) wrapper not working correctly
How to do character math?
Android - How to stack the views added dynamically?
Java: implement a loop with duplicate values in Array
Graphics paint component and loop trouble
Java: Transfer a file from server to client and from client to server
Problems with struts2 interceptor
Beginners Java Blue J returning a String in a method
How to pass Array of complex objects as a IN parameter in a stored procedure using JPA 2
Android: Finding fundamental frequency of audio input
Generic Java Logger output null.null for class and member name
How to use semaphores correctly
Hystrix: HystrixBadRequestException for failed validations
Sending a file over java socket
How can I make a variable work in more that one class? (Java)
Java - What are the layout managers available in awt.* and swing.* packages?
Reading ints from file and storing value to an array
random integers between 2 values
Maven -- is there a command to download the parent poms of all dependency jars?
Null Pointer Exception in Array when adding ActionListener
Hystrix Request Caching by Example
© Copyright 2017 Publishing Limited. All rights reserved.