Skip to content

Commit 284e02c

Browse files
yaeldMSIvanidzo4ka
authored andcommitted
ImagePixelExtractorTransform support for images with no alpha channel (#1821)
1 parent 33c1884 commit 284e02c

File tree

9 files changed

+15
-34
lines changed

9 files changed

+15
-34
lines changed

src/Microsoft.ML.ImageAnalytics/ImagePixelExtractorTransform.cs

+3-23
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,9 @@ private ValueGetter<VBuffer<TValue>> GetGetterCore<TValue>(Row input, int iinfo,
483483
return;
484484
}
485485

486-
Host.Check(src.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb);
486+
Host.Check(src.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb
487+
|| src.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb,
488+
"Transform only supports pixel formats Format24bppRgb and Format32bppArgb");
487489
Host.Check(src.Height == height && src.Width == width);
488490

489491
var editor = VBufferEditor.Create(ref dst, size);
@@ -542,28 +544,6 @@ private ValueGetter<VBuffer<TValue>> GetGetterCore<TValue>(Row input, int iinfo,
542544
else
543545
{
544546
int idstMin = 0;
545-
if (ex.Alpha)
546-
{
547-
// The image only has rgb but we need to supply alpha as well, so fake it up,
548-
// assuming that it is 0xFF.
549-
if (!vf.IsEmpty)
550-
{
551-
Single v = (0xFF - offset) * scale;
552-
for (int i = 0; i < cpix; i++)
553-
vf[i] = v;
554-
}
555-
else
556-
{
557-
for (int i = 0; i < cpix; i++)
558-
vb[i] = 0xFF;
559-
}
560-
idstMin = cpix;
561-
562-
// We've preprocessed alpha, avoid it in the
563-
// scan operation below.
564-
a = false;
565-
}
566-
567547
for (int y = 0; y < h; ++y)
568548
{
569549
int idstBase = idstMin + y * w;

src/Microsoft.ML.ImageAnalytics/ImageResizerTransform.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ protected override Delegate MakeGetter(Row input, int iinfo, Func<int, bool> act
411411
destWidth = (int)(sourceWidth * aspect);
412412
destHeight = (int)(sourceHeight * aspect);
413413
}
414-
dst = new Bitmap(info.Width, info.Height);
414+
dst = new Bitmap(info.Width, info.Height, src.PixelFormat);
415415
var srcRectangle = new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight);
416416
var destRectangle = new Rectangle(destX, destY, destWidth, destHeight);
417417
using (var g = Graphics.FromImage(dst))

test/Microsoft.ML.OnnxTransformTest/DnnImageFeaturizerTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public void OnnxStatic()
134134
Assert.Equal(512, buffer.Length);
135135
numRows += 1;
136136
}
137-
Assert.Equal(3, numRows);
137+
Assert.Equal(4, numRows);
138138
}
139139
}
140140

test/Microsoft.ML.OnnxTransformTest/OnnxTransformTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public void OnnxStatic()
210210
Assert.Equal(1000, buffer.Length);
211211
numRows += 1;
212212
}
213-
Assert.Equal(3, numRows);
213+
Assert.Equal(4, numRows);
214214
}
215215
}
216216

test/Microsoft.ML.Tests/Scenarios/TensorflowTests.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,17 @@ public void TensorFlowTransforCifarEndToEndTest()
5656
{
5757
ImagePath = GetDataPath("images/banana.jpg")
5858
});
59-
Assert.Equal(1, prediction.PredictedScores[0], 2);
60-
Assert.Equal(0, prediction.PredictedScores[1], 2);
59+
Assert.Equal(0, prediction.PredictedScores[0], 2);
60+
Assert.Equal(1, prediction.PredictedScores[1], 2);
6161
Assert.Equal(0, prediction.PredictedScores[2], 2);
6262

6363
prediction = predictFunction.Predict(new CifarData()
6464
{
6565
ImagePath = GetDataPath("images/hotdog.jpg")
6666
});
6767
Assert.Equal(0, prediction.PredictedScores[0], 2);
68-
Assert.Equal(1, prediction.PredictedScores[1], 2);
69-
Assert.Equal(0, prediction.PredictedScores[2], 2);
68+
Assert.Equal(0, prediction.PredictedScores[1], 2);
69+
Assert.Equal(1, prediction.PredictedScores[2], 2);
7070
}
7171
}
7272

test/Microsoft.ML.Tests/ScenariosWithDirectInstantiation/TensorflowTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ public void TensorFlowTransformCifar()
654654
Assert.Equal(10, buffer.Length);
655655
numRows += 1;
656656
}
657-
Assert.Equal(3, numRows);
657+
Assert.Equal(4, numRows);
658658
}
659659
}
660660

@@ -718,7 +718,7 @@ public void TensorFlowTransformCifarSavedModel()
718718
Assert.Equal(10, buffer.Length);
719719
numRows += 1;
720720
}
721-
Assert.Equal(3, numRows);
721+
Assert.Equal(4, numRows);
722722
}
723723
}
724724

test/Microsoft.ML.Tests/TensorFlowEstimatorTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public void TestTensorFlowStatic()
175175
Assert.Equal(10, buffer.Length);
176176
numRows += 1;
177177
}
178-
Assert.Equal(3, numRows);
178+
Assert.Equal(4, numRows);
179179
}
180180
}
181181

@@ -222,7 +222,7 @@ public void TestTensorFlowStaticWithSchema()
222222
Assert.Equal(10, buffer.Length);
223223
numRows += 1;
224224
}
225-
Assert.Equal(3, numRows);
225+
Assert.Equal(4, numRows);
226226
}
227227
}
228228

test/data/images/images.tsv

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
tomato.bmp tomato
12
banana.jpg banana
23
hotdog.jpg hotdog
34
tomato.jpg tomato

test/data/images/tomato.bmp

1.22 MB
Binary file not shown.

0 commit comments

Comments
 (0)