Header and pointer of the Mat object in Open CV? - opencv

What does the Header and pointer term means in Mat, please elaborate me on each one through some example.

After some googling I found answer to your question in this article:
Mat is basically a class having two data parts: the matrix header
(containing information such as the size of the matrix, the method
used for storing, at which address is the matrix stored and so on) and
a pointer to the matrix containing the pixel values (may take any
dimensionality depending on the method chosen for storing) .

Related

What do the numbers returned by OpenCV's VideoCapture::get(CV_CAP_PROP_FORMAT) mean?

The OpenCV function VideoCapture::get returns "the specified VideoCapture property". We can ask for the frame's format using the property CV_CAP_PROP_FORMAT.
The frames are Mat objects and from the docs (here) Mat "represents an n-dimensional dense numerical single-channel or multi-channel array" but does not seem to include a format enum.
How do I understand which format each of the numbers returned by VideoCapture::get(CV_CAP_PROP_FORMAT) are? How do I map them into image format options?
As #DanMašek comments above it is the Mat's datatype code, e.g. CV_8UC1, as can be seen, for example, in the code here:
case CV_CAP_PROP_FORMAT:
return CV_MAKETYPE(CV_8U, capture->frame.nChannels);
(with the #define for those type synonyms here.)
#Hammer explains these synomyms further in a comment to his answer here: "the U means unsigned, the S means signed, the F means float. the number is how many bits are involved. CV_16S means a 16 bit signed integer". Further to that the "Cn" means the number of channels, e.g. C3 for RGB.

OpenCV Principal Component Analysis terminology - what actually is a 'sample'?

I'm working with Principal Component Analysis (PCA) in openCV. The constructor inputs for the case I'm interested in are:
PCA(InputArray data, InputArray mean, int flags, double retainedVariance);
Regarding the InputArray 'data' the documents state the appropriate flags should be:
CV_PCA_DATA_AS_ROW indicates that the input samples are stored as
matrix rows.
CV_PCA_DATA_AS_COL indicates that the input samples are
stored as matrix columns.
My question pertains to the use of the term 'samples' in that I'm not sure what a sample is in this context.
For example let's say I have 4 sets of data and for the sake of illustration let's label them A-D. Now each set A through D has 8 elements. They are then set up in the Mat variable I'll use as InputArray as follows:
The question is, which is it:
My sets are samples?
My data elements are samples?
Another way of asking:
Do I have 4 samples (CV_PCA_DATA_AS_COL)
Or do I have 4 sets of 8 samples (CV_PCA_DATA_AS_ROW)
?
As a guess, I'd choose CV_PCA_DATA_AS_COL (i.e. I have 4 samples) - but that's just where my head is at... Until I learn the correct terminology it seems the word 'sample' could apply to either reasoning.
Ugh...
So the answer was found by reversing the logic behind the documentation for the PCA::project step...
Mat PCA::project(InputArray vec)
vec – input vector(s); must have the same dimensionality and the same
layout as the input data used at PCA phase, that is, if
CV_PCA_DATA_AS_ROW are specified, then vec.cols==data.cols (vector
dimensionality)
i.e. 'sample' is equivalent to 'set', and the elements are the 'dimension'.
(and my guess was correct :)

How to perform cross addition on an image in opencv

i am reading a tutorial and there is an equation as shown in the image. I know that sign in the image called cross addition, but my question is is there any method in opencv that performs cross addition?
This 'plus in a circle' in this context most likely refers to Direct addition of Matrices
In particular, the unary notation ⊕I1..n refers to the construction of a diagonalised matrix of the matrices I.
For example, suppose we have:
There is no single method in OpenCV that performs this but you can easily use existing matrix operations to do it by:
Create output matrix of correct size and init with zeros
Iterate over matrices to be direct added and set appropriate esubrange of output matrix

How to normalize OpenCV feature descriptors to an integer scale?

OpenCV SURF implementation returns a sequence of 64/128 32 bit float values (descriptor) for each feature point found in the image. Is there a way to normalize this float values and take them to an integer scale (for example, [0, 255])?. That would save important space (1 or 2 bytes per value, instead of 4). Besides, the conversion should ensure that the descriptors remain meaningful for other uses, such as clustering.
Thanks!
There are other feature extractors than SURF. The BRIEF extractor uses only 32 bytes per descriptor. It uses 32 unsigned bytes [0-255] as its elements. You can create one like this: Ptr ptrExtractor = DescriptorExtractor::create("BRIEF");
Be aware that a lot of image processing routines in OpenCV need or assume that the data is stored as floating-point numbers.
You can treat the float features as an ordinary image (Mat or cvmat) and then use cv::normalize(). Another option is using cv::norm() to find the range of descriptor values and then cv::convertTo() to convert to CV_8U. Look up the OpenCV documentation for these functions.
The descriptor returned by cv::SurfFeatureDetector is already normalized. You can verify this by taking the L2 Norm of the cv::Mat returned, or refer to the paper.

OpenCV CalcPca input data

I am trying to implement a face recognition training function with opencv, using "eigenfaces". I have the sample data, but I can't find any info on CalcPCA function arguments. All I know is that it takes data matrix, reference to average eigenface matrix, reference to eigen vector, and reference to eigen values matrix.
My question is, how should I pass the data from several test image matrices into the first argument of CalcPCA so I can get the average eigenface and vectors?
This seems to be a good example: http://tech.groups.yahoo.com/group/OpenCV/message/47627
You can do in this way:
You have for example 10 Mat where each math represent an image.
Now you can create a new Mat that you can put into this new Mat the previus 10 Mat.
At this point use Mat.pushback(...) to insert the 10 Mat.
Hope this is helpful for you.
Marco

Resources