Skip to content

Commit 2c0bb21

Browse files
yaeldMSGalOshri
authored andcommitted
Fix bug in pixel extractor transform, and add more unit tests. (#900)
1 parent 4e0800c commit 2c0bb21

File tree

3 files changed

+589
-16
lines changed

3 files changed

+589
-16
lines changed

src/Microsoft.ML.ImageAnalytics/ImagePixelExtractorTransform.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -505,30 +505,30 @@ private ValueGetter<VBuffer<TValue>> GetGetterCore<TValue>(IRow input, int iinfo
505505
if (ex.Interleave)
506506
{
507507
int idst = 0;
508-
for (int y = 0; y < h; ++y)
509-
for (int x = 0; x < w; x++)
508+
for (int x = 0; x < w; x++)
509+
for (int y = 0; y < h; ++y)
510510
{
511-
var pb = src.GetPixel(y, x);
511+
var pb = src.GetPixel(x, y);
512512
if (vb != null)
513513
{
514-
if (a) { vb[idst++] = (byte)0; }
514+
if (a) { vb[idst++] = pb.A; }
515515
if (r) { vb[idst++] = pb.R; }
516516
if (g) { vb[idst++] = pb.G; }
517517
if (b) { vb[idst++] = pb.B; }
518518
}
519519
else if (!needScale)
520520
{
521-
if (a) { vf[idst++] = 0.0f; }
521+
if (a) { vf[idst++] = pb.A; }
522522
if (r) { vf[idst++] = pb.R; }
523523
if (g) { vf[idst++] = pb.G; }
524524
if (b) { vf[idst++] = pb.B; }
525525
}
526526
else
527527
{
528-
if (a) { vf[idst++] = 0.0f; }
528+
if (a) { vf[idst++] = (pb.A - offset) * scale; }
529529
if (r) { vf[idst++] = (pb.R - offset) * scale; }
530-
if (g) { vf[idst++] = (pb.B - offset) * scale; }
531-
if (b) { vf[idst++] = (pb.G - offset) * scale; }
530+
if (g) { vf[idst++] = (pb.G - offset) * scale; }
531+
if (b) { vf[idst++] = (pb.B - offset) * scale; }
532532
}
533533
}
534534
Contracts.Assert(idst == size);

src/Microsoft.ML.ImageAnalytics/VectorToImageTransform.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ private ValueGetter<Bitmap> GetterFromType<TValue>(IRow input, int iinfo, ColInf
386386
float alpha = 0;
387387
if (ex.Interleave)
388388
{
389-
if (ex.Alpha) position++;
389+
if (ex.Alpha) alpha = Convert.ToSingle(values[position++]);
390390
if (ex.Red) red = Convert.ToSingle(values[position++]);
391391
if (ex.Green) green = Convert.ToSingle(values[position++]);
392392
if (ex.Blue) blue = Convert.ToSingle(values[position++]);
@@ -405,10 +405,10 @@ private ValueGetter<Bitmap> GetterFromType<TValue>(IRow input, int iinfo, ColInf
405405
else
406406
{
407407
pixel = Color.FromArgb(
408-
(int)((alpha - offset) * scale),
409-
(int)((red - offset) * scale),
410-
(int)((green - offset) * scale),
411-
(int)((blue - offset) * scale));
408+
ex.Alpha ? (int)Math.Round((alpha - offset) * scale) : 0,
409+
(int)Math.Round((red - offset) * scale),
410+
(int)Math.Round((green - offset) * scale),
411+
(int)Math.Round((blue - offset) * scale));
412412
}
413413
dst.SetPixel(x, y, pixel);
414414
}

0 commit comments

Comments
 (0)