private Mat histMasking(Mat cameraMat) { Mat hsvMat = new Mat(); Mat dstMat = new Mat(); Cv2.CvtColor(cameraMat, hsvMat, ColorConversionCodes.BGR2HSV); Cv2.CalcBackProject(new[] { hsvMat }, new[] { 0, 1 }, handHist, dstMat, new[] { new Rangef(0, 180), new Rangef(0, 255) }); if (filter2D) { Mat filter2DMat = Cv2.GetStructuringElement(filter2DShape, (new Size(filter2DSize.x, filter2DSize.y))); Cv2.Filter2D(dstMat, dstMat, -1, filter2DMat); } if (thresholdFilter) { Cv2.Threshold(dstMat, dstMat, thresholdLow, thresholdHigh, ThresholdTypes.Binary); } if (morphologyTransformation) { Mat morphologyTransformationMat = Cv2.GetStructuringElement(morphologyTransformationShape, (new Size(morphologyTransformationSize.x, morphologyTransformationSize.y))); Cv2.MorphologyEx(dstMat, dstMat, morphologyTransformationType, morphologyTransformationMat, null, morphologyTransformationIterations); } if (dilatateFilter) { Mat dilatateShapeMat = Cv2.GetStructuringElement(this.dilatateShape, (new Size(dilatateSize.x, dilatateSize.y))); Cv2.Dilate(dstMat, dstMat, dilatateShapeMat, null, dilatateIterations); } if (erodeFilter) { Mat erodeShapeMat = Cv2.GetStructuringElement(this.erodeShape, (new Size(erodeSize.x, erodeSize.y))); Cv2.Erode(dstMat, dstMat, erodeShapeMat, null, erodeIterations); } handMask = dstMat; if (bitwiseFilter) { // Cv2.BitwiseAnd(cameraMat, dstMat, dstMat); //TODO: we need it to mask our hand, in this form it return exception } return(dstMat); }
public Mat histMaking() { Mat hsv = new Mat(); Mat dst = new Mat(); Mat disc = new Mat(); Mat thresh = new Mat(); Mat merge_thresh = new Mat(); Mat result = new Mat(); Cv2.CvtColor(frame, hsv, ColorConversionCodes.BGR2HSV); Cv2.CalcBackProject(new Mat[] { hsv }, new int[] { 0, 1 }, handHist, dst, new Rangef[] { new Rangef(0, 180), new Rangef(0, 256) }); disc = Cv2.GetStructuringElement(MorphShapes.Ellipse, new Size(31, 31)); Cv2.Filter2D(dst, dst, -1, disc); Cv2.Threshold(dst, thresh, 150, 255, ThresholdTypes.Binary); Cv2.Merge(new Mat[] { thresh, thresh, thresh }, merge_thresh); Cv2.BitwiseAnd(frame, merge_thresh, result); return(result); }
static int VoteForSizeAndOrientation(KeyPoint[] modelKeyPoints, KeyPoint[] observedKeyPoints, DMatch[][] matches, Mat mask, float scaleIncrement, int rotationBins) { int idx = 0; int nonZeroCount = 0; byte[] maskMat = new byte[mask.Rows]; GCHandle maskHandle = GCHandle.Alloc(maskMat, GCHandleType.Pinned); using (Mat m = new Mat(mask.Rows, 1, MatType.CV_8U, maskHandle.AddrOfPinnedObject())) { mask.CopyTo(m); List <float> logScale = new List <float>(); List <float> rotations = new List <float>(); double s, maxS, minS, r; maxS = -1.0e-10f; minS = 1.0e10f; //if you get an exception here, it's because you're passing in the model and observed keypoints backwards. Just switch the order. for (int i = 0; i < maskMat.Length; i++) { if (maskMat[i] > 0) { KeyPoint observedKeyPoint = observedKeyPoints[i]; KeyPoint modelKeyPoint = modelKeyPoints[matches[i][0].TrainIdx]; s = Math.Log10(observedKeyPoint.Size / modelKeyPoint.Size); logScale.Add((float)s); maxS = s > maxS ? s : maxS; minS = s < minS ? s : minS; r = observedKeyPoint.Angle - modelKeyPoint.Angle; r = r < 0.0f ? r + 360.0f : r; rotations.Add((float)r); } } int scaleBinSize = (int)Math.Ceiling((maxS - minS) / Math.Log10(scaleIncrement)); if (scaleBinSize < 2) { scaleBinSize = 2; } float[] scaleRanges = { (float)minS, (float)(minS + scaleBinSize + Math.Log10(scaleIncrement)) }; using (var scalesMat = new Mat <float>(rows: logScale.Count, cols: 1, data: logScale.ToArray())) using (var rotationsMat = new Mat <float>(rows: rotations.Count, cols: 1, data: rotations.ToArray())) using (var flagsMat = new Mat <float>(logScale.Count, 1)) using (Mat hist = new Mat()) { flagsMat.SetTo(new Scalar(0.0f)); float[] flagsMatFloat1 = flagsMat.ToArray(); int[] histSize = { scaleBinSize, rotationBins }; float[] rotationRanges = { 0.0f, 360.0f }; int[] channels = { 0, 1 }; // with infrared left and right, rotation max = min and calchist fails. Adding 1 to max enables all this to work! if (rotations.Count > 0) { Rangef[] ranges = { new Rangef(scaleRanges[0], scaleRanges[1]), new Rangef(rotations.Min(), rotations.Max() + 1) }; double minVal, maxVal; Mat[] arrs = { scalesMat, rotationsMat }; Cv2.CalcHist(arrs, channels, null, hist, 2, histSize, ranges); Cv2.MinMaxLoc(hist, out minVal, out maxVal); Cv2.Threshold(hist, hist, maxVal * 0.5, 0, ThresholdTypes.Tozero); Cv2.CalcBackProject(arrs, channels, hist, flagsMat, ranges); MatIndexer <float> flagsMatIndexer = flagsMat.GetIndexer(); for (int i = 0; i < maskMat.Length; i++) { if (maskMat[i] > 0) { if (flagsMatIndexer[idx++] != 0.0f) { nonZeroCount++; } else { maskMat[i] = 0; } } } m.CopyTo(mask); } } } maskHandle.Free(); return(nonZeroCount); }