/// <summary> /// Applies a correctionMap to a raw DepthImage /// </summary> /// <param name="rawImage"></param> /// <param name="correctionMap"></param> /// <returns></returns> public DepthImage ApplyDepthCorrection(DepthImage rawImage, DepthCorrectionMap correctionMap) { if (rawImage.Width != correctionMap.Width ||rawImage.Height != correctionMap.Height) { throw new Exception("Image size does not match"); } //calculate the values for the section that is not involved in border cutting int BorderCutXmin = 0 + correctionMap.CutOffLeft; int BorderCutXmax = rawImage.Width - 1 - correctionMap.CutOffRight; int BorderCutYmin = 0 + correctionMap.CutOffTop; int BorderCutYmax = rawImage.Height - 1 - correctionMap.CutOffBOttom; //Apply the Depth Correction for (int x=0; x < rawImage.Width; x++) { for (int y = 0; y < rawImage.Height; y++) { if (rawImage.Data[x,y] > 0) //Only if there wasn't a reading error rawImage.Data[x, y] = rawImage.Data[x, y] + correctionMap.CorrectionData[x, y]; //Coordinates outside the CutOffBorder? if (!((BorderCutXmin <= x) && (x <= BorderCutXmax) && (BorderCutYmin <= y) && (y <= BorderCutYmax))) rawImage.Data[x, y] = 0; } } return rawImage; }
/// <summary> /// Creates a DepthCorrectionMap from an uncorrected raw image and the desired TableDistance /// </summary> /// <param name="uncorrectedImage"></param> /// <param name="averageTableDistance"></param> /// <returns></returns> public DepthCorrectionMap CreateDepthCorrectionMap(DepthImage uncorrectedImage, int averageTableDistance) { DepthCorrectionMap correctionMap = new DepthCorrectionMap(uncorrectedImage.Width, uncorrectedImage.Height); int lastValue = 0; for (int x = 0; x < uncorrectedImage.Width; x++) { for (int y = 0; y < uncorrectedImage.Height; y++) { if (uncorrectedImage.Data[x, y] > 0) { //Correctly recognized point: calculate correction value correctionMap.CorrectionData[x, y] = averageTableDistance - uncorrectedImage.Data[x, y]; lastValue = averageTableDistance - uncorrectedImage.Data[x, y]; } else { //Not recognized point (height = 0) use recent calibration value as approximation correctionMap.CorrectionData[x, y] = lastValue; } } } return correctionMap; }
public bool Load(string path) { if (!Directory.Exists(path)) return false; FileStream fstr = null; try { fstr = new FileStream(path + "CorrectionMap.bin", FileMode.Open); BinaryFormatter bfr = new BinaryFormatter(); object o = bfr.Deserialize(fstr); DefaultCorrectionMap = (DepthCorrectionMap)o; fstr.Close(); } catch { if (fstr != null) fstr.Close(); return false; } return true; }