Armadillo - delete rows in a cube - c++

I'm using Armadillo C++ library for matrices.
I have a 3d matrix (Cube) of size [73 256 1000].
I would like to take only part of the rows of the cube, e.g. in matlab
A = A(3:66, :, :);
How can I do it with Armadillo? I haven't found anything in the API.
Thanks.

The functionality is listed in Armadillo's documentation, in the subcube views section. You can use span::all to indicate an entire range. to For example:
cube A(100,50,10);
A.randu();
A = A(span(3,66), span::all, span::all);

Related

Applying Affine transform on an image using dlib

I am looking to align two face images using the 68 landmarks learnt by the dlib detector. I know I can convert the images to OpenCV Mat and then use the warpAffine method. I am having certain memory leaks with it and decided to see if using only dlib is possible. I noticed that the dlib library has some methods in geometry.h for this purpose.
For this, I am using find_affine_transform method to get a point_transform_affine object where I find the affine transformation between 3 points obtained using the shape and template. In the documentation, it is written that we can use this object to apply the transformation on vector of points. However, I have been unable to find some example for it.
Could you tell me how I can apply the transformation learnt ?
Secondly, I have an image loaded in array2d object. Is there a way to go from array2d to vector of points ?
Some initial code is listed down below for the find_affine_transform.
std::vector<dlib::vector<double,2>> TemplateLandmarks;
std::vector<dlib::vector<double,2>> ObtainedLandmarks;
// push_back the specific coordinates in the above vectors
array2d<bgr_pixel> img;
// read the image from a file path using load_image
// learning the best transformation map
point_transform_affine H = find_affine_transform ( ObtainedLandmarks , TemplateLandmarks );
This can be done using the extract_image_chips function in dlib. There is even an example of its use in the face landmarking example program to align faces.

Armadillo porting imagesc to save image bitmap from matrix

I have this matlab code to display image object after do super spectrogram (stft, couple plca...)
t = z2 *stft_options.hop/stft_options.sr;
f = stft_options.sr*[0:size(spec_t,1)-1]/stft_options.N/1000;
max_val = max(max(db(abs(spec_t))));
imagesc(t, f, db(abs(spec_t)),[max_val-60 max_val]);
And get this result:
I was porting to C++ successfully by using Armadillo lib and get the mat results:
mat f,t,spec_t;
The problem is that I don't have any idea for converting bitmap like imagesc in matlab.
I searched and found this answer, but seems it doesn't work in my case because:
I use a double matrix instead of integer matrix, which can't be mark as bitmap color
The imagesc method take 4 parameters, which has the bounds with vectors x and y
The imagesc method also support scale ( I actually don't know how it work)
Does anyone have any suggestion?
Update: Here is the result of save method in Armadillo. It doesn't look like spectrogram image above. Do I miss something?
spec_t.save("spec_t.png", pgm_binary);
Update 2: save spectrogram with db and abs
mat spec_t_mag = db(abs(spec_t)); // where db method: m = 10 * log10(m);
mag_spec_t.save("mag_spec_t.png", pgm_binary);
And the result:
Armadillo is a linear algebra package, AFAIK it does not provide graphics routines. If you use something like opencv for those then it is really simple.
See this link about opencv's imshow(), and this link on how to use it in a program.
Note that opencv (like most other libraries) uses row-major indexing (x,y) and Armadillo uses column-major (row,column) indexing, as explained here.
For scaling, it's safest to convert to unsigned char yourself. In Armadillo that would be something like:
arma::Mat<unsigned char> mat2=255*(mat-mat.min())/(mat.max()-mat.min());
The t and f variables are for setting the axes, they are not part of the bitmap.
For just writing an image you can use Armadillo. Here is a description on how to write portable grey map (PGM) and portable pixel map (PPM) images. PGM export is only possible for 2D matrices, PPM export only for 3D matrices, where the 3rd dimension (size 3) are the channels for red, green and blue.
The reason your matlab figure looks prettier is because it has a colour map: a mapping of every value 0..255 to a vector [R, G, B] specifying the relative intensity of red, green and blue. A photo has an RGB value at every point:
colormap(gray);
x=imread('onion.png');
imagesc(x);
size(x)
That's the 3rd dimension of the image.
Your matrix is a 2d image, so the most natural way to show it is as grey levels (as happened for your spectrum).
x=mean(x,3);
imagesc(x);
This means that the R, G and B intensities jointly increase with the values in mat. You can put a colour map of different R,G,B combinations in a variable and use that instead, i.e. y=colormap('hot');colormap(y);. The variable y shows the R,G,B combinations for the (rescaled) image values.
It's also possible to make your own colour map (in matlab you can specify 64 R, G, and B combinations with values between 0 and 1):
z[63:-1:0; 1:2:63 63:-2:0; 0:63]'/63
colormap(z);
Now for increasing image values, red intensities decrease (starting from the maximum level), green intensities quickly increase then decrease, and blue values increase from minuimum to maximum.
Because PPM appears (I don't know the format) not to support colour maps, you need to specify the R,G,B values in a 3D array. For a colour order similar to z you would neet to make a Cube<unsigned char> c(ysize, xsize, 3) and then for every pixel y, x in mat2, do:
c(y,x,0) = 255-mat2(y,x);
c(y,x,1) = 255-abs(255-2*mat2(y,x));
x(y,x,2) = mat2(y,x)
or something very similar.
You may use SigPack, a signal processing library on top of Armadillo. It has spectrogram support and you may save the plot to a lot of different formats (png, ps, eps, tex, pdf, svg, emf, gif). SigPack uses Gnuplot for the plotting.

How to implemet 1D convolution in opencv?

Is there any way to implement convolution of 1D signal in OpenCV?
As I can see there is only filter2D, but I'm looking for something like Matlab's convn.
For 1-D convolutions, you might want to look at np.convolve.
See here: https://docs.scipy.org/doc/numpy/reference/generated/numpy.convolve.html
Python OpenCV programs that need a 1-D convolution can use it readily.
You can always view a 1D vector as a 2D mat, and thus simply calling the opencv build-it functions resolves the problem.
Below is a snippet that I use to smooth an image histogram.
# Inputs:
# gray = a gray scale image
# smoothing_nbins = int, the width of 1D filter
# Outputs:
# hist_sm = the smoothed image histogram
hist = cv2.calcHist([gray],[0],None,[256],[0,256])
hist_sm = cv2.blur(hist, (1, smoothing_nbins))
As you can see, the only trick you need here is to set one filter dimension to 1.
As far as I know, if you use convolve2D with just the 1D matrix, it still works. But depending on your specific work, it may not.

Matlab griddata equivalent in C++

I am looking for a C++ equivalent to Matlab's griddata function, or any 2D global interpolation method.
I have a C++ code that uses Eigen 3. I will have an Eigen Vector that will contain x,y, and z values, and two Eigen matrices equivalent to those produced by Meshgrid in Matlab. I would like to interpolate the z values from the Vectors onto the grid points defined by the Meshgrid equivalents (which will extend past the outside of the original points a bit, so minor extrapolation is required).
I'm not too bothered by accuracy--it doesn't need to be perfect. However, I cannot accept NaN as a solution--the interpolation must be computed everywhere on the mesh regardless of data gaps. In other words, staying inside the convex hull is not an option.
I would prefer not to write an interpolation from scratch, but if someone wants to point me to pretty good (and explicit) recipe I'll give it a shot. It's not the most hateful thing to write (at least in an algorithmic sense), but I don't want to reinvent the wheel.
Effectively what I have is scattered terrain locations, and I wish to define a rectilinear mesh that nominally follows some distance beneath the topography for use later. Once I have the node points, I will be good.
My research so far:
The question asked here: MATLAB functions in C++ produced a close answer, but unfortunately the suggestion was not free (SciMath).
I have tried understanding the interpolation function used in Generic Mapping Tools, and was rewarded with a headache.
I briefly looked into the Grid Algorithms library (GrAL). If anyone has commentary I would appreciate it.
Eigen has an unsupported interpolation package, but it seems to just be for curves (not surfaces).
Edit: VTK has a matplotlib functionality. Presumably there must be an interpolation used somewhere in that for display purposes. Does anyone know if that's accessible and usable?
Thank you.
This is probably a little late, but hopefully it helps someone.
Method 1.) Octave: If you're coming from Matlab, one way is to embed the gnu Matlab clone Octave directly into the c++ program. I don't have much experience with it, but you can call the octave library functions directly from a cpp file.
See here, for instance. http://www.gnu.org/software/octave/doc/interpreter/Standalone-Programs.html#Standalone-Programs
griddata is included in octave's geometry package.
Method 2.) PCL: They way I do it is to use the point cloud library (http://www.pointclouds.org) and VoxelGrid. You can set x, and y bin sizes as you please, then set a really large z bin size, which gets you one z value for each x,y bin. The catch is that x,y, and z values are the centroid for the points averaged into the bin, not the bin centers (which is also why it works for this). So you need to massage the x,y values when you're done:
Ex:
//read in a list of comma separated values (x,y,z)
FILE * fp;
fp = fopen("points.xyz","r");
//store them in PCL's point cloud format
pcl::PointCloud<pcl::PointXYZ>::Ptr basic_cloud_ptr (new pcl::PointCloud<pcl::PointXYZ>);
int numpts=0;
double x,y,z;
while(fscanf(fp, "%lg, %lg, %lg", &x, &y, &z)!=EOF)
{
pcl::PointXYZ basic_point;
basic_point.x = x; basic_point.y = y; basic_point.z = z;
basic_cloud_ptr->points.push_back(basic_point);
}
fclose(fp);
basic_cloud_ptr->width = (int) basic_cloud_ptr->points.size ();
basic_cloud_ptr->height = 1;
// create object for result
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>());
// create filtering object and process
pcl::VoxelGrid<pcl::PointXYZ> sor;
sor.setInputCloud (basic_cloud_ptr);
//set the bin sizes here. (dx,dy,dz). for 2d results, make one of the bins larger
//than the data set span in that axis
sor.setLeafSize (0.1, 0.1, 1000);
sor.filter (*cloud_filtered);
So that cloud_filtered is now a point cloud that contains one point for each bin. Then I just make a 2-d matrix and go through the point cloud assigning points to their x,y bins if I want an image, etc. as would be produced by griddata. It works pretty well, and it's much faster than matlab's griddata for large datasets.

How to add matrices with alglib?

I already know how to multiply two matrices with alglib, using rmatrixgemm (see this question).
Is there a way to compute a linear combination of two matrices without using this function, setting B to the identity ? It wouldn't be very efficient.
Alglib provides tons of complex algorithms but I can't find such a basic function.
The manual is here.
I think rmatrixgencopy is ALGLIB's way of providing matrix addition when you set the alpha and beta input parameters both to 1.
rmatrixgencopy (C++)
rmatrixgencopy (C#)
you might be able just to use alglib.cmatrixgemm to do Addition.
This subroutine calculates C = alpha*op1(A)op2(B) +betaC where:
C is MxN general matrix
op1(A) is MxK matrix
op2(B) is KxN matrix
"op" may be identity transformation, transposition, conjugate transposition.
If you want to do C = A + C, you just need to set: B = Identity, alpha = 1, beta = 1, op = identity transformation.
Why don't you try using another library that was created for the purpose of matrix math such as MTL4?
http://www.simunova.com/en/node/24
Manual - http://www.simunova.com/node/148