javacpp-presets-opencv,get pixel value of Mat? - opencv

I am using javacpp-presets/opencv which is a java wrapper for opencv. My question is how to get pixel value in Mat object or how to convert Mat object to multi-dimension java array?
I am using tensorflow java api for model inference.It needs float[][][][] java array as input argument.

Solved see this
Mat bgr = new Mat();
UByteIndexer rgbaIdx = bgr.createIndexer();
System.out.print(rgbaIdx.get(0, 0, 0))

Related

How to convert a Mat (OpenCV) to INDArray (DL4J)?

I have a keras network model that takes INDArray but I don't know how to convert a Mat to an INDArray. I know that this is the same question of How to convert Mat (opencv) to INDArray (DL4J)? but it did not help me. Is there some API that performs this task? Thanks
You can use a NativeImageLoader for the conversion.
import org.datavec.image.loader.NativeImageLoader;
(...)
Mat cvImage();
// Fill in your Mat with something
NativeImageLoader nil = new NativeImageLoader();
INDArray image = nil.asMatrix(cvImage).div;
Make sure you have the dependency for datavec in your pom.xml. What error do you get?

Conversion between cv::Mat and arma::mat

I am using OpenCV and also want to add some of cool functions from mlpack, which is using Armadillo matrices.
Is there an easy way to convet between cv::Mat and arms::mat?
Thanks!
OpenCV's Mat has a pointer to its data. Armadillo has a constructor that is capable of reading from external data. It's easy to put them together. Remember that Armadillo stores in column-major order, whereas OpenCV uses row-major. I suppose you'll need to add another step for transformation, before or after.
cv::Mat opencv_mat; //opencv's mat, already transposed.
arma::mat arma_mat( reinterpret_cast<double*>opencv_mat.data, opencv_mat.rows, opencv_mat.cols )
The cv::Mat constructor has a form that accepts pointer to data, and arma::mat has a function for a double* pointer to its data called memptr().
So, if you'd like to convert from arma::mat to cv::Mat, this should work:
cv::Mat opencv_mat( rows, cols, CV_64FC1, arma_mat.memptr() )

How to create a binary image from array with opencv?

I am new to opencv and I have a data array for a binary image, like [1,0,0,1,1,0,0,1...]. My goal is to read it in opencv and manipulate it with opencv functions, so how to create a binary image from raw data array with opencv?
you can easily create a Mat from that data :
unsigned char bits[] = {1,0,0,1,1,0,0,1,1};
Mat m( 3,3, CV_8UC1, bits );
if your bits were an int[] you'd have to use CV_32SC1.

Access multidimensional Mat range OpenCV

I have a 3x3x1000 OpenCV Mat matrix created using
int sz[] = {3,3,1000};
Mat bigCube(3, sz, CV_8U);
I want to do matrix operations on the 1000 separate 3x3 sub-matrices. But I can not find a way to do this.
The most obvious would be in a for-loop using Range, something like;
bigCube(Range(0,3),Range(0,3),Range(i,i+1));
//Do some operations...
But this won't compile. Is there a way to do this?

What's the full name of `IplImage` and `CvMat` in OpenCV?

There is a IplImage and CvMat in OpenCV. What are the full names of them?
IPL in IplImage stands for Intel Processing Library, which is a remnant of when OpenCV was maintained by Intel.
CV in cvMat stands for Computer Vision Matrix, which is a data structure commonly used in graphics.
IplImage is an old structure, which I believe is internally converted into cv::Mat, or just Mat if you're in the cv namespace already. Likewise, cvMat is converted into Mat as well.
http://opencv.willowgarage.com/documentation/cpp/basic_structures.html?highlight=iplimage

Resources