Reputation: 5444
I ve got two vectors and I want to convert them Mat files in order to use opencv's SVM train. The vectors that I have got is one a vector for train data and a vector for labels. How is it possible to convert them to the necessary type from svmtrain??/
for(int i=0; i<face.size();i++){
img = imread("faceNface/face/"+face.at(i),1);
cvtColor(img, img, CV_BGR2GRAY);
img = img.reshape(1, 12100).t();
trainData.push_back(img);
lbs.push_back(1);
}
for(int i=0; i<non_face.size();i++){
img = imread("faceNface/nonface/"+non_face.at(i), 1);
cvtColor(img, img, CV_BGR2GRAY);
img = img.reshape(1, 12100).t();
trainData.push_back(img);
lbs.push_back(-1);
}
Upvotes: 1
Views: 1561
Reputation: 5250
If your label vector type is std::vector<double>
then you can use the following function. If it is not you need to do some changes. I believe you can figure it out.
cv::Mat Classifier::vectorToMat(std::vector<double> data)
{
int size = data.size();
cv::Mat mat(size, 1, CV_32F);
for(int i = 0; i < size; ++i)
{
mat.at<float>(i, 0) = data[i];
}
return mat;
}
For the data itself, I have two recommendations for you;
1) Convert your 1x12100 sized image to std::vector<double>
, then you will have std::vector<std::vector<double>>
as trainData
, then you can use following function.
cv::Mat Classifier::vectorToMat(std::vector<std::vector<double> > data)
{
int height = data.size();
std::vector<double> vectorData = data[0];
int width = vectorData.size();
cv::Mat mat(height, width, CV_32F);
for(int i = 0; i < height; ++i)
{
for(int j = 0; j < width; ++j)
{
mat.at<float>(i,j) = data[i][j];
}
}
return mat;
}
2) Modify the code that I posted to do the same operation with std::vector<cv::Mat>
. Actually doing this is better because if you do this you don't need to convert cv::Mat
to std::vector<double>
in the first place, and do less operation.
Hope it helps.
Upvotes: 4