BioGTK  6.7.0
A .NET library & program for annotating, editing various microscopy imaging formats using Bioformats supported images. Including whole slide, pyramidal, and series.
Loading...
Searching...
No Matches
Transforms.cs
1using System;
2using System.Collections.Generic;
3using AForge;
4using System.Linq;
5using System.Text;
6using System.Threading.Tasks;
7using System.Windows;
8using MathNet.Numerics.Statistics;
9using Gdk;
10
11namespace BioGTK
12{
13
15 {
16 public Transforms(int target_length)
17 {
18 this.mTargetLength = target_length;
19 }
20
29 public float[] ApplyImage(Bitmap b)
30 {
31 int neww = 0;
32 int newh = 0;
33 if(SAM.SAM2 && MicroSAM.theSingleton == null)
34 {
35 neww = 1024;
36 newh = 1024;
37 }
38 else
39 this.GetPreprocessShape(b.SizeX, b.SizeY, this.mTargetLength, ref neww, ref newh);
40
41 float[,,] resizeImg = this.Resize(b,neww,newh);
42
43 float[] means = new float[resizeImg.GetLength(0)];
44 for (int i = 0; i < resizeImg.GetLength(0); i++)
45 {
46 float[] data = new float[resizeImg.GetLength(1) * resizeImg.GetLength(2)];
47 for (int j = 0; j < resizeImg.GetLength(1); j++)
48 {
49 for (int k = 0; k < resizeImg.GetLength(2); k++)
50 {
51 data[j * resizeImg.GetLength(2) + k] = resizeImg[i, j, k];
52 }
53 }
54 means[i] = (float)MathNet.Numerics.Statistics.Statistics.Mean(data);
55 }
56
57 float[] stdDev = new float[resizeImg.GetLength(0)];
58 for (int i = 0; i < resizeImg.GetLength(0); i++)
59 {
60 float[] data = new float[resizeImg.GetLength(1) * resizeImg.GetLength(2)];
61 for (int j = 0; j < resizeImg.GetLength(1); j++)
62 {
63 for (int k = 0; k < resizeImg.GetLength(2); k++)
64 {
65 data[j * resizeImg.GetLength(2) + k] = resizeImg[i, j, k];
66 }
67 }
68 stdDev[i] = (float)MathNet.Numerics.Statistics.Statistics.StandardDeviation(data);
69 }
70
71
72 float[] transformedImg = new float[3 * this.mTargetLength * this.mTargetLength];
73 for (int i = 0; i < neww; i++)
74 {
75 for (int j = 0; j < newh; j++)
76 {
77 int index = j * this.mTargetLength + i;
78 transformedImg[index] = (resizeImg[0, i, j] - means[0]) / stdDev[0];
79 transformedImg[this.mTargetLength * this.mTargetLength + index] = (resizeImg[1, i, j] - means[1]) / stdDev[1];
80 transformedImg[2 * this.mTargetLength * this.mTargetLength + index] = (resizeImg[2, i, j] - means[2]) / stdDev[2];
81 }
82 }
83
84 return transformedImg;
85 }
95 float[,,] Resize(Bitmap originalImage, int w,int h)
96 {
97 AForge.Imaging.Filters.ResizeBilinear res = new AForge.Imaging.Filters.ResizeBilinear(w, h);
98 Bitmap resizedImage = res.Apply(originalImage.GetImageRGBA());
99 //resizedImage.Save("resized.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
100 float[,,] newimg = new float[3, w, h];
101 for (int i = 0; i < w; i++)
102 {
103 for (int j = 0; j < h; j++)
104 {
105 newimg[0, i, j] = resizedImage.GetPixel(i, j).Rf * 255;
106 newimg[1, i, j] = resizedImage.GetPixel(i, j).Gf * 255;
107 newimg[2, i, j] = resizedImage.GetPixel(i, j).Bf * 255;
108 }
109 }
110 //originalImage.Dispose();
111 resizedImage.Dispose();
112 return newimg;
113 }
114
126 public PointPromotion ApplyCoords(PointPromotion org_point, int orgw, int orgh)
127 {
128 int neww = 0;
129 int newh = 0;
130 this.GetPreprocessShape(orgw, orgh, this.mTargetLength, ref neww, ref newh);
131 PointPromotion newpointp = new PointPromotion(org_point.foreGround);
132 float scalx = (float)neww / (float)orgw;
133 float scaly = (float)newh / (float)orgh;
134 newpointp.X = (int)(org_point.X * scalx);
135 newpointp.Y = (int)(org_point.Y * scaly);
136
137 return newpointp;
138 }
148 public BoxPromotion ApplyBox(BoxPromotion org_box, int orgw, int orgh)
149 {
150 BoxPromotion box = new BoxPromotion();
151
152 PointPromotion left = this.ApplyCoords((org_box as BoxPromotion).mLeftUp, orgw, orgh);
153 PointPromotion lefrightt = this.ApplyCoords((org_box as BoxPromotion).mRightBottom, orgw, orgh);
154
155 box.mLeftUp = left;
156 box.mRightBottom = lefrightt;
157 return box;
158 }
159
168 void GetPreprocessShape(int oldw, int oldh, int long_side_length, ref int neww, ref int newh)
169 {
170 float scale = long_side_length * 1.0f / Math.Max(oldh, oldw);
171 float newht = oldh * scale;
172 float newwt = oldw * scale;
173 //The micro-sam model has a width and height of 1024 we take this into account.
174 if (MicroSAM.theSingleton == null)
175 {
176 neww = (int)(newwt);//+0.5
177 newh = (int)(newht);//+0.5
178 }
179 else
180 {
181 neww = 1024;
182 newh = 1024;
183 }
184 }
185 int mTargetLength;
186 }
187}
float[] ApplyImage(Bitmap b)
Definition Transforms.cs:29
BoxPromotion ApplyBox(BoxPromotion org_box, int orgw, int orgh)
PointPromotion ApplyCoords(PointPromotion org_point, int orgw, int orgh)
Definition ML.cs:17