I would like to declare an cv::Mat object and somewhere else in my code, change its dimension (nrows and ncols). I couldn't find any method in the documentation of OpenCV. They always suggest to include the dimension in the constuctor.
An easy and clean way is to use the create() method. You can call it as many times as you want, and it will reallocate the image buffer when its parameters do not match the existing buffer:
Mat frame;
for(int i=0;i<n;i++)
{
...
// if width[i], height[i] or type[i] are != to those on the i-1
// or the frame is empty(first loop)
// it allocates new memory
frame.create(height[i], width[i], type[i]);
...
// do some processing
}
Docs are available at https://docs.opencv.org/3.4/d3/d63/classcv_1_1Mat.html#a55ced2c8d844d683ea9a725c60037ad0
If you mean to resize the image, check resize()!
Create a new Mat dst with the dimensions and data type you want, then:
cv::resize(src, dst, dst.size(), 0, 0, cv::INTER_CUBIC);
There are other interpolation methods besides cv::INTER_CUBIC, check the docs.
Do you just want to define it with a Size variable you compute like this?
// dynamically compute size...
Size dynSize(0, 0);
dynSize.width = magicWidth();
dynSize.height = magicHeight();
int dynType = CV_8UC1;
// determine the type you want...
Mat dynMat(dynSize, dynType);
If you know the maximum dimensions and only need to use a subrange of rows/cols from the total Mat use the functions cv::Mat::rowRange and/or cv::Mat::colRange
http://docs.opencv.org/modules/core/doc/basic_structures.html#mat-rowrange
Related
This answer states how to copy a row of a matrix to another row: How to copy a row of a Mat to another Mat's column in OpenCv?
However if I try to copy a row of a matrix to a column vector the program is ends abruptly.
Example:
Mat A(640,480,CV_64F);
Mat B(480,1,CV_64F);
A.row(0).copyTo(B.col(0));
A possible workaround is:
Mat A(640,480,CV_64F);
Mat B;
A.row(0).copyTo(B);
B = B.t();
But how can I get type CV_64F in B if A is a different type? Is the data type copied, too? And - most important – why do I need this workaround?
copyTo() function can only copy data between the matrices of same size (width and height) and same type. For this reason you can't use it directly to copy row of matrix to column of matrix. Normally OpenCV will reallocate target matrix so that its size and type will match original image, but call to col() function returns constant temporary object that can't be reallocated. As a result your program crushed.
Actually this problem can be solved. You can copy row to column (if they have same amount of elements) and you can copy data between matrices of different types. Mat provides an iterator to its data, and that means you can use all C++ functions that works with iterators. copy() function for example.
Mat A(640, 480, CV_64F);
Mat B(480, 1, CV_32S); // note that the type don't have to be the same
Mat tmp = A.row(0); // no data is copied here. it is needed only because taking iterator to temporary object is really bad idea
copy(tmp.begin<double>(), tmp.end<double>(), B.begin<int>());
Since code like the following (transposing the row only) is not supported by the API:
A.row(0).t().copyTo( B.col(0) );
The workaround is either create a temporary matrix from the row, transpose it, then copy to your target matrix
Mat temp = A.row(0);
temp = temp.t();
temp.copyTo( B.col(0) );
I would rather do something like this though:
Mat A( 640, 480,CV_64F );
Mat B( 1, 480, CV_64F ); /* rather than 480 x 1, make a 1 x 480 first */
A.row(0).copyTo( B.row(0) );
B = B.t(); /* transpose it afterwards */
I presume this is all just because the API is not supporting it yet.
I have two images, the first one smaller than the other. I need to copy the second image on the first image. To do so, I need to set the ROI on the first one, copy the second image onto the first one and then reset the ROI.
However I am using the C++ interface so I have no idea how to do this. In C I could have used cvSetImageROI but this doesn't work on the C++ interface.
So basically whats the C++ alternative to cvSetImageROI?
//output is a pointer to the mat whom I want the second image (colourMiniBinMask) copied upon
Rect ROI (478, 359, 160, 120);
Mat imageROI (*output, ROI);
colourMiniBinMask.copyTo (imageROI);
imshow ("Gravity", *output);
I think you have something wrong. If the first one is smaller than the other one and you want to copy the second image in the first one, you don't need an ROI. You can just resize the second image in copy it into the first one.
However if you want to copy the first one in the second one, I think this code should work:
cv::Rect roi = cv::Rect((img2.cols - img1.cols)/2,(img2.rows - img1.rows)/2,img1.cols,img1.rows);
cv::Mat roiImg;
roiImg = img2(roi);
img1.copyTo(roiImg);
This is the code I used. I think the comments explain it.
/* ROI by creating mask for the parallelogram */
Mat mask = cvCreateMat(480, 640, CV_8UC1);
// Create black image with the same size as the original
for(int i=0; i<mask.cols; i++)
for(int j=0; j<mask.rows; j++)
mask.at<uchar>(Point(i,j)) = 0;
// Create Polygon from vertices
vector<Point> approxedRectangle;
approxPolyDP(rectangleVertices, approxedRectangle, 1.0, true);
// Fill polygon white
fillConvexPoly(mask, &approxedRectangle[0], approxedRectangle.size(), 255, 8, 0);
// Create new image for result storage
Mat imageDest = cvCreateMat(480, 640, CV_8UC3);
// Cut out ROI and store it in imageDest
image->copyTo(imageDest, mask);
I also wrote about this and put some pictures here.
I would like to store all my precalculated Keypoints/decriptors of several images in a Mat list/structure or something, so I could be able to use them later to match them with others image descriptors.
Do you have an idea?
Apparently, there is a way to use
List<Mat>
but i dont know how.
You store the descriptor of one image in one Mat variable. So, basically you have one Mat for one descriptors. So, if you have 100 descriptors then, all of these decriptors should be present in a single Mat. You can do it as following:
Step-1: Declare a vector of Mat type.
vector<Mat> allDescriptors;
Step-2: Then find descriptors for each image and store it in Mat format
Mat newImageDescriptor;
Step-3: finally, push the descriptor calculated above into the vector.
allDescriptors.push_back(newImageDescriptor);
Repeat step-2 & 3 for all of your images
Now, you can access them as following:
You can access the data in vector as you do it in case of arrays
so allDescriptors[0] will give you the first descriptor in Mat format
So, by using a for loop, you can access all of your descriptors.
for(int i=0; i<allDescriptors.size(); i++)
{
Mat accessedDescriptor;
allDescriptors[i].copyTo(accessedDescriptor);
}
If your elements are stored in contiguous array you can assign them to a list at once with:
#include <list>
std::list<Mat> l;
l.assign( ptr, ptr + sz); // where ptr is a pointer to array of Mat s
// and sz is the size of array
Create precalculated elements:
Mat mat1;
Mat mat2;
And the list of elements of this type:
#include <list>
std::list<Mat> l;
Add elements to list:
l.push_back( mat1);
l.push_back( mat2)
note: there are other modifiers that you can use to insert elements. You will find a description of them here. There are other containers which usage you can consider. Selection of appropriate container is very important. You have to take into account the operations that will be crucial to you, which will be called most often.
This is regarding to your another question of copying the vector<Mat> to another vector<Mat>
Lets say you have one vector vector<Mat> des1 and you want to copy it to vector<Mat> des2 then you should do the following:
for(int i=0; i<des1.size(); i++)
{
des1[i].copyTo(des2[i]);
}
Remember that vector<Mat> is something like an arrya of Mat. So, how can you copy a vector to another vector by CopyTo which is used to copy a Matrix.
I am trying to store a IPL_DEPTH_8U, 3 channel image into an array so that I can store 100 images in memory.
To initialise my 4D array I used the following code (rows,cols,channel,stored):
int size[] = { 324, 576, 3, 100 };
CvMatND* cvImageBucket; = cvCreateMatND(3, size, CV_8U);
I then created a matrix and converted the image into the matrix
CvMat *matImage = cvCreateMat(Image->height,Image->width,CV_8UC3 );
cvConvert(Image, matImage );
How would I / access the CvMatND to copy the CvMat into it at the position of stored?
e.g. cvImageBucket(:,:,:,0) = matImage; // copied first image into array
You've tagged this as both C and C++. If you want to work in C++, you could use the (in my opinion) simpler cv::Mat structure to store each of the images, and then use these to populate a vector with all the images.
For example:
std::vector<cv::Mat> imageVector;
cv::Mat newImage;
newImage = getImage(); // where getImage() returns the next image,
// or an empty cv::Mat() if there are no more images
while (!newImage.empty())
{
// Add image to vector
imageVector.push_back(image);
// get next image
newImage = getImage();
}
I'm guessing something similar to:
for ith matImage
memcpy((char*)cvImageBucket->data+i*size[0]*size[1]*size[2],(char*)matImage->data,size[0]*size[1]*size[2]);
Although I agree with #Chris that it is best to use vector<Mat> rather than a 4D matrix, this answer is just to be a reference for those who really need to use 4D matrices in OpenCV (even though it is a very unsupported, undocumented and unexplored thing with so little available online and claimed to be working just fine!).
So, suppose you filled a vector<Mat> vec with 2D or 3D data which can be CV_8U, CV_32F etc.
One way to create a 4D matrix is
vector<int> dims = {(int)vec.size(), vec[0].rows, vec[0].cols};
Mat m(dims, vec[0].type(), &vec[0]);
However, this method fails when the vector is not continuous which is typically the case for big matrices. If you do this for a discontinuous matrix, you will get a segmentation fault or bad access error when you would like to use the matrix (i.e. copying, cloning, etc). To overcome this issue, you can copy matrices of the vector one by one into the 4D matrix as follows:
Mat m2(dims, vec[0].type());
for (auto i = 0; i < vec.size(); i++){
vec[i].copyTo(temp.at<Mat>(i));
}
Notice that both methods require the matrices to be the same resolution. Otherwise, you may get undesired results or errors.
Also, notice that you can always use for loops but it is generally not a good idea to use them when you can vectorize.
I have two images, the first one smaller than the other. I need to copy the second image on the first image. To do so, I need to set the ROI on the first one, copy the second image onto the first one and then reset the ROI.
However I am using the C++ interface so I have no idea how to do this. In C I could have used cvSetImageROI but this doesn't work on the C++ interface.
So basically whats the C++ alternative to cvSetImageROI?
//output is a pointer to the mat whom I want the second image (colourMiniBinMask) copied upon
Rect ROI (478, 359, 160, 120);
Mat imageROI (*output, ROI);
colourMiniBinMask.copyTo (imageROI);
imshow ("Gravity", *output);
I think you have something wrong. If the first one is smaller than the other one and you want to copy the second image in the first one, you don't need an ROI. You can just resize the second image in copy it into the first one.
However if you want to copy the first one in the second one, I think this code should work:
cv::Rect roi = cv::Rect((img2.cols - img1.cols)/2,(img2.rows - img1.rows)/2,img1.cols,img1.rows);
cv::Mat roiImg;
roiImg = img2(roi);
img1.copyTo(roiImg);
This is the code I used. I think the comments explain it.
/* ROI by creating mask for the parallelogram */
Mat mask = cvCreateMat(480, 640, CV_8UC1);
// Create black image with the same size as the original
for(int i=0; i<mask.cols; i++)
for(int j=0; j<mask.rows; j++)
mask.at<uchar>(Point(i,j)) = 0;
// Create Polygon from vertices
vector<Point> approxedRectangle;
approxPolyDP(rectangleVertices, approxedRectangle, 1.0, true);
// Fill polygon white
fillConvexPoly(mask, &approxedRectangle[0], approxedRectangle.size(), 255, 8, 0);
// Create new image for result storage
Mat imageDest = cvCreateMat(480, 640, CV_8UC3);
// Cut out ROI and store it in imageDest
image->copyTo(imageDest, mask);
I also wrote about this and put some pictures here.