OpenCV - IplImage - c++

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.

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.

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

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!

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?

Is it safe converting a Mat to itself with different type?

Suppose I want to use a Mat as float. The following code can be compiled without error. However, is it safe doing this?
Mat im = imread('test.jpg', CV_LOAD_IMAGE_GRAYSCALE);
im.convertTo(im, CV_32F1);
I want to do this because it's written more compact, otherwise I need to create a temporary Mat.
The documentation of Mat::convertTo() doesn't give much information about the memory usage of the function.
Mat::convertTo function should be safe to use when using inplace calls (i.e. same input and output Mat objects).
According to OpenCV DevZone, this function did have a bug when using inplace calls, but it was fixed a few years ago.

the output of cv::imwrite vs cv::imencode

Does cv::imencode have an identical behavior to cv::imwrite when converting cv::Mat to JPEG? I know that first one write to buffer and the second write to file but I am asking about the written content.
when you call cv::imwrite() it did not call cv::imencode() internally! both functions uses internal ImageEncoder. take a look at loadsave.cpp