Skip to content

Introduce order for pixel extraction #2602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
64 changes: 38 additions & 26 deletions src/Microsoft.ML.ImageAnalytics/ExtensionsCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,16 @@ public static ImageLoadingEstimator LoadImages(this TransformsCatalog catalog, s
=> new ImageLoadingEstimator(CatalogUtils.GetEnvironment(catalog), imageFolder, SimpleColumnInfo.ConvertToValueTuples(columnPairs));

/// <include file='doc.xml' path='doc/members/member[@name="ImagePixelExtractingEstimator"]/*' />
/// <param name="catalog"> The transform's catalog.</param>
/// <param name="outputColumnName"> Name of the column resulting from the transformation of <paramref name="inputColumnName"/>.</param>
/// <param name="inputColumnName"> Name of column to transform. If set to <see langword="null"/>, the value of the <paramref name="outputColumnName"/> will be used as source.</param>
/// <param name="colors"> Specifies which <see cref="ImagePixelExtractingEstimator.ColorBits"/> to extract from the image. The order of colors is: Alpha, Red, Green Blue.</param>
/// <param name="interleave">Wheather to interleave the pixels, meaning keep them in the `ARGB ARGB` order, or leave them separated in the planar form, where the colors are outputed one by one
/// alpha, red, green, blue for all the pixels of the image. </param>
/// <param name="scale">Scale color pixel value by this amount.</param>
/// <param name="offset">Offset color pixel value by this amount.</param>
/// <param name="asFloat">Output the array as float array. If false, output as byte array.</param>
/// <param name="catalog">The transform's catalog.</param>
/// <param name="outputColumnName">Name of the column resulting from the transformation of <paramref name="inputColumnName"/>.</param>
/// <param name="inputColumnName">Name of column to transform. If set to <see langword="null"/>, the value of the <paramref name="outputColumnName"/> will be used as source.</param>
/// <param name="colors">What colors to extract.</param>
/// <param name="order">In which order to extract colors from pixel.</param>
/// <param name="interleave">Whether to interleave the pixels colors, meaning keep them in the <paramref name="order"/> order, or leave them in the plannar form:
/// all the values for one color for all pixels, then all the values for another color and so on.</param>
/// <param name="offset">Offset pixel's color value by this amount. Applied to color value first.</param>
/// <param name="scale">Scale pixel's color value by this amount. Applied to color value second.</param>
/// <param name="asFloat">Output array as float array. If false, output as byte array and ignores <paramref name="offset"/> and <paramref name="scale"/>.</param>
/// <example>
/// <format type="text/markdown">
/// <![CDATA[
Expand All @@ -63,12 +64,13 @@ public static ImageLoadingEstimator LoadImages(this TransformsCatalog catalog, s
public static ImagePixelExtractingEstimator ExtractPixels(this TransformsCatalog catalog,
string outputColumnName,
string inputColumnName = null,
ImagePixelExtractingEstimator.ColorBits colors = ImagePixelExtractingEstimator.ColorBits.Rgb,
ImagePixelExtractingEstimator.ColorBits colors = ImagePixelExtractingEstimator.Defaults.Colors,
ImagePixelExtractingEstimator.ColorsOrder order = ImagePixelExtractingEstimator.Defaults.Order,
bool interleave = false,
float scale = ImagePixelExtractingTransformer.Defaults.Scale,
float offset = ImagePixelExtractingTransformer.Defaults.Offset,
bool asFloat = ImagePixelExtractingTransformer.Defaults.Convert)
=> new ImagePixelExtractingEstimator(CatalogUtils.GetEnvironment(catalog), outputColumnName, inputColumnName, colors, interleave, scale, offset, asFloat);
float offset = ImagePixelExtractingEstimator.Defaults.Offset,
float scale = ImagePixelExtractingEstimator.Defaults.Scale,
bool asFloat = ImagePixelExtractingEstimator.Defaults.Convert)
=> new ImagePixelExtractingEstimator(CatalogUtils.GetEnvironment(catalog), outputColumnName, inputColumnName, colors, order, interleave, offset, scale, asFloat);

/// <include file='doc.xml' path='doc/members/member[@name="ImagePixelExtractingEstimator"]/*' />
/// <param name="catalog">The transform's catalog.</param>
Expand All @@ -89,8 +91,8 @@ public static ImagePixelExtractingEstimator ExtractPixels(this TransformsCatalog
/// <seealso cref= "ImageLoadingEstimator" />
/// </remarks >
/// <param name="catalog">The transform's catalog.</param>
/// <param name="inputColumnName">Name of the input column.</param>
/// <param name="outputColumnName">Name of the resulting output column.</param>
/// <param name="outputColumnName">Name of the column resulting from the transformation of <paramref name="inputColumnName"/>.</param>
/// <param name="inputColumnName">Name of column to transform. If set to <see langword="null"/>, the value of the <paramref name="outputColumnName"/> will be used as source.</param>
/// <param name="imageWidth">The transformed image width.</param>
/// <param name="imageHeight">The transformed image height.</param>
/// <param name="resizing"> The type of image resizing as specified in <see cref="ImageResizingEstimator.ResizingKind"/>.</param>
Expand Down Expand Up @@ -150,16 +152,26 @@ public static VectorToImageConvertingEstimator ConvertToImage(this TransformsCat
/// <param name="width">The width of the output images.</param>
/// <param name="outputColumnName"> Name of the column resulting from the transformation of <paramref name="inputColumnName"/>.</param>
/// <param name="inputColumnName"> Name of column to transform. If set to <see langword="null"/>, the value of the <paramref name="outputColumnName"/> will be used as source.</param>
/// <param name="colors"> Specifies which <see cref="ImagePixelExtractingEstimator.ColorBits"/> are in the input pixel vectors. The order of colors is: Alpha, Red, Green Blue.</param>
/// <param name="interleave">Whether the pixels are interleaved, meaning whether they are in `ARGB ARGB` order, or separated in the planar form, where the colors are specified one by one
/// alpha, red, green, blue for all the pixels of the image. </param>
/// <param name="scale">The values are scaled by this value before being converted to pixels.</param>
/// <param name="offset">The offset is subtracted (before scaling) before converting the values to pixels.</param>
/// <param name="colors">Specifies which <see cref="ImagePixelExtractingEstimator.ColorBits"/> are in the input pixel vectors. The order of colors specified in <paramref name="order"/>.</param>
/// <param name="order">In which order extracted colors presented in array.</param>
/// <param name="interleave">Whether the pixels are interleaved, meaning whether they are in <paramref name="order"/> order, or separated in the planar form:
/// all the values for one color for all pixels, then all the values for another color and so on.</param>
/// <param name="scale">The values are scaled by this value before being converted to pixels. Applied to vector value first.</param>
/// <param name="offset">The offset is subtracted before converting the values to pixels. Applied to vector value second.</param>
/// <param name="defaultAlpha">Default value for alpha color, would be overriden if <paramref name="colors"/> contains <see cref="ImagePixelExtractingEstimator.ColorBits.Alpha"/>.</param>
Copy link
Contributor

@artidoro artidoro Feb 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think your code now has the opposite logic.

Would prefer to be consistent with the above extension here too. If you could also reorder them so that the first one applied comes first that would be great. #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean?


In reply to: 259550143 [](ancestors = 259550143)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you talking about scale offset or defaultAlpha?


In reply to: 259550340 [](ancestors = 259550340,259550143)

Copy link
Contributor

@artidoro artidoro Feb 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I think codeflow did not pick up the right location for this. I meant to comment on scale and offset.

  1. I think the arguments should be presented in the same order in which they are applied.
  2. I think the order and the comment should match the above extension for ImageLoader
  3. I think the logic that you have in your code here does not match the comment:
    pixel = Color.FromArgb(
    ex.Alpha ? (int)Math.Round(alpha * scale - offset) : 0,
    (int)Math.Round(red * scale - offset),
    (int)Math.Round(green * scale - offset),
    (int)Math.Round(blue * scale - offset));

    It seems that you are first scaling and the applying the offset. #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I can do this: P-pixel x -VectorValue
p= xscale1-offset1
x =p
scale2-offset2

this way I need to solve equation in order to pick up scale1 and offset1 if I know scale2 and offset2.
or I can have

x=(p-offset1)scale1
p = x
scale2 - offset2
and relationship between offset1 = -offset2 and scale1 = 1/scale2

So in one transform i apply scale first, in another i apply offset.
I can make them same order in their parameters, but since I'm specifying which applied first and which second, it looks weird then you put one which applied second to first in argument list.


In reply to: 259551787 [](ancestors = 259551787)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for explaining the logic it makes sense like this!


In reply to: 259553517 [](ancestors = 259553517,259551787)

/// <param name="defaultRed">Default value for red color, would be overriden if <paramref name="colors"/> contains <see cref="ImagePixelExtractingEstimator.ColorBits.Red"/>.</param>
/// <param name="defaultGreen">Default value for grenn color, would be overriden if <paramref name="colors"/> contains <see cref="ImagePixelExtractingEstimator.ColorBits.Green"/>.</param>
/// <param name="defaultBlue">Default value for blue color, would be overriden if <paramref name="colors"/> contains <see cref="ImagePixelExtractingEstimator.ColorBits.Blue"/>.</param>
public static VectorToImageConvertingEstimator ConvertToImage(this TransformsCatalog catalog, int height, int width, string outputColumnName, string inputColumnName = null,
ImagePixelExtractingEstimator.ColorBits colors = VectorToImageConvertingTransformer.Defaults.Colors,
bool interleave = VectorToImageConvertingTransformer.Defaults.InterleaveArgb,
float scale = VectorToImageConvertingTransformer.Defaults.Scale,
float offset = VectorToImageConvertingTransformer.Defaults.Offset)
=> new VectorToImageConvertingEstimator(CatalogUtils.GetEnvironment(catalog), height, width, outputColumnName, inputColumnName, colors, interleave, scale, offset);
ImagePixelExtractingEstimator.ColorBits colors = ImagePixelExtractingEstimator.Defaults.Colors,
ImagePixelExtractingEstimator.ColorsOrder order = ImagePixelExtractingEstimator.Defaults.Order,
bool interleave = ImagePixelExtractingEstimator.Defaults.Interleave,
float scale = VectorToImageConvertingEstimator.Defaults.Scale,
float offset = VectorToImageConvertingEstimator.Defaults.Offset,
int defaultAlpha = VectorToImageConvertingEstimator.Defaults.DefaultAlpha,
int defaultRed = VectorToImageConvertingEstimator.Defaults.DefaultRed,
int defaultGreen = VectorToImageConvertingEstimator.Defaults.DefaultGreen,
int defaultBlue = VectorToImageConvertingEstimator.Defaults.DefaultBlue)
=> new VectorToImageConvertingEstimator(CatalogUtils.GetEnvironment(catalog), height, width, outputColumnName, inputColumnName, colors, order, interleave, scale, offset);
}
}
Loading