how to define and smooth a 3D cv::Mat - c++

I created a 3D matrix as cv::mat which contain on each axis (X, Y and Z) avalue from 0 to 255, as follow:
int sizes[] = { 100, 100, 100 };
Mat *matrix;
matrix = new Mat(3,sizes, CV_32FC1, cv::Scalar(0));
for(int i=0;i<100;i++)
for(int j=0;j<100;j++)
for(int k=0;k<100;k++){
//some values are 255
matrix->at<float>(i,j,k) = 0;
// and some of them are 255 : (TODO)
}
And now I want smooth the whole 3D matrix, how I can do that with opencv lib?
Any help will be appreciated,

I would not use new Mat() to initialize an OpenCV Mat. There is no need for this. You also run the risk of forgeting to free your Matrix once finished with it.
Simply use:
Mat matrix(3,sizes, CV_32FC1, cv::Scalar(0));
As for creating a 3D Matrix you can find a good example here
If you wish to filter the 3D Matrix with a Gaussian filter you will find a simple example here

Related

Resizing mat openCV in Visual Studio

In c++, Visual Studio, I have image
cv::Mat EnergyImage;
which is 2D with dimension of 2x6000000
I want to convert it to 2 image with size of 200x300x100(=6000000), better to say two 3d images.
how can I reshape it and access to 2 image seprately?
OpenCV provides the cv::Mat constructor API where you can create an n-dimensional Matrix with arbitrary bins in each dimension as:
cv::Mat m = cv::Mat(1, 6000000, CV_8UC1, cv::Scalar(100));
int sizes[3] = {200, 300, 100};
cv::Mat multiDimMat = cv::Mat(3, sizes, CV_8UC1, &m.data[0]);
The above code would suffice if you had linear cv::Mat with only one row, but since your input cv::Mat has 2 rows and you want 2 separate cv::Mat objects for each row, you may try:
for(int i = 0; i < m.rows; i++)
{
const uchar* mi = m.ptr<uchar>(i);
// Perform the same routine as above code
}

EXC_BAD_ACCESS error OpenCV?

I'm trying write a code for image segmentation in OpenCV. As a part of the image processing, I'm trying to detect the edges of a test image using Sobel filter.
In order to find the magnitude of gradient on both dX and dY direction, I'm computing the euclidean distance of both the gradients. But when I run the code I get the above error. I do know that the above error occurs when I am trying "ACCESS" an unavailable location in memory, but I am sure I have defined all Mat in my code.
This is part of my code.
//Blur the raw image to remove noise
GaussianBlur(src, src, kernel, 2);
//Run sobel edge detector
Sobel(src, edgeX, src.depth(), 1, 0);
Sobel(src, edgeY, src.depth(), 0, 1);
edge = Mat::zeros(317,554,CV_8UC1);
for (int r = 0; r < edgeX.rows; r++)
{
for (int c = 0; c < edgeY.cols; c++)
{
edge.at<double>(r,c) = sqrt((edgeX.at<double>(r,c)*edgeX.at<double>(r,c)) + (edgeY.at<double>(r,c)*edgeY.at<double>(r,c)));
}
}
where:
src: the RGB test image
edgeX: sobel output with dX gradient
edgeY: sobel output with dY
edge: is the Mat with the euclidean distance.
I get the error at this line
edge.at<double>(r,c) = sqrt((edgeX.at<double>(r,c)*edgeX.at<double>(r,c)) + (edgeY.at<double>(r,c)*edgeY.at<double>(r,c)));
when trying to access edge.at<double>(316,395)
How do I debug this?
What am I doing wrong?
edge is a matrix of type CV_8UC1, which means a matrix of uchar, not of double.
You need to access it with at<uchar>:
edge.at<uchar>(r,c) = sqrt((edgeX.at<uchar>(r,c)*edgeX.at<uchar>(r,c)) + (edgeY.at<uchar>(r,c)*edgeY.at<uchar>(r,c)));
You can avoid this kind of problems using Mat_<Tp>, that allows also easier access without using the .at function:
Mat1b edge(317,554,uchar(0));
for (int r = 0; r < edgeX.rows; r++) {
for (int c = 0; c < edgeY.cols; c++) {
edge(r,c) = sqrt((edgeX(r,c)*edgeX(r,c)) + (edgeY(r,c)*edgeY(r,c)));
}
}
In this case, you can also use cv::magnitude which performs the same operation you're doing with your for loops (but it needs matrices of float):
Sobel(src, edgeX, CV_32F, 1, 0);
Sobel(src, edgeY, CV_32F, 0, 1);
Mat edge;
magnitude(edgeX, edgeY, edge);
// Convert to CV_8UC1
edge.convertTo(edge, CV_8UC1);

PCA Project and Backproject in OpenCV 2.3 (C++)

I'm working on a face recognition project and I am having problems when projecting on PCA subspace.
When I pass a mat vector to my funcion with the resized images, I project them, and then I reconstruct them to verify it's working well, but all I have in "Cam" window is a grey image (all same color).
I don't know what I am doing bad.
This is the function:
void doPCA (const vector<Mat>& images)
{
int nEigens = images.size()-1;
Mat data (images.size(), images[0].rows*images[0].cols, images[0].type() );
for (int i = 0; i < images.size(); i++)
{
Mat aux = data.row(i);
images[i].reshape(1,1).copyTo(aux);
}
PCA pca(data,Mat(),CV_PCA_DATA_AS_ROW,nEigens);
//Project images
Mat dataprojected(data.rows, nEigens, CV_32FC1) ;
for(int i=0; i<images.size(); i++)
{
pca.project(data.row(i), dataprojected.row(i));
}
//Backproject to reconstruct images
Mat datareconstructed (data.rows, data.cols, data.type());
for(int i=0; i<images.size(); i++)
{
pca.backProject (dataprojected.row(i), datareconstructed.row(i) );
}
for(int i=0; i<images.size(); i++)
{
imshow ("Cam", datareconstructed.row(i).reshape(1,images[0].rows) );
waitKey();
}
}
I think this post is a duplicate of:
PCA + SVM using C++ Syntax in OpenCV 2.2
Ah, I have found the error in your code. When you create the data matrix you do:
images[i].reshape(1,1).copyTo(aux);
You have to use convertTo to convert the data into the correct type and copy it to your data matrix:
images[i].reshape(1,1).convertTo(aux, CV_32FC1, 1/255.);
Then the normalized eigenvectors should be ok. And don't forget to to normalize the values between 0 and 255 before displaying them, you can use cv::normalize to do this, here's a simple function for turning it into grayscale:
Mat toGrayscale(const Mat& src) {
Mat srcnorm;
cv::normalize(src, srcnorm, 0, 255, NORM_MINMAX, CV_8UC1);
return srcnorm;
}
You may want to look at the example in my blog:
http://bytefish.de/blog/pca_in_opencv#simple_example

How to assign 2d images to planes of a 3d OpenCV Mat, then extract 2D Mats along a different axis?

I want to make a 3D Mat out of stacked 2D Mats and then pull out 2D Mats along a different axis of the 3D Mat. I know I can build this up manually accessing every row or col of every image but is there a better to do it like the following?
vector<cv::Mat> images;
... populate vector with images
int sz[] = {images[0].rows, images[0].cols, images.size()}
cv::Mat cube(3, sz, images[0].type() );
cube = Scalar(0);
for (int i = 0; i < images.size(); i++) {
... place images into 3d mat
}
vector<cv::Mat> image_rows;
for (int i = 0; i < images[0].rows(); i++) {
... push image planes into image_rows along width and depth of 3D cube matrix
}
I have done a lot of work with opencv and as far as I know that is the best way to do it. You could make a Mat of higher dimensional vectors but then your access would be even less clean. Stick to the vector its your best bet.

PCA + SVM using C++ Syntax in OpenCV 2.2

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!