I have a file of 100+ images which i need to store each image into their individual matrices. Is there any way i can do this instead of hard-coding (as shown below)?
Mat src1 = imread("ts_04-11-21_16-27-00-mod", CV_8UC1);
Mat src2 = imread("ts_04-11-21_16-27-01-mod", CV_8UC1);
Mat src3 = imread("ts_04-11-21_16-27-02-mod", CV_8UC1);
Mat src4 = imread("ts_04-11-21_16-27-03-mod", CV_8UC1);
Mat src5 = imread("ts_04-11-21_16-27-04-mod", CV_8UC1);
I'm using Opencv and C++.
Maybe something like:
vector<Mat> images;
for(int i=0;i<n;i++)
{
Mat in = imread("ts_04-11-21_16-27-0"+i+"-mod", CV_8UC1);
images.push_back(in);
}
beware that reading a lot of images can be very memory consuming.
Related
What is the best way to represent with OpenCV(C++) a mat of mats? At the moment in my code, I use a vector of vector<Mat> and I need the benefits of using a matrix.
Limited to my searches, unfortunately such thing is not defined in Opencv-C++ (python version has it though). vector is all you can get.
Although if all of your images are grayscale and have the same size, you can define an n-channel image and put each of your images into one of the channels, for example:
Mat im1 = Mat::ones(n_row , n_col , CV_8UC1);
Mat im2 = Mat::ones(n_row , n_col , CV_8UC1);
Mat im3 = Mat::ones(n_row , n_col , CV_8UC1);
... // n similar images...
Mat img(n_row , n_col , CV_8UC(n));
vector<Mat> vec;
vec.push_back(im1);
vec.push_back(im2);
vec.push_back(im3);
... // push back all the n images
merge(vec,img);
I have an image which I am putting into a vector of uchar which I am sending from processor to processor using open mpi, however I need to put the vector of uchar into a cv::Mat.
Is there an easy way to do this?
cv::Mat image_on_proc = newpopulation(cv::Rect(0, start, population.cols, rows_in_section));
std::vector<byte> img = matToBytes(image_on_proc);
std::vector<uchar> test;
for(int i=0; i<image_on_proc.rows; i++)
{
for(int j=0; j<image_on_proc.cols; j++)
{
test.push_back(image_on_proc.at<uchar>(i,j));
}
}
MPI_Barrier(MPI_COMM_WORLD);
MPI_Send(&test[0],test.size()*sizeof(uchar), MPI_BYTE, 0, 99, MPI_COMM_WORLD);
The above shows how I put the image into the vector and send it, there is a corresponding recv and it works, however I do not know how to turn it back into an image.
You can store a Mat into a vector<uchar> using:
Mat img = ...; // img must be CV_8UC1 in this example
vector<uchar> v(img.begin<uchar>(), img.end<uchar>());
and back into a Mat using:
Mat img2(img.rows, img.cols, img.type(), v.data());
Note that here you're not copying values, but just creating a Mat header for the data in v, so img2 will reflect any change done on v. If v goes out of scope, then img2 becomes invalid. You can simply use clone() to copy data, like:
Mat img3 = Mat(img.rows, img.cols, img.type(), v.data()).clone();
I'm converting a BGR image to YCrCb and saving it in the disk; however, the output images are gray, how can I save the images to disk with color?
Here is just an example of how my code looks like:
Mat img = imread("...");
Mat img2;
cvtColor(img, img2, CV_BGR2YCrCb);
vector<Mat> planes;
split(imgColorConverted, planes);
imwrite(".../planes1.jpg", planes[0]);
imwrite(".../planes2.jpg", planes[1]);
imwrite(".../planes3.jpg", planes[2]);
When I open the saved images, it is gray. Is it possible to save the images as the following example that I found on Wikipedia (Wikipedia - YCbCr)?:
Thank you!
In you case, you save 1 channel pictures, so it's gray. In fact, what you should do is:
Mat img = imread("...");
Mat img2;
cvtColor(img, img2, CV_BGR2YCrCb);
vector<Mat> planes;
split(imgColorConverted, planes);
Mat* planes2 = new Mat[3];
Mat emptyMat(planes[0].size(), planes[0].type(), Scalar(0));
Mat Cr, Cb;
imwrite(".../planes1.jpg", planes[0]);
// Set Cr channel on R channel
planes2[0] = emptyMat;
planes2[1] = emptyMat;
planes2[2] = planes[1];
merge(planes2, 3, Cr);
imwrite(".../planes2.jpg", Cr);
// Set Cb channel on B channel
planes2[0] = planes[2];
planes2[1] = emptyMat;
planes2[2] = emptyMat;
merge(planes2, 3, Cb);
imwrite(".../planes3.jpg", Cb);
I'm beginner in opencv. I have not gotten main concepts of opencv in details.
So maybe my code it's too dumb;
Out of my curiosity I want to try machine learning functions like a KNN, ANN.
I have the set of images with size 28*28 pixels. I want to do train cassifier for digit recognition. So first I need to assemble train set and train classes;
Mat train_data = Mat(rows, cols, CV_32FC1);
Mat train_classes = Mat(rows, 1, CV_32SC1);
Mat img = imread(image);
Mat float_data(1, cols, CV_32FC1);
img.convertTo(float_data, CV_32FC1);
How to fill train_data with float_data ?
I thought It was smth like this:
for (int i = 0; i < train_data.rows; ++i)
{
... // image is a string which contains next image path
image = imread(image);
img.convertTo(float_data, CV_32FC1);
for( int x = 0; x < train_data.cols; x++ ){
train_data.at<float> (i, x) = float_data.at<float>( x);;
}
}
KNearest knn;
knn.train(train_data, train_classes);
but it's of course doesn't work . . .
Please, tell me how to do it right. Or at least suggest the books for dummies :)
Mat train_data; // initially empty
Mat train_labels; // empty, too.
// for each img in the train set :
Mat img = imread("image_path");
Mat float_data;
img.convertTo(float_data, CV_32FC1); // to float
train_data.push_back( float_data.reshape(1,1) ); // add 1 row (flattened image)
train_labels.push_back( label_for_image ); // add 1 item
KNearest knn;
knn.train(train_data, train_labels);
it's all the same for other ml algos !
I'm having problems getting PCA and Eigenfaces working using the latest C++ syntax with the Mat and PCA classes. The older C syntax took an array of IplImage* as a parameter to perform its processing and the current API only takes a Mat that is formatted by Column or Row. I took the Row approach using the reshape function to fit my image's matrix to fit in a single row. I eventually want to take this data and then use the SVM algorithm to perform detection, but when I do that all my data is just a stream of 0s. Can someone please help me out? What am I doing wrong? Thanks!
I saw this question and it's somewhat related, but I'm not sure what the solution is.
This is basically what I have:
vector<Mat> images; //This variable will be loaded with a set of images to perform PCA on.
Mat values(images.size(), 1, CV_32SC1); //Values are the corresponding values to each of my images.
int nEigens = images.size() - 1; //Number of Eigen Vectors.
//Load the images into a Matrix
Mat desc_mat(images.size(), images[0].rows * images[0].cols, CV_32FC1);
for (int i=0; i<images.size(); i++) {
desc_mat.row(i) = images[i].reshape(1, 1);
}
Mat average;
PCA pca(desc_mat, average, CV_PCA_DATA_AS_ROW, nEigens);
Mat data(desc_mat.rows, nEigens, CV_32FC1); //This Mat will contain all the Eigenfaces that will be used later with SVM for detection
//Project the images onto the PCA subspace
for(int i=0; i<images.size(); i++) {
Mat projectedMat(1, nEigens, CV_32FC1);
pca.project(desc_mat.row(i), projectedMat);
data.row(i) = projectedMat.row(0);
}
CvMat d1 = (CvMat)data;
CvMat d2 = (CvMat)values;
CvSVM svm;
svm.train(&d1, &d2);
svm.save("svmdata.xml");
What etarion said is correct.
To copy a column or row you always have to write:
Mat B = mat.col(i);
A.copyTo(B);
The following program shows how to perform a PCA in OpenCV. It'll show the mean image and the first three Eigenfaces. The images I used in there are available from http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html:
#include "cv.h"
#include "highgui.h"
using namespace std;
using namespace cv;
Mat normalize(const Mat& src) {
Mat srcnorm;
normalize(src, srcnorm, 0, 255, NORM_MINMAX, CV_8UC1);
return srcnorm;
}
int main(int argc, char *argv[]) {
vector<Mat> db;
// load greyscale images (these are from http://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html)
db.push_back(imread("s1/1.pgm",0));
db.push_back(imread("s1/2.pgm",0));
db.push_back(imread("s1/3.pgm",0));
db.push_back(imread("s2/1.pgm",0));
db.push_back(imread("s2/2.pgm",0));
db.push_back(imread("s2/3.pgm",0));
db.push_back(imread("s3/1.pgm",0));
db.push_back(imread("s3/2.pgm",0));
db.push_back(imread("s3/3.pgm",0));
db.push_back(imread("s4/1.pgm",0));
db.push_back(imread("s4/2.pgm",0));
db.push_back(imread("s4/3.pgm",0));
int total = db[0].rows * db[0].cols;
// build matrix (column)
Mat mat(total, db.size(), CV_32FC1);
for(int i = 0; i < db.size(); i++) {
Mat X = mat.col(i);
db[i].reshape(1, total).col(0).convertTo(X, CV_32FC1, 1/255.);
}
// Change to the number of principal components you want:
int numPrincipalComponents = 12;
// Do the PCA:
PCA pca(mat, Mat(), CV_PCA_DATA_AS_COL, numPrincipalComponents);
// Create the Windows:
namedWindow("avg", 1);
namedWindow("pc1", 1);
namedWindow("pc2", 1);
namedWindow("pc3", 1);
// Mean face:
imshow("avg", pca.mean.reshape(1, db[0].rows));
// First three eigenfaces:
imshow("pc1", normalize(pca.eigenvectors.row(0)).reshape(1, db[0].rows));
imshow("pc2", normalize(pca.eigenvectors.row(1)).reshape(1, db[0].rows));
imshow("pc3", normalize(pca.eigenvectors.row(2)).reshape(1, db[0].rows));
// Show the windows:
waitKey(0);
}
and if you want to build the matrix by row (like in your original question above) use this instead:
// build matrix
Mat mat(db.size(), total, CV_32FC1);
for(int i = 0; i < db.size(); i++) {
Mat X = mat.row(i);
db[i].reshape(1, 1).row(0).convertTo(X, CV_32FC1, 1/255.);
}
and set the flag in the PCA to:
CV_PCA_DATA_AS_ROW
Regarding machine learning. I wrote a document on machine learning with the OpenCV C++ API that has examples for most of the classifiers, including Support Vector Machines. Maybe you can get some inspiration there: http://www.bytefish.de/pdf/machinelearning.pdf.
data.row(i) = projectedMat.row(0);
This will not work. operator= is a shallow copy, meaning no data is actually copied. Use
cv::Mat sample = data.row(i); // also a shallow copy, points to old data!
projectedMat.row(0).copyTo(sample);
The same also for:
desc_mat.row(i) = images[i].reshape(1, 1);
I would suggest looking at the newly checked in tests in svn head
modules/core/test/test_mat.cpp
online here : https://code.ros.org/svn/opencv/trunk/opencv/modules/core/test/test_mat.cpp
has examples for PCA in the old c and new c++
Hope that helps!