Armadillo port imagesc to save bitmap image from matrix

I have this matlab code to display an image object after a 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:

enter image description here

I ported to C ++ successfully using Armadillo lib and getting match results:

 mat f,t,spec_t; 

The problem is that I have no idea to convert a bitmap like imagesc to matlab.

I searched and found this answer , but it seems that this does not work in my case, because:

  • I use a double matrix instead of an integer matrix that cannot be marked as the color of the bitmap
  • The imagesc method takes 4 parameters that have borders with the vectors x and y
  • The imagesc method also supports scale (I really don't know how it works)


Does anyone have any suggestions?

Update . Here is the result of the save method in Armadillo. This is not like the spectrogram image above. Did I miss something?

 spec_t.save("spec_t.png", pgm_binary); 

enter image description here

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:

enter image description here

+7
c ++ image image-processing matlab armadillo
source share
2 answers

Armadillo is a linear algebra package; AFAIK does not provide graphical routines. If you use something like opencv for those, then it is really simple.

See this link for opencv imshow() and this link for how to use it in a program.

Note that opencv (like most other libraries) uses row indexing (x, y), and Armadillo uses column indexing (row, column), as described here .

For scaling it is most safe to convert to unsigned char yourself. In Armadillo, it will be something like:

 `arma::Mat<unsigned char> mat2=255*(mat-mat.min())/(mat.max()-mat.min());` 

Variables t and f are designed to set the axes; they are not part of the bitmap image.

You can use Armadillo for easy image writing. Here's a description of how to write handheld gray card (PGM) images and portable pixel cards (PPM). PGM export is only possible for 2D matrices, PPM export is only for 3D matrices, where the 3rd dimension (size 3) is the channels for red, green and blue.

The reason your Matlab shape looks prettier is because it has a color map: mapping each 0. 0.55 value to a vector [R, G, B], which determines the relative intensities of red, green, and blue. The photo has an RGB value at each point:

 colormap(gray); x=imread('onion.png'); imagesc(x); size(x) 

This is the 3rd dimension of the image.

Your matrix is ​​a 2d image, so the most natural way to show it is with a gray level (as happened for your spectrum).

 x=mean(x,3); imagesc(x); 

This means that the intensities of R, G, and B increase together with the values ​​in mat . You can put a color map of various combinations of R, G, B into a variable and use this instead, i.e. y=colormap('hot');colormap(y); . The y variable shows combinations of R, G, B for (scaled) image values.

You can also create your own color map (in Matlab you can specify 64 combinations of R, G and B with values ​​from 0 to 1):

 z[63:-1:0; 1:2:63 63:-2:0; 0:63]'/63 colormap(z); 

Now, to increase the image values, the red intensities decrease (starting from the maximum level), the green intensity increases rapidly, and then decreases, and the blue values ​​increase from minimum to maximum.

Since PPM appears (I don’t know the format) so as not to support color maps, you need to specify the values ​​of R, G, B in a three-dimensional array. For a color order similar to z , you should make Cube<unsigned char> c(ysize, xsize, 3) , and then for each 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.

+4
source share

You can use SigPack , a signal processing library on top of Armadillo. It supports a spectrogram, and you can save the plot in many different formats (png, ps, eps, tex, pdf, svg, emf, gif). SigPack uses Gnuplot to chart.

+2
source share

All Articles