/// <summary> /// Creates a heat map model from image file. Supported formats: JPEG, PNG, TIFF etc. See: <see cref="Image.FromFile(string, bool)"/> /// </summary> /// <param name="imageFile">Path of the image file.</param> /// <param name="widthOfMap">The width of map - number of horizontal fields, vertical are computed in respect to image dimensional proportion </param> /// <param name="loger">The loger.</param> /// <returns>Heat map model, fields have mean brightness value</returns> public static HeatMapModel CreateFromImage(String imageFile, Int32 widthOfMap = 1000, ILogBuilder loger = null) { Bitmap grayImage = null; var imgFile = imageFile.getWritableFile(Data.enums.getWritableFileMode.existing, loger); if (!imgFile.Exists) { loger.log("Loading image file failed (" + imageFile + ") for heatmap model mapping"); } else { try { Bitmap sbmp = new Bitmap(Image.FromFile(imgFile.FullName)); grayImage = sbmp; // grayImage = Grayscale.CommonAlgorithms.Y.Apply(sbmp); } catch (Exception ex) { loger.log("Error in loading file (" + imageFile + "): " + ex.Message); } } Int32 heightOfMap = Convert.ToInt32(grayImage.Size.Height.GetRatio(grayImage.Size.Width) * widthOfMap); HeatMapModel map = HeatMapModel.Create(widthOfMap, heightOfMap, "D3"); map.properties.addAndDescribeKey("grayImage", grayImage, false, false); map.properties.addAndDescribeKey("source", imageFile, false, false); map.AllocateSize(widthOfMap, heightOfMap); Int32 bs = Convert.ToInt32(grayImage.Size.Width / widthOfMap); map.properties.addAndDescribeKey("fieldSize", bs, false, false); for (Int32 y = 0; y < heightOfMap; y++) { for (Int32 x = 0; x < widthOfMap; x++) { map[x, y] = grayImage.GetIntesity(x, y, bs); } } return(map); }
/// <summary> /// Renders HeatMapModel of specified size /// </summary> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <returns></returns> public HeatMapModel MakeHeatMap(Int32 width, Int32 height, Int32 xPeriod = 20, Int32 yPeriod = 20) { xAxisFunction.outputRange = new imbNumberScale(numberRangePresetEnum.zeroToOne); yAxisFunction.outputRange = new imbNumberScale(numberRangePresetEnum.zeroToOne); HeatMapModel map = HeatMapModel.Create(width, height, "D3"); map.AllocateSize(width, height); for (Int32 y = 0; y < height; y++) { Double yValue = yAxisFunction.GetOutput(y.GetRatio(yPeriod)); for (Int32 x = 0; x < width; x++) { map[x, y] = xAxisFunction.GetOutput(x.GetRatio(xPeriod)) + yValue; //grayImage.GetIntesity(x, y, bs); } } return(map); }
/// <summary> /// Creates randomized heat map, with specified x-y axis size. /// </summary> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="min">The minimum.</param> /// <param name="max">The maximum.</param> /// <param name="axisFormat">The axis format.</param> /// <returns></returns> public static HeatMapModel CreateRandom(Int32 width, Int32 height, Int32 min, Int32 max, String axisFormat = "D2") { List <String> xKeys = new List <string>(); List <String> yKeys = new List <string>(); for (int i = 0; i < width; i++) { xKeys.Add(i.ToString(axisFormat)); } for (int i = 0; i < height; i++) { yKeys.Add(i.ToString(axisFormat)); } HeatMapModel model = new HeatMapModel(); model.Deploy(xKeys, yKeys); model.Randomize(min, max); return(model); }