/// <summary> /// The ApplyDistort method applies the distortion policy to the simple datum. /// </summary> /// <param name="sd">Specifies the SimpleDatum to distort.</param> /// <param name="p">Specifies the distortion parameters that define the distortion policy.</param> /// <returns>The distorted SimpleDatum is returned.</returns> public SimpleDatum ApplyDistort(SimpleDatum sd, DistortionParameter p) { double dfProb = m_random.NextDouble(); Bitmap bmp = ImageData.GetImage(sd); if (dfProb > 0.5) { bmp = randomBrightness(bmp, p.brightness_prob, p.brightness_delta); bmp = randomContrast(bmp, p.contrast_prob, p.contrast_lower, p.contrast_upper); bmp = randomSaturation(bmp, p.saturation_prob, p.saturation_lower, p.saturation_upper); } else { bmp = randomBrightness(bmp, p.brightness_prob, p.brightness_delta); bmp = randomSaturation(bmp, p.saturation_prob, p.saturation_lower, p.saturation_upper); bmp = randomContrast(bmp, p.contrast_prob, p.contrast_lower, p.contrast_upper); } SimpleDatum sd1 = ImageData.GetImageData(bmp, sd.Channels, sd.IsRealData, sd.Label); if (sd1.IsRealData) { sd.SetData(sd1.RealData.ToList(), sd.Label); } else { sd.SetData(sd1.ByteData.ToList(), sd.Label); } return(randomChannelOrder(sd, p.random_order_prob)); }
/// <summary> /// Crop the SimpleDatum according to the bbox. /// </summary> /// <param name="d">Specifies the SimpleDatum to crop.</param> /// <param name="bbox">Specifies the bounding box.</param> /// <returns>The cropped SimpleDatum is returned.</returns> public SimpleDatum CropImage(SimpleDatum d, NormalizedBBox bbox) { int nDatumChannels = d.Channels; int nDatumHeight = d.Height; int nDatumWidth = d.Width; // Get the bbox dimension. NormalizedBBox clipped_bbox = m_bbox.Clip(bbox); NormalizedBBox scaled_bbox = m_bbox.Scale(bbox, nDatumHeight, nDatumWidth); int w_off = (int)scaled_bbox.xmin; int h_off = (int)scaled_bbox.ymin; int width = (int)(scaled_bbox.xmax - scaled_bbox.xmin); int height = (int)(scaled_bbox.ymax - scaled_bbox.ymin); // Crop the image using bbox. SimpleDatum crop_datum = new SimpleDatum(d, height, width); int nCropDatumSize = nDatumChannels * height * width; if (d.IsRealData) { double[] rgData = new double[nCropDatumSize]; for (int h = h_off; h < h_off + height; h++) { for (int w = w_off; w < w_off + width; w++) { for (int c = 0; c < nDatumChannels; c++) { int nDatumIdx = (c * nDatumHeight + h) * nDatumWidth + w; int nCropDatumIdx = (c * height + h - h_off) * width + w - w_off; rgData[nCropDatumIdx] = d.RealData[nDatumIdx]; } } } crop_datum.SetData(rgData.ToList(), d.Label); } else { byte[] rgData = new byte[nCropDatumSize]; for (int h = h_off; h < h_off + height; h++) { for (int w = w_off; w < w_off + width; w++) { for (int c = 0; c < nDatumChannels; c++) { int nDatumIdx = (c * nDatumHeight + h) * nDatumWidth + w; int nCropDatumIdx = (c * height + h - h_off) * width + w - w_off; rgData[nCropDatumIdx] = d.ByteData[nDatumIdx]; } } } crop_datum.SetData(rgData.ToList(), d.Label); } return(crop_datum); }
/// <summary> /// The ApplyDistort method applies the distortion policy to the simple datum. /// </summary> /// <param name="sd">Specifies the SimpleDatum to distort.</param> /// <param name="p">Specifies the distortion parameters that define the distortion policy.</param> /// <returns>The distorted SimpleDatum is returned.</returns> public SimpleDatum ApplyDistort(SimpleDatum sd, DistortionParameter p) { double dfProb = m_random.NextDouble(); Bitmap bmp = ImageData.GetImage(sd); if (dfProb > 0.5) { bmp = randomBrightnessContrastSaturation(bmp, p.brightness_prob, p.brightness_delta, p.contrast_prob, p.contrast_lower, p.contrast_upper, p.saturation_prob, p.saturation_lower, p.saturation_upper); } else { bmp = randomBrightnessSaturationContrast(bmp, p.brightness_prob, p.brightness_delta, p.contrast_prob, p.contrast_lower, p.contrast_upper, p.saturation_prob, p.saturation_lower, p.saturation_upper); } SimpleDatum sd1 = ImageData.GetImageData(bmp, sd); sd.SetData(sd1); bmp.Dispose(); return(randomChannelOrder(sd, p.random_order_prob)); }
/// <summary> /// Expand the SimpleDatum according to the bbox. /// </summary> /// <param name="d">Specifies the SimpleDatum to expand.</param> /// <param name="expand_bbox">Specifies the bounding box.</param> /// <param name="fExpandRatio">Specifies the expansion ratio.</param> /// <returns>The expanded SimpleDatum is returned.</returns> public SimpleDatum ExpandImage(SimpleDatum d, NormalizedBBox expand_bbox, float fExpandRatio) { int nDatumChannels = d.Channels; int nDatumHeight = d.Height; int nDatumWidth = d.Width; // Get the bbox dimension. int width = (int)(nDatumHeight * fExpandRatio); int height = (int)(nDatumWidth * fExpandRatio); float h_off = (float)m_random.NextDouble(); float w_off = (float)m_random.NextDouble(); h_off = (float)Math.Floor(h_off); w_off = (float)Math.Floor(w_off); expand_bbox.xmin = -w_off / nDatumWidth; expand_bbox.ymin = -h_off / nDatumHeight; expand_bbox.xmax = (width - w_off) / nDatumWidth; expand_bbox.ymax = (height - h_off) / nDatumHeight; // Crop the image using bbox. SimpleDatum expand_datum = new SimpleDatum(d, height, width); int nExpandDatumSize = nDatumChannels * height * width; if (d.IsRealData) { double[] rgData = new double[nExpandDatumSize]; for (int h = (int)h_off; h < (int)h_off + nDatumHeight; h++) { for (int w = (int)w_off; w < (int)w_off + nDatumWidth; w++) { for (int c = 0; c < nDatumChannels; c++) { int nDatumIdx = (int)((c * nDatumHeight + h - h_off) * nDatumWidth + w - w_off); int nExpandIdx = (c * height + h) * width + w; rgData[nExpandIdx] = d.RealData[nDatumIdx]; } } } expand_datum.SetData(rgData.ToList(), d.Label); } else { byte[] rgData = new byte[nExpandDatumSize]; for (int h = (int)h_off; h < (int)h_off + nDatumHeight; h++) { for (int w = (int)w_off; w < (int)w_off + nDatumWidth; w++) { for (int c = 0; c < nDatumChannels; c++) { int nDatumIdx = (int)((c * nDatumHeight + h - h_off) * nDatumWidth + w - w_off); int nExpandIdx = (c * height + h) * width + w; rgData[nExpandIdx] = d.ByteData[nDatumIdx]; } } } expand_datum.SetData(rgData.ToList(), d.Label); } return(expand_datum); }