How do I convert a matrix in Swift to cv::Mat? - c++

Bigger context: I'm trying to use CoreMotion's CMRotationMatrix in C++ code. I'm using a wrapper to bring Swift code into C++.
However, I don't know how to bring the CMRotationMatrix into my C++ opencv code. With this question (How to retrieve value from C++ matrix in Swift?) I get the sense that I'm supposed to convert the CMRotationMatrix into uint8_t array of arrays but I'm not sure if this is the right path and even if it is, I don't know how to convert uint8_t to cv::Mat.
Any help would be appreciated. Thanks!

Related

operating with complex struct/class in Julia from C++

In the recent project, I am trying to write a simple wrapper of C++ library (OpenCV) to be utilized in Julia with the use of CxxWrap.
The case of C++ code (where arguments and return types are my own, rather simple, structs) is working.
The problem I have is with more complex data structures (defined in, let's say OpenCV); in our case (I want it to be simple to understand) I want to get information about the frame, so I execute:
using PyCall
const cv2 = pyimport("cv2")
module CppHello
using CxxWrap
#wrapmodule(joinpath(#__DIR__,"libhello"))
function __init__()
#initcxx
end
end
cap = CppHello.openVideo() // (*)
to above I have two questions:
do I have to explicitly define returned type by openVideo() -- suppose for this moment that I want to use only my library in C++ to start any of the OpenCV functions;
if "No" to the above can I do something like that:
cap = cv2.VideoCapture(0) # from library
cap.isOpened() && frm = cap.frame()
The point is that I am interested only in a few operations at frame along with passing returned value to other procedures (show frame utilizing C++ on the screen or save in file).
Motivation is a problem of low performance of imshow() executed on Julia level with used PyCall (in contrary with goodFeaturesToTrack or calcOpticalFlowPyrLK) and drastic low FPS compared with C++.
Maybe there is another solution I unnoticed.
As I have a problem with (*) I thought that maybe I can simply write a struct (of known elements) of pointers to hold the data returned by C++ functions?
As it is my first edition of the question, I will be grateful for any info about correctness and completeness.

Equivalent of .NET's MemoryMarshal in C++ (Arduino)?

I am new to Arduino and C++ development coming from C# so likely am missing so fundamental understanding. Kindly answer accordingly.
Context
I am writing an Arduino sketch whereby I form a Http GET request in order to receive data from a Web API. In receiving the response, I am able to read the stream into byte data[] using client.read(data,client.available()). In my application, I know each byte represents a char in ASCII encoding. For processing of the response, I wish to convert this byte[] to a char[], however this got me thinking...
Question
How in C++ can I generically cast a byte[] to another known type without copying memory? In C# I would achieve this using the MemoryMarshal. Something tells me a I should be able to simply initialise an object from a pointer?
Many Thanks
byte is not a native C++ type, the Arduino environment creates it using a typedef, it is actually a uint_8 (unsigned 8 bit integer) type. This is also the underlying type of a char, so you don't actually have to do anything. A byte array and char array are already the same data type, they're just labelled differently.
You could use a (char) cast to improve your code clarity, and this would not copy any data.
Update:
You can use a cast in C or C++ to tell the compiler to interpret some raw data as a different data type. In the example below, an array of 6 floats containing 2D vectors arranged XYXYXY, is cast to an array of 2D vector structures. This is done without any data being copies. However there are many pitfalls with this technique. You need be absolutely sure how the compiler is laying out the underlying storage of the structure. This is not defined by the standard so can vary between compilers, many will add padding for word alignment which will vary based on the architecture used. So use this method with care.
struct My2DVector {
float x, y;
}
float flatVectorData[] = { 0.0, 1.0, 2.0, 2.5, -5.0, 3.0};
// Cast the pointer to float to a pointer to My2DVector
My2DVector* structVectorData = (My2DVector*)flatVectorData;
printf("Vector 2 (%f %f)\n", structVectorData[1].x, structVectorData[1].y);

converting a matlab array (MAT-file) to C++ array

I have a 2-D double-precision array in MATLAB that contains specific data.
I want to use this array in c++, so I save the array in a mat-file.
I know that MATLAB has some c functions that provide reading mat-file in c++ (matdsgn , matOpen , ...), but I don't know how to use them in a c++ program. Actually, I don't know how to use a C library in C++.
Any help would be appreciated.
If you have MATLAB 2017a, there is a built-in function. See this MATHWORKS link: Math Works

How to obtained and use the type of variable?

I'm writing a function process Mat, using OpenCV. My problem is that when the type of Input matrix changes, I need corresponding type of ptr.
For example:
for CV_8UC1, I need ptr<uchar>,
for CV_16UC1 I need ptr<UINT16>,
However I cannot use Mat.type(), since it only returns an int for enum.
I thought of using switch-case, but it's rather verbose. Is there any concise way to do it?

OpenCV - IplImage

What is IplImage in OpenCV? Can you just explain what is meant by it as a type? When should we use it?
Thanks.
Based on the 'c++' tag you have in your question:
You should not use it, you should use cv::Mat. Every IplImage is a cv::Mat internally.
IplImage is only used in the C API. It is a kind of CvMat and holds image data. Its name originates from OpenCV's roots (Intel IPL).
It is not relevant anymore if you use the C++ API of OpenCV.
IplImage and cv::Mat are different header types for matrix/image data. They're basically compatible with each other, but IplImage is used in OpenCV's C API, while cv::Mat is used in the new C++ API.
When you decide to use OpenCV's C API, you will mostly use IplImages. Otherwise you will mostly use cv::Mats.
Of course, you can convert IplImages and cv::Mats to each other, e.g.
cv::Mat mat(myIplImage);
In this case, they share the same underlying data, i.e. modifications made through either header will be visible regardless of what header you're using to access the data.
Deep-copy (not only header is "transformed"/copied, but also the underlying data) is possible with
cv::Mat mat(myIplImage, true)
Note that multiple IplImages can also point to the same underlying data, as can multiple cv::Mats.
When working with OpenCV and similar libraries it is important to notice that cv::Mats and IplImages are only "headers" for the actual data. You could say that cv::Mats and IplImages are basically pointers plus important meta information like number of rows, columns, channels and the datatype used for individual cells/pixels of the matrix/image. And, like real pointers, they can reference/point to the same real data.
For example, look at the definition of IplImage: http://opencv.willowgarage.com/documentation/basic_structures.html#iplimage
The most important member is char *imageData;. This pointer references the actual image data. However, the IplImage as a whole contains meta information about the image as well, like number of rows and columns.
The IplImage structure was inherited from the Intel Image Processing Library, in which the format is native. OpenCV only supports a subset of possible IplImage formats, as outlined in the parameter list above.
If you are usin C++ you don´t use IplImage because this only use in a C program, (IplImage is in C API).
In addition if you are usin C you need post Cv before the variables names: "CvMat *" "CvPoint " "IplImage " etc.
It ´s better to use C++ code, you don´t use Cv: "Mat *" etc. but you can´t use the IplImage type, instead of IplImage you use a Mat type for example in findchessboardcorner function.