public bool checkAgainstMask(Point location) { if (generatorState < State.TempletSet) { Debug.Log("Templet not set."); return(false); } double templetValue = Cv2.Sum(templetFill).Val0; OpenCvSharp.Rect maskRect = new OpenCvSharp.Rect(matchingBoxRect.X + location.X, matchingBoxRect.Y + location.Y, templetFill.Width, templetFill.Height); Mat mask = new Mat(plantMaskInvert, maskRect); Mat bitwiseResult = new Mat(); Cv2.BitwiseAnd(mask, templetFill, bitwiseResult); double outsideMaskValue = Cv2.Sum(bitwiseResult).Val0; double templetOutsideMaskRatio = 0.0; if (templetValue > 0.0) { templetOutsideMaskRatio = outsideMaskValue / templetValue; } if (templetOutsideMaskRatio > outsideMaskRatio) { return(false); } return(true); }
//分水岭分割函数封装 private Mat waterShed(Mat src, int MEADIANBlUR_KSIZE, Size ELEMENT_SIZE) { var imageGray = new Mat(); var thresh = new Mat(); var fg = new Mat(); var bgt = new Mat(); var bg = new Mat(); var marker = new Mat(); var marker32 = new Mat(); var m = new Mat(); var res = new Mat(); var threshOpen = new Mat(); var threshClose = new Mat(); Cv2.CvtColor(src, imageGray, ColorConversionCodes.BGR2GRAY); Cv2.EqualizeHist(imageGray, imageGray); //直方图均衡化 Cv2.MedianBlur(imageGray, imageGray, MEADIANBlUR_KSIZE); //中值滤波 Cv2.Threshold(imageGray, thresh, 0, 255, ThresholdTypes.Otsu); Cv2.Erode(thresh, fg, 0, null, 2); Cv2.Dilate(thresh, bgt, 0, null, 3); Cv2.Threshold(bgt, bg, 1, 128, ThresholdTypes.BinaryInv); marker = fg + bg; marker.ConvertTo(marker32, MatType.CV_32SC1); Cv2.Watershed(src, marker32); Cv2.ConvertScaleAbs(marker32, m); Cv2.Threshold(m, thresh, 0, 255, ThresholdTypes.Otsu); var element = Cv2.GetStructuringElement(MorphShapes.Rect, ELEMENT_SIZE); //获取自定义核 Cv2.MorphologyEx(thresh, threshOpen, MorphTypes.Open, element); //开运算 Cv2.MorphologyEx(threshOpen, threshClose, MorphTypes.Close, element); //闭运算; Cv2.BitwiseAnd(src, src, res, threshClose); return(res); }
private Mat GetSceleton(Mat grayImage) { var binary = new Mat(); Cv2.Threshold(grayImage, binary, 240, 255, ThresholdType.BinaryInv); var skel = Mat.Zeros(binary.Rows, binary.Cols, MatType.CV_8UC1).ToMat(); var temp = new Mat(binary.Cols, binary.Rows, MatType.CV_8UC1); var elem = Cv2.GetStructuringElement(StructuringElementShape.Cross, new OpenCvSharp.CPlusPlus.Size(3, 3)); bool done; do { Cv2.MorphologyEx(binary, temp, MorphologyOperation.Open, elem); Cv2.BitwiseNot(temp, temp); Cv2.BitwiseAnd(binary, temp, temp); Cv2.BitwiseOr(skel, temp, skel); Cv2.Erode(binary, binary, elem); double max; double min; Cv2.MinMaxLoc(binary, out min, out max); done = (max == 0); } while (!done); return(skel); }
public static Mat BitwiseAnd(this Mat mat1, Mat mat2) { var ret = new Mat(); Cv2.BitwiseAnd(mat1, mat2, ret); return(ret); }
static void Main(string[] args) { foreach (var s in new string[] { "l", "r" }) { for (int k = 4; k <= 7; k++) { for (int c = 1; c <= k; c++) { string kc = $@"D:\MEGAsync\dataset front features\k{k}\c{c}-{s}"; string[] files = Directory.EnumerateFiles(kc, "*.*", SearchOption.AllDirectories).Where(w => w.EndsWith(".png")).ToArray(); var dst = new Mat(files[0], ImreadModes.AnyColor); for (int i = 1; i < files.Length; i++) { var src = new Mat(files[i], ImreadModes.AnyColor); Cv2.BitwiseAnd(dst, src, dst); //Cv2.AddWeighted(dst, 0.5d, src, 0.5d, 0d, dst); } Cv2.Flip(dst, dst, FlipMode.X); dst.SaveImage($@"D:\MEGAsync\dataset front features\k{k}\c{c}-{s}.png"); } } } }
public IplImage SliceImage(IplImage src) //일정영역 제한 { slice = new IplImage(src.Size, BitDepth.U8, 3); mask = new IplImage(src.Size, BitDepth.U8, 3); mask = src.Clone(); Mat input1 = new Mat(mask); Mat bitwise = new Mat(); CvPoint[][] points = new CvPoint[2][]; for (int i = 0; i < 2; i++) { points[i] = new CvPoint[4]; } points[0][0] = new CvPoint(0, src.Height * 95 / 100); //좌하 points[0][3] = new CvPoint(src.Width, src.Height * 95 / 100); //우하 points[0][1] = new CvPoint(src.Width * 3 / 7, src.Height * 5 / 8); //좌상 points[0][2] = new CvPoint(src.Width * 4 / 7, src.Height * 5 / 8); //우상 Cv.FillPoly(slice, points, CvColor.White); Mat input2 = new Mat(slice); Cv2.BitwiseAnd(input1, input2, bitwise); return(bitwise.ToIplImage()); }
void createBinaryMasks(byte maskThreshold, bool ANDwithExistingMask = false) { for (int i = 0; i < 2; i++) { for (int j = 0; j < calibrationDevices.Length; j++) { if (calibrationDevices[j].isConnected) { Mat workingImage = new Mat(calibrationDevices[j].subtractionImage[i].Height, calibrationDevices[j].subtractionImage[i].Width, calibrationDevices[j].subtractionImage[i].Type(), 0); Cv2.Subtract(i == 0 ? calibrationDevices[j].webcam.leftImage : calibrationDevices[j].webcam.rightImage, calibrationDevices[j].subtractionImage[i], workingImage); if (!ANDwithExistingMask) { Cv2.Threshold(workingImage, calibrationDevices[j].maskImage[i], maskThreshold, 255, ThresholdTypes.Binary); } else { Mat tempMask = new Mat(calibrationDevices[j].subtractionImage[i].Height, calibrationDevices[j].subtractionImage[i].Width, calibrationDevices[j].subtractionImage[i].Type(), 0); Cv2.Threshold(workingImage, tempMask, maskThreshold, 255, ThresholdTypes.Binary); Cv2.BitwiseAnd(tempMask, calibrationDevices[j].maskImage[i], calibrationDevices[j].maskImage[i]); tempMask.Release(); } workingImage.Release(); } } } }
// Update is called once per frame void Update() { cap.Read(frame); if (!frame.Empty()) { //assume this part of the frame contains only background smoothed_img = frame.Blur(new Size(5, 5)); frame_hsv = frame.CvtColor(ColorConversionCodes.BGR2HSV); Scalar lb = new Scalar(0, 0, 50); Scalar ub = new Scalar(180, 70, 180); Mat disc = Cv2.GetStructuringElement(MorphShapes.Ellipse, new Size(7, 7)); Cv2.MorphologyEx(thresh, thresh, MorphTypes.Close, disc, null, 3); contours = Cv2.FindContoursAsMat(thresh, RetrievalModes.List, ContourApproximationModes.ApproxSimple); mask = new Mat(thresh.Size(), thresh.Type(), Scalar.All(0)); Cv2.Merge(new Mat[] { mask, mask, mask }, mask); Cv2.BitwiseAnd(mask, frame, mask); //Cv2.Merge(new Mat[]{frame_backproj,frame_backproj,frame_backproj},frame_backproj); tex.LoadImage(smoothed_img.ToBytes(".png", new int[] { 0 })); } }
public void Bitwise() { const int count = 256; var random = new Random(0); var array1 = Enumerable.Range(0, count).Select(i => (byte)i).OrderBy(_ => random.Next()).ToArray(); var array2 = Enumerable.Range(0, count).Select(i => (byte)i).OrderBy(_ => random.Next()).ToArray(); using var mat1 = new Mat(count, 1, MatType.CV_8UC1, array1); using var mat2 = new Mat(count, 1, MatType.CV_8UC1, array2); using var and = new Mat(); using var or = new Mat(); using var xor = new Mat(); using var not = new Mat(); Cv2.BitwiseAnd(mat1, mat2, and); Cv2.BitwiseOr(mat1, mat2, or); Cv2.BitwiseXor(mat1, mat2, xor); Cv2.BitwiseNot(mat1, not); for (int i = 0; i < count; i++) { Assert.Equal((byte)(array1[i] & array2[i]), and.Get <byte>(i)); Assert.Equal((byte)(array1[i] | array2[i]), or.Get <byte>(i)); Assert.Equal((byte)(array1[i] ^ array2[i]), xor.Get <byte>(i)); Assert.Equal((byte)(~array1[i]), not.Get <byte>(i)); } }
public IplImage BitwiseMat(IplImage src) { Mat Input1 = new Mat(src); Mat Input2 = new Mat(this.Binary(src, 150)); Mat bitwise = new Mat(); Window win_src1 = new Window("src1", WindowMode.StretchImage, Input1); Window win_src2 = new Window("src2", WindowMode.StretchImage, Input2); Cv2.BitwiseNot(Input1, bitwise); Window win_Not = new Window("BitwiseNot", WindowMode.StretchImage, bitwise); Cv2.BitwiseAnd(Input1, Input2.CvtColor(ColorConversion.GrayToBgr), bitwise); Window win_And = new Window("BitwiseAnd", WindowMode.StretchImage, bitwise); Cv2.BitwiseOr(Input1, Input2.CvtColor(ColorConversion.GrayToBgr), bitwise); Window win_Or = new Window("BitwiseOr", WindowMode.StretchImage, bitwise); Cv2.BitwiseXor(Input1, Input2.CvtColor(ColorConversion.GrayToBgr), bitwise); Window win_Xor = new Window("BitwiseXor", WindowMode.StretchImage, bitwise); //IplImage 형식// //Cv.Not(); //Cv.And(); //Cv.Or(); //Cv.Xor(); //IplImage 형식// return(Input2.ToIplImage()); }
private void MaskSegmentasionImage() { if (plantMask.Size() != plantSegmentasionImage.Size()) { Cv2.Resize(plantMask, plantMask, plantSegmentasionImage.Size(), 0, 0, InterpolationFlags.Linear); } Cv2.BitwiseAnd(plantSegmentasionImage, plantMask, plantSegmentasionImage); }
private Mat MaskedFrame() { var maskedFrame = new Mat(); Cv2.BitwiseAnd(resizedFrame, resizedFrame, maskedFrame, MakeMask()); return(maskedFrame); }
public override void Apply(Mat input) { _start = DateTime.Now; Input = input; Mat hsv = new Mat(); Mat ycrcb = new Mat(); Mat hsv_mask = new Mat(input.Size(), MatType.CV_8UC1, Scalar.All(0)); Mat ycrcb_mask = new Mat(input.Size(), MatType.CV_8UC1, Scalar.All(0)); Mat global_mask = new Mat(input.Size(), MatType.CV_8UC1, Scalar.All(0)); //Cv2.CvtColor(hsv, hsv, ColorConversionCodes.BGR2HSV); //Cv2.CvtColor(ycrcb, ycrcb, ColorConversionCodes.BGR2HSV); if (_config.HSVThresholdConfig.IsActive) { Cv2.CopyTo(input, hsv); HSVFilter hsvFilter = new HSVFilter(); HSVThresoldFilter hsvThreshold = new HSVThresoldFilter(_config.HSVThresholdConfig); hsvFilter.Apply(hsv); hsvThreshold.Apply(hsvFilter.Output); Cv2.CopyTo(hsvThreshold.Output, hsv_mask); } else { hsv_mask = new Mat(input.Size(), MatType.CV_8UC1, Scalar.All(255)); } if (_config.YCrCbThresholdConfig.IsActive) { Cv2.CopyTo(input, ycrcb); YCrCbFilter ycrcbFilter = new YCrCbFilter(); YCrCbThreshold ycrcbthreshold = new YCrCbThreshold(_config.YCrCbThresholdConfig); ycrcbFilter.Apply(ycrcb); ycrcbthreshold.Apply(ycrcbFilter.Output); Cv2.CopyTo(ycrcbthreshold.Output, ycrcb_mask); } else { ycrcb_mask = new Mat(input.Size(), MatType.CV_8UC1, Scalar.All(255)); } Cv2.BitwiseAnd(hsv_mask, ycrcb_mask, global_mask); Cv2.CopyTo(global_mask, Output); //Cv2.CopyTo(input, Output, global_mask); //if (IsActive) //{ //OpenCvSharp.Cv2.InRange(input, new OpenCvSharp.Scalar(_config.HSVThresholdConfig.H.Min, _config.HSVThresholdConfig.S.Min, _config.HSVThresholdConfig.V.Min), new OpenCvSharp.Scalar(_config.HSVThresholdConfig.H.Max, _config.HSVThresholdConfig.S.Max, _config.HSVThresholdConfig.V.Max), Output); //} //else //{ // OpenCvSharp.Cv2.CopyTo(input, Output); //} base.Apply(input); }
public static void killDarkPixel(Mat srcImage, int threshold) { Mat mask = new Mat(); Cv2.InRange(srcImage, new Scalar(0, 0, 0), new Scalar(threshold, threshold, threshold), mask); Mat bg = new Mat(new Size(srcImage.Width, srcImage.Height), MatType.CV_8UC3, new Scalar(0, 0, 0)); Cv2.BitwiseAnd(bg, srcImage, srcImage, mask); }
//Настройка нижнего порога преобразования private void numThresholdMax_ValueChanged(object sender, EventArgs e) { numOfContCB.Enabled = false; threshold = depthMap.Clone().InRange(new Scalar((double)numThresholdMin.Value), new Scalar((double)numThresholdMax.Value)).Clone().Erode(new Mat()).Dilate(new Mat()); Mat output = Mat.Zeros(threshold.Size(), threshold.Type()); Cv2.BitwiseAnd(depthMap, threshold, output); window.ShowImage(output); output.Dispose(); }
/// <summary> /// この関数を実行すると,カメラから画像を取得し,グリッドマークを検出しその座標を返します. /// 実行時のレンズはx50対物であることを前提とします. /// </summary> /// <returns>グリッドマークを検出したピクセル座標。検出できなかった時は(-1,-1)が返される</returns> public Vector2 SearchGridMarkx50() { // レンズが50倍に設定されていない場合は例外を返すようにしたいがやり方が分からん(20140724) //if (parameterManager.Magnification != ParameterManager.) { // throw new LensTypeException(ParameterManager.LensMagnificationOfGridMarkSearch); //} Camera c = Camera.GetInstance(); byte[] b = c.ArrayImage; Mat mat = new Mat(440, 512, MatType.CV_8U, b); //mat.ImWrite(String.Format(@"c:\img\{0}_g.bmp", System.DateTime.Now.ToString("yyyyMMdd_HHmmss_fff"))); Cv2.GaussianBlur(mat, mat, Cv.Size(5, 5), -1); Cv2.Threshold(mat, mat, 60, 255, ThresholdType.BinaryInv); //mat.ImWrite(String.Format(@"c:\img\{0}_t.bmp", System.DateTime.Now.ToString("yyyyMMdd_HHmmss_fff"))); Moments mom = new Moments(mat); if (mom.M00 < 1000 * 255) { return(new Vector2(-1.0, -1.0)); } double cx = mom.M10 / mom.M00; double cy = mom.M01 / mom.M00; Mat innercir = Mat.Zeros(440, 512, MatType.CV_8UC1); Cv2.Circle(innercir, new Point(cx, cy), 30, new Scalar(255, 255, 255), 3); int innerpath = Cv2.CountNonZero(innercir); Cv2.BitwiseAnd(innercir, mat, innercir); int innersum = Cv2.CountNonZero(innercir); Mat outercir = Mat.Zeros(440, 512, MatType.CV_8UC1); Cv2.Circle(outercir, new Point(cx, cy), 200, new Scalar(255, 255, 255), 3); int outerpath = Cv2.CountNonZero(outercir); Cv2.BitwiseAnd(outercir, mat, outercir); int outersum = Cv2.CountNonZero(outercir); double innerratio = innersum * 1.0 / innerpath * 1.0; double outerratio = outersum * 1.0 / outerpath * 1.0; if (innerratio < 0.8) { return(new Vector2(-1.0, -1.0)); } if (outerratio > 0.2) { return(new Vector2(-1.0, -1.0)); } return(new Vector2(cx, cy)); }
public void 評価用画像作成(Mat テンプレート, Mat 検査結果, ref Mat dst) { Mat mask = テンプレート.Clone();//この時点では輪郭が白 Zero(ref dst); Cv2.BitwiseNot(テンプレート, mask);//正解画像の輪郭を黒にする Cv2.BitwiseAnd(検査結果, 検査結果, dst, mask); mask.Dispose(); }
private void Button2_Click(object sender, EventArgs e) { // Button2 클릭 이벤트 // 온도로 표시되는 특정 색상만을 추출하여 pictureBox1에 띄움. // 색상 검출에 쓰이는 Opencv의 Mat 형식을 사용하기 위해 Bitmap 형식을 Mat으로 바꿔줌. src1 = BitmapConverter.ToMat(bmp1); src2 = BitmapConverter.ToMat(bmp1); Mat[] mv1 = new Mat[3]; Mat[] mv2 = new Mat[3]; Mat mask1 = new Mat(); Mat mask2 = new Mat(); // 특정 색 검출을 위해 RBG 변환 Cv2.CvtColor(src1, src1, ColorConversionCodes.BGR2RGB); mv1 = Cv2.Split(src1); Cv2.CvtColor(src1, src1, ColorConversionCodes.RGB2BGR); // 색상 추출 시 필요한 값을 기준으로 영역 내 색상을 나타내는 마스크 생성 // 최솟값 ~ 최댓값 사이의 색상(RGB는 3채널로 3채널 Scalar)을 이용해 mask 생성 Cv2.InRange(mv1[0], new Scalar(0, 240, 0), new Scalar(14, 255, 3), mask1); // 원본 이미지에 BitwiseAnd 연산을 통해 원하는 색상 추출 Cv2.BitwiseAnd(src1, mask1.CvtColor(ColorConversionCodes.GRAY2BGR), src1); // 적색은 BGR로 색 추출하는 것이 나음. Cv2.CvtColor(src2, src2, ColorConversionCodes.BGR2BGR555); mv2 = Cv2.Split(src2); Cv2.CvtColor(src2, src2, ColorConversionCodes.BGR5552BGR); Cv2.InRange(mv2[0], new Scalar(0, 0, 250), new Scalar(3, 3, 255), mask2); // Cv2.InRange(mv2[0], new Scalar((double)Scalar.Red), new Scalar((double)Scalar.Red), mask2); Cv2.BitwiseAnd(src2, mask2.CvtColor(ColorConversionCodes.GRAY2BGR), src2); // 추출한 색을 바탕으로 이진화. Mat gray1 = new Mat(); Mat gray2 = new Mat(); Cv2.CvtColor(src1, gray1, ColorConversionCodes.BGR2GRAY); Cv2.Threshold(gray1, src1, 50, 240, ThresholdTypes.BinaryInv); // Mat 형식은 pictureBox에 띄울 수 없으므로 다시 Bitmap 형식으로 변환 bmp1 = BitmapConverter.ToBitmap(src1); pictureBox1.Image = bmp1; Cv2.CvtColor(src2, gray2, ColorConversionCodes.BGR2GRAY); Cv2.Threshold(gray2, src2, 50, 240, ThresholdTypes.BinaryInv); bmp2 = BitmapConverter.ToBitmap(src2); pictureBox2.Image = bmp2; }
/// <summary> /// Calculate overlap of two binary images of ellipses. /// </summary> /// <param name="gtEllipseImage">Binary image with the ground truth ellipse</param> /// <param name="fitEllipseImage">Binary image with the fitted ellipse</param> /// <returns>Relative overlap of two binary images of ellipses</returns> private static float EvaluateOverlap(Mat gtEllipseImage, Mat fitEllipseImage) { int fitSum = (int)Cv2.Sum(fitEllipseImage).Val0; int gtSum = (int)Cv2.Sum(gtEllipseImage).Val0; Mat overlapImage = new Mat(); Cv2.BitwiseAnd(fitEllipseImage, gtEllipseImage, overlapImage); int overlapSum = (int)Cv2.Sum(overlapImage).Val0; return(overlapSum / (float)Math.Max(fitSum, gtSum)); }
private void 定義済み色抽出(Mat src, ref Mat dst) { String point_info = ""; Mat mask_combine = new Mat(src.Size(), MatType.CV_8UC3); OpenCvSharp.Point point_右手 = new Point(); OpenCvSharp.Point point_左手 = new Point(); OpenCvSharp.Point point_右足 = new Point(); OpenCvSharp.Point point_左足 = new Point(); OpenCvSharp.Point point_胴 = new Point(); Mat mask_右手 = new Mat(src.Size(), MatType.CV_8UC3); Mat mask_左手 = new Mat(src.Size(), MatType.CV_8UC3); Mat mask_右足 = new Mat(src.Size(), MatType.CV_8UC3); Mat mask_左足 = new Mat(src.Size(), MatType.CV_8UC3); Mat mask_胴 = new Mat(src.Size(), MatType.CV_8UC3); 色抽出_面積最大マスク(src, Scalar_右手[0], Scalar_右手[1], ref mask_右手, ref point_右手); 色抽出_面積最大マスク(src, Scalar_左手[0], Scalar_左手[1], ref mask_左手, ref point_左手); 色抽出_面積最大マスク(src, Scalar_右足[0], Scalar_右足[1], ref mask_右足, ref point_右足); 色抽出_面積最大マスク(src, Scalar_左足[0], Scalar_左足[1], ref mask_左足, ref point_左足); 色抽出_面積最大マスク(src, Scalar_胴[0], Scalar_胴[1], ref mask_胴, ref point_胴); マスク合成(new Mat[] { mask_右手, mask_左手, mask_右足, mask_左足, mask_胴 }, ref mask_combine); mask_右手.Dispose(); mask_左手.Dispose(); mask_右足.Dispose(); mask_左足.Dispose(); mask_胴.Dispose(); point_info += "" + point_右手.X + ',' + point_右手.Y + ','; point_info += "" + point_左手.X + ',' + point_左手.Y + ','; point_info += "" + point_右足.X + ',' + point_右足.Y + ','; point_info += "" + point_左足.X + ',' + point_左足.Y + ','; point_info += "" + point_胴.X + ',' + point_胴.Y; textBox_Points.Text = point_info; Cv2.BitwiseAnd(src, mask_combine, dst); mask_combine.Dispose(); }
public Mat ApplyCartoonFilter(Mat imageSource) { if (imageSource == null) { return(null); } Mat bilateralImage = ApplyBilateralFilter(imageSource, cartoonFilterFactor); Mat cannyImage = 255 - imageSource.Canny(100, 200); Cv2.BitwiseAnd(bilateralImage, cannyImage, imageSource); return(imageSource); }
//Выбор контура из списка обнаруженных private void numOfContCB_SelectedIndexChanged(object sender, EventArgs e) { Mat output = Mat.Zeros(threshold.Size(), threshold.Type()); mask = Mat.Zeros(threshold.Size(), threshold.Type()); mask.DrawContours(contours, numOfContCB.SelectedIndex, Scalar.White, -1); Cv2.BitwiseAnd(depthMap, mask, output); window.ShowImage(output); output.Dispose(); }
public static void KillGrayPixel(Mat srcImage, int threshold) { Mat satImage = srcImage.CvtColor(ColorConversionCodes.RGB2HSV); Mat[] hsvChannels = satImage.Split(); Mat satChannel = hsvChannels[1]; satChannel = satChannel.Threshold(threshold, 255, ThresholdTypes.Binary); Cv2.BitwiseNot(satChannel, satChannel); Mat bg = new Mat(new Size(srcImage.Width, srcImage.Height), MatType.CV_8UC3, new Scalar(0, 0, 0)); Cv2.BitwiseAnd(bg, srcImage, srcImage, satChannel); }
static Mat DiffImage(Mat t0, Mat t1, Mat t2) { Mat d1 = new Mat(); Cv2.Absdiff(t2, t1, d1); Mat d2 = new Mat(); Cv2.Absdiff(t1, t0, d2); Mat diff = new Mat(); Cv2.BitwiseAnd(d1, d2, diff); return(diff); }
//public static byte[] YakultLowerMaskData { get; } = { 105, 50, 50 }; //public static byte[] YakultUpperMaskData { get; } = { 117, 255, 255 }; //private InputArray YakultLowerMask { get; } = InputArray.Create(YakultLowerMaskData); //private InputArray YakultUpperMask { get; } = InputArray.Create(YakultUpperMaskData); public override void Update( ) { Mat edgeMask = new Mat( ); Cv2.Canny(Source.GrayFrame, edgeMask, 300, 200); edgeMask = edgeMask.Dilate(null, iterations: 3); edgeMask = edgeMask.MorphologyEx(MorphTypes.Close, null, iterations: 8); edgeMask = edgeMask.Erode(null, iterations: 4); Mat colorMask = Source.HsvImage.InRange(SteelBallLowerMask, SteelBallUpperMask); colorMask = colorMask.MorphologyEx(MorphTypes.Close, null); Cv2.BitwiseAnd(edgeMask, colorMask, Mask); }
private void bn_LogicalSum_Click(object sender, RoutedEventArgs e) { if (listImage.Count > 0) { SubWindow.Win_LogicalSum win = new SubWindow.Win_LogicalSum(listImage); if (win.ShowDialog() == true) { int nMode = win.nMode; int nIdx1 = win.nSrc1; int nIdx2 = win.nSrc2; Mat matSrc1 = listImage[nIdx1].fn_GetImage(); Mat matSrc2 = listImage[nIdx2].fn_GetImage(); Mat matDst = new Mat(); timeStart = DateTime.Now; switch (nMode) { case 0: Cv2.Add(matSrc1, matSrc2, matDst); break; case 1: Cv2.Subtract(matSrc1, matSrc2, matDst); break; case 2: //Cv2.Average(matSrc1, matSrc2, matDst); break; case 3: //Cv2.Differential() Cv2.Absdiff(matSrc1, matSrc2, matDst); break; case 4: Cv2.BitwiseAnd(matSrc1, matSrc2, matDst); break; case 5: Cv2.BitwiseOr(matSrc1, matSrc2, matDst); break; } fn_WriteLog($"[Logical Sum] {listImage[nIdx1].Title} + {listImage[nIdx2].Title} : {nMode} ({(DateTime.Now - timeStart).TotalMilliseconds} ms)"); fn_NewImage(matDst, $"Logical Sum {nMode}"); } } }
private void SetBinary(Parameters para) { if (binarySet) { return; } if (!rMaksSet) { SetRangeMask(para); } if (!cMaskSet) { SetColorMask(para); } Cv2.BitwiseAnd(cMask, rMask, binary); binarySet = true; }
public static Mat ClampThreshold(Mat data, Color color, float errorFactor) { var splitedData = data.Split(); var splitedColor = new int[] { color.B, color.G, color.R }; var limit = 255 * errorFactor; for (var i = 0; i < splitedData.Length; i++) { var mask_ovr = splitedData[i].Threshold((int)(splitedColor[i] - limit), 255, ThresholdTypes.Binary); var mask_und = splitedData[i].Threshold((int)(splitedColor[i] + limit), 255, ThresholdTypes.BinaryInv); Cv2.BitwiseAnd(mask_ovr, mask_und, splitedData[i]); } var ret = (splitedData[0] & splitedData[1] & splitedData[2]).ToMat(); return(ret); }
//通过轮廓分割图像 private Mat contourSeg(Mat src, ThresholdTypes CONTOUR_TYPE, int SIGMAX) { Mat imageGray = new Mat(); Mat img = new Mat(); Cv2.CvtColor(src, imageGray, ColorConversionCodes.RGB2GRAY); Cv2.GaussianBlur(imageGray, imageGray, new Size(5, 5), SIGMAX); Cv2.Threshold(imageGray, img, 0, 255, CONTOUR_TYPE); Mat element = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(10, 10)); Cv2.MorphologyEx(img, img, MorphTypes.Open, element); Cv2.MorphologyEx(img, img, MorphTypes.Close, element); Mat[] contours; var hierarchy = new Mat(); Cv2.FindContours(img, out contours, hierarchy, RetrievalModes.CComp, ContourApproximationModes.ApproxSimple); //查找最大轮廓 double maxarea = 0; int maxAreaIdx = 0; for (int index = contours.Length - 1; index >= 0; index--) { double tmparea = Math.Abs(Cv2.ContourArea(contours[index])); if (tmparea > maxarea) { maxarea = tmparea; maxAreaIdx = index; //记录最大轮廓的索引号 } } Mat[] contourMax = new Mat[1]; var maxContour = contours[maxAreaIdx]; contourMax.SetValue(maxContour, 0); Mat mask = Mat.Zeros(src.Size(), MatType.CV_8UC1); //掩模上填充轮廓 Cv2.DrawContours(mask, contourMax, -1, Scalar.All(255), -1); Mat real = src.Clone(); var result = new Mat(); Cv2.CvtColor(mask, mask, ColorConversionCodes.GRAY2RGB); Cv2.BitwiseAnd(real, mask, result); return(result); }
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); }