cleanup
This commit is contained in:
parent
a00b0c1d41
commit
e2702c00ee
4 changed files with 8 additions and 117 deletions
|
|
@ -1,79 +1,9 @@
|
|||
using System.Collections.Concurrent;
|
||||
using OpenCvSharp;
|
||||
using OpenCvSharp;
|
||||
|
||||
namespace WebApp;
|
||||
|
||||
public class ImageGenerator
|
||||
{
|
||||
private const string PATH = "D:/tiles1705/";
|
||||
|
||||
public byte[] GenerateImage(RequestBody requestBody)
|
||||
{
|
||||
var start = DateTime.Now;
|
||||
|
||||
string[] inputs = requestBody.CanvasRect.Split(":");
|
||||
Coordinate coord0 = new Coordinate(inputs[0]);
|
||||
Coordinate coord1 = new Coordinate(inputs[1]);
|
||||
(var matrix, _, _) = GenerateDict(coord0, coord1);
|
||||
|
||||
ConcurrentDictionary<int, Mat> rows = new();
|
||||
Parallel.ForEach(matrix, pair =>
|
||||
{
|
||||
Mat row = new Mat();
|
||||
List<Mat> mats = new();
|
||||
foreach (var coord in pair.Value)
|
||||
{
|
||||
string fileName = PATH + coord.Name + ".png";
|
||||
mats.Add(new Mat( fileName ) );
|
||||
}
|
||||
|
||||
Cv2.HConcat(mats, row);
|
||||
rows[pair.Key] = row;
|
||||
});
|
||||
|
||||
Mat output = new Mat();
|
||||
Cv2.VConcat(rows.Values, output);
|
||||
|
||||
var result = output.ImEncode();
|
||||
var end = DateTime.Now;
|
||||
var elapsed = end - start;
|
||||
Console.WriteLine($"Elapsed: {elapsed.TotalMilliseconds} ms");
|
||||
return result;
|
||||
}
|
||||
|
||||
public byte[] GenerateImage2(RequestBody requestBody)
|
||||
{
|
||||
var start = DateTime.Now;
|
||||
Coordinate a1 = new Coordinate("A1");
|
||||
Mat a1Mat = new Mat(a1.Path);
|
||||
|
||||
string[] inputs = requestBody.CanvasRect.Split(":");
|
||||
Coordinate coord0 = new Coordinate(inputs[0]);
|
||||
Coordinate coord1 = new Coordinate(inputs[1]);
|
||||
|
||||
double scale = requestBody.OutputScale;
|
||||
|
||||
(var matrix, int rowCount, int colCount) = GenerateMatrix(coord0, coord1);
|
||||
Mat temp = GenerateMatCanvas(a1Mat, rowCount, colCount, new Rect());
|
||||
|
||||
Parallel.ForEach(matrix, item =>
|
||||
{
|
||||
Mat mat = new Mat(item.Path);
|
||||
Rect rect = new Rect((item.Col - 1) * a1Mat.Cols, (item.Row - 1) * a1Mat.Rows, a1Mat.Cols, a1Mat.Rows);
|
||||
mat.CopyTo(temp[rect]);
|
||||
});
|
||||
|
||||
double newWidth = temp.Width * scale;
|
||||
double newHeight = temp.Height * scale;
|
||||
|
||||
var resized = temp.Resize(new Size(newWidth, newHeight));
|
||||
var result = resized.ImEncode();
|
||||
var end = DateTime.Now;
|
||||
var elapsed = end - start;
|
||||
Console.WriteLine($"Elapsed: {elapsed.TotalMilliseconds} ms");
|
||||
return result;
|
||||
}
|
||||
|
||||
public byte[] GenerateImage3(RequestBody requestBody)
|
||||
{
|
||||
var start = DateTime.Now;
|
||||
|
|
@ -136,7 +66,7 @@ public class ImageGenerator
|
|||
return result;
|
||||
}
|
||||
|
||||
internal (List<Coordinate> Matrix, int RowCount, int ColCount) GenerateMatrix(Coordinate coordinate1, Coordinate coordinate2)
|
||||
private (List<Coordinate> Matrix, int RowCount, int ColCount) GenerateMatrix(Coordinate coordinate1, Coordinate coordinate2)
|
||||
{
|
||||
int minRow = Math.Min(coordinate1.Row, coordinate2.Row);
|
||||
int maxRow = Math.Max(coordinate1.Row, coordinate2.Row);
|
||||
|
|
@ -159,38 +89,6 @@ public class ImageGenerator
|
|||
return (results, rowCount, colCount);
|
||||
}
|
||||
|
||||
internal (ConcurrentDictionary<int,List<Coordinate>> Matrix, int RowCount, int ColCount) GenerateDict(Coordinate coordinate1, Coordinate coordinate2)
|
||||
{
|
||||
ConcurrentDictionary<int,List<Coordinate>> results = new();
|
||||
int minRow = Math.Min(coordinate1.Row, coordinate2.Row);
|
||||
int maxRow = Math.Max(coordinate1.Row, coordinate2.Row);
|
||||
int minCol = Math.Min(coordinate1.Col, coordinate2.Col);
|
||||
int maxCol = Math.Max(coordinate1.Col, coordinate2.Col);
|
||||
|
||||
int rowCount = maxRow - minRow + 1;
|
||||
int colCount = maxCol - minCol + 1;
|
||||
|
||||
for (int i = 0; i < rowCount; i++)
|
||||
{
|
||||
List<Coordinate> result = new();
|
||||
for (int j = 0; j < colCount; j++)
|
||||
{
|
||||
int row = minRow + i;
|
||||
int col = minCol + j;
|
||||
result.Add(new Coordinate(row, col));
|
||||
}
|
||||
results[i] = result;
|
||||
}
|
||||
|
||||
return (results, rowCount, colCount);
|
||||
}
|
||||
|
||||
internal Mat GenerateMatCanvas(Mat reference, int rowCount, int colCount, Rect canvas)
|
||||
{
|
||||
return new Mat(canvas.Width, canvas.Height, reference.Type());
|
||||
// return new Mat(reference.Rows * rowCount - offset.X, reference.Cols * colCount - offset.Y, reference.Type());
|
||||
}
|
||||
|
||||
private Rect GenerateInitialRectCanvas(Mat reference, int rowCount, int colCount)
|
||||
{
|
||||
int width = reference.Width * colCount;
|
||||
|
|
@ -198,7 +96,7 @@ public class ImageGenerator
|
|||
return new Rect(0, 0, width, height);
|
||||
}
|
||||
|
||||
internal Rect GenerateGlobalRoI(double[] cropOffset, double[] cropSize, int width, int height)
|
||||
private Rect GenerateGlobalRoI(float[] cropOffset, float[] cropSize, int width, int height)
|
||||
{
|
||||
int x = (int)(cropOffset[0] * width);
|
||||
int y = (int)(cropOffset[1] * height);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue