Skip to content

Commit

Permalink
Refactor image method
Browse files Browse the repository at this point in the history
Issue #471 Refactor the method that draws a matrix as an image so that the method returns the image. This had side effect on the Oscillations2010 class.
  • Loading branch information
towsey committed Jun 12, 2021
1 parent 71a4c04 commit 108210d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
4 changes: 3 additions & 1 deletion src/AudioAnalysisTools/Ocillations/Oscillations2010.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace AudioAnalysisTools
using System.Collections.Generic;
using AudioAnalysisTools.DSP;
using AudioAnalysisTools.StandardSpectrograms;
using SixLabors.ImageSharp;
using TowseyLibrary;

public static class Oscillations2010
Expand Down Expand Up @@ -169,7 +170,8 @@ public static void Execute(SpectrogramStandard sonogram, int minHz, int maxHz,

//following two lines write bmp image of cos values for checking.
string fPath = @"C:\SensorNetworks\Output\cosines.bmp";
ImageTools.DrawMatrix(cosines, fPath, true);
var image = ImageTools.DrawMatrix(cosines, true);
image.Save(fPath);

foreach (AcousticEvent av in events)
{
Expand Down
38 changes: 18 additions & 20 deletions src/TowseyLibrary/ImageTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3409,38 +3409,36 @@ public static Image<Rgb24> DrawXaxisScale(Image<Rgb24> image, int scaleHeight, d
/// Draws matrix but automatically determines the scale to fit 1000x1000 pixel image.
/// </summary>
/// <param name="matrix">the data.</param>
public static void DrawMatrix(double[,] matrix, string pathName, bool doScale)
public static Image<Rgb24> DrawMatrix(double[,] matrix, bool doScale)
{
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);

int maxYpixels = rows;
int maxXpixels = cols;
int YpixelsPerCell = 1;
int XpixelsPerCell = 1;
int yPixelsPerCell = 1;
int xPixelsPerCell = 1;
if (doScale)
{
maxYpixels = 1000;
var maxYpixels = 1000;
maxXpixels = 2500;
YpixelsPerCell = maxYpixels / rows;
XpixelsPerCell = maxXpixels / cols;
if (YpixelsPerCell == 0)
yPixelsPerCell = maxYpixels / rows;
xPixelsPerCell = maxXpixels / cols;
if (yPixelsPerCell == 0)
{
YpixelsPerCell = 1;
yPixelsPerCell = 1;
}

if (XpixelsPerCell == 0)
if (xPixelsPerCell == 0)
{
XpixelsPerCell = 1;
xPixelsPerCell = 1;
}
}

int Ypixels = YpixelsPerCell * rows;
int Xpixels = XpixelsPerCell * cols;
int yPixels = yPixelsPerCell * rows;
int xPixels = xPixelsPerCell * cols;

Color[] grayScale = GrayScale();

var bmp = new Image<Rgb24>(Xpixels, Ypixels);
var bmp = new Image<Rgb24>(xPixels, yPixels);

double[,] norm = DataTools.normalise(matrix);
for (int r = 0; r < rows; r++)
Expand All @@ -3453,19 +3451,19 @@ public static void DrawMatrix(double[,] matrix, string pathName, bool doScale)
greyId = 0;
}

int xOffset = XpixelsPerCell * c;
int yOffset = YpixelsPerCell * r;
for (int x = 0; x < XpixelsPerCell; x++)
int xOffset = xPixelsPerCell * c;
int yOffset = yPixelsPerCell * r;
for (int x = 0; x < xPixelsPerCell; x++)
{
for (int y = 0; y < YpixelsPerCell; y++)
for (int y = 0; y < yPixelsPerCell; y++)
{
bmp[xOffset + x, yOffset + y] = grayScale[greyId];
}
}
}
}

bmp.Save(pathName);
return bmp;
}

public static void DrawMatrixInColour(double[,] matrix, string pathName, bool doScale)
Expand Down

0 comments on commit 108210d

Please sign in to comment.