conversion of cvMat to IplImage - opencv

I have two programs, one which accepts an Image as a matrix and does the processing like tracking objects using contour detection.The second program takes image as an array(IplImage) and counting no. of objects.But I want to merge these programs to count as well as track these objects.How can I merge them ?

In the following code left is CvMat, left1 is IplImage. In this way you can manually convert cvmat to IplImage.
for (int y=0;y<height1;y++)
{
uchar* leftdata=(uchar*)(left->data.ptr+y*left->step);
uchar* left1data=(uchar* )(left->imageData+y*left1step);
for (int x=0;x<width1;x++)
left1data[x]=leftdata[x];
}
or here is another link How to convert a Mat variable type in an IplImage variable type in OpenCV 2.0?

Related

concatenate a rotation and a translation of cv::Mat to a eigen

I am doing a 6-dof transformation with the RANSAC given in OpenCV and I now want to convert two matrices of cv::Mat to an Isometry3d of Eigen but I didn't find good examples about this problem.
e.g.
cv::Mat rot;
cv::Mat trsl;
// the rot is 3-by-3 and trsl is 3-by-1 vector.
Eigen::Isometry3d trsf;
trsf.rotation = rot;
trsf.translation = trsl; // I know trsf has two members but it seems not the correct way to do a concatenation.
Anyone give me a hand? Thanks.
Essentially, you need an Eigen::Map to read the opencv data and store it to parts of your trsf:
typedef Eigen::Matrix<double, 3, 3, Eigen::RowMajor> RMatrix3d;
Eigen::Isometry3d trsf;
trsf.linear() = RMatrix3d::Map(reinterpret_cast<const double*>(rot.data));
trsf.translation() = Eigen::Vector3d::Map(reinterpret_cast<const double*>(trsl.data));
You need to be sure that rot and trsl indeed hold double data (perhaps consider using cv::Mat_<double> instead).

OpenCV how To convert Mat to uchar [] []

I have the following code:
Mat image = cvLoadImage("our path ");
How do I convert the result to a two dimensional array of uchar? Please help me know how to read it as bytes and put it in the array.
plz can you tell me how to display the resulat as array i have problem with coding it .
First, cvLoadImage() returns IplImage* not Mat. It is converted implicitly to Mat, so you can use it but it won't be released properly, leading to memory leak. You should use imread instead.
As for the question itself, you can use ptr function. For example:
uchar* p = image.ptr<uchar>(i);
p is now pointing to i-th line of image. You can work with it as usual array of uchar (read, change, copy, and so on).

converting cvmat to iplimage

How can I convert a cvMat matrix to IplImage that can be saved using cvSaveImage, in C using OpenCV?
I learnt about a function cvGetImage(const CvArr* arr, IplImage* imageHeader). I understand that arr stands for the cvMat Array, but could not really understand what 'image header' actually is. Is that the pointer that stores the image ?? That is, would the following work?
clusters = cvCreateMat( image2_size, 1, CV_32SC1 );
IplImage *kmeans;
cvGetImage(clusters, &kmeans);
cvSaveImage("kmeans.jpg", kmeans);
//clusters is the output matrix after performing k-means clustering
// on a certain image.

get matrix of image in opencv

//Open the image
Mat img_rgb = imread("sudoku2.png", CV_LOAD_IMAGE_GRAYSCALE);
if (img_rgb.empty())
{
cout<<"Cannot open the image"<<endl;
return;
}
Mat img_bw = img_rgb > 128;
imwrite("image_bw.jpg", img_bw);
Now, I want to get all pixels of img_bw and save it into a matrix M (int[img_bw.rows][img_bw.cols]). How to do it in C++.
What format ?
The raw byte data in cv::Mat is available from the .ptr() function, ie img_bw.ptr().
Opencv also has an xml and json read and write functions for matrices, just by using the << operator - see opencv tutorial on xml and yaml i/o
EDIT: In c++ you can access pixels with the .at operator.
Use img_data.at<uchar>(x,y) for an unsigned char (CV_8U) pixel and img_data.at<float>(x,y) for a CV_32F image.

Can not convert with cvMerge,DFT

I am trying to make the dft of one single channeled image, and as cvDft is expecting complex values, I was adviced to merge the original image with another image with all 0's so this last one will be considered as imaginary part.
My problem comes when using cvmerge function,
Mat tmp = imread(filename,0);
if( tmp.empty() )
{cout << "Usage: dft <image_name>" << endl;
return -1;}
Mat Result(tmp.rows,tmp.cols,CV_64F,2);
Mat tmp1(tmp.rows,tmp.cols,CV_64F, 0);
Mat image(tmp.rows,tmp.cols,CV_64F,2);
cvMerge(tmp,tmp1,image);`
It gives me the next error: can not convert cvMAt to cvArr
Anyone could help me? thanks!
1) it seems like you're mixing up 2 different styles of opencv code
cv::Mat (- Mat) is a c++ class from the new version of opencv, cvMerge is a c function from the old version of opencv.
instead of using cvmerge use merge
2) you're trying to merge a matrix (tmp) of type CV_8U (probably) with a CV_64F
use convertTo to get tmp as CV_64F
3) why is your Result & image mats (the destination mat) are initializes to cv::Scalar(2)? i think you're misusing the constractor parameters. see here for more info.
4) you're image mat is a single channel mat and you wanted it as a 2 channel mat (as mentioned in the question), change the declaration to
Mat image(tmp.rows,tmp.cols,CV_64FC2);

Resources