How to create a binary image from array with opencv? - 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.

Related

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

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))

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() )

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?

Multiple single channel matrix converted to single multi channel matrix

i am working in opencv c++ api with matrices
I have 4 single channel Mat that i will like to merge into one 4 channel matrix. It is basically the rgba channels i have in 4 matrices and want to combine into one rgba image/matrix. Anyone who knows how to do that?
You can use cv::merge to do what you want. One possible usage:
cv::Mat r,g,b,a;
//Fill r,g,b,a with data
cv::Mat result;
std::vector<cv::Mat> channels;
channels.push_back(r);
channels.push_back(g);
channels.push_back(b);
channels.push_back(a);
cv::merge(channels, result);

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