public void run(Image <Gray, byte> image) { Stopwatch sw = new Stopwatch(); sw.Start(); _imageInput = image; if (null != _imageInput) { if (0 != actionMultiSearchData.InputAOIWidth && 0 != actionMultiSearchData.InputAOIHeight) { _imageInput.ROI = new Rectangle(actionMultiSearchData.InputAOIX, actionMultiSearchData.InputAOIY, actionMultiSearchData.InputAOIWidth, actionMultiSearchData.InputAOIHeight); } } else { return; } Image <Gray, double> integralOfInput = _imageInput.Integral(); result1 = GetIntegralDiff(integralOfInput, 1, 1, dIntegralOfModel); result2 = GetIntegralDiff(integralOfInput, actionMultiSearchData.ModelAOIWidth / 4, actionMultiSearchData.ModelAOIHeight / 4, dIntegralOfModel2); Image <Gray, byte> _image = new Image <Gray, byte>(result1.Bitmap); //_image._EqualizeHist(); // CvInvoke.Threshold(_image, result1,80,100, ThresholdType.BinaryInv); //CvInvoke.Threshold(result2, result2, 80, 100, ThresholdType.BinaryInv); result = new Image <Gray, double>(result1.Size); CvInvoke.Add(result1, result2, result); CvInvoke.Threshold(result, result, 100, 255, ThresholdType.BinaryInv); //detector(result); sw.Stop(); }
public override void ActionExcute() { Stopwatch sw = new Stopwatch(); sw.Start(); _imageInput = VisionManage.listScene[VisionManage.iCurrSceneIndex].listAction[actionData.imageSrc - 1].imageResult.Clone(); if (null != _imageInput) { if (0 != actionMultiSearchData.InputAOIWidth && 0 != actionMultiSearchData.InputAOIHeight) { _imageInput.ROI = new Rectangle(actionMultiSearchData.InputAOIX, actionMultiSearchData.InputAOIY, actionMultiSearchData.InputAOIWidth, actionMultiSearchData.InputAOIHeight); } } else { return; } Image <Gray, double> integralOfInput = _imageInput.Integral(); result1 = GetIntegralDiff(integralOfInput, 1, 1, dIntegralOfModel); result2 = GetIntegralDiff(integralOfInput, actionMultiSearchData.ModelAOIWidth / 4, actionMultiSearchData.ModelAOIHeight / 4, dIntegralOfModel2); Image <Gray, byte> _image = new Image <Gray, byte>(result1.Bitmap); //_image._EqualizeHist(); // CvInvoke.Threshold(_image, result1,80,100, ThresholdType.BinaryInv); //CvInvoke.Threshold(result2, result2, 80, 100, ThresholdType.BinaryInv); result = new Image <Gray, double>(result1.Size); CvInvoke.Add(result1, result2, result); CvInvoke.Threshold(result, result, 100, 255, ThresholdType.BinaryInv); //detector(result); sw.Stop(); }
private void buttonSharpContrast_Click(object sender, EventArgs e) { double multiplier = Convert.ToDouble(textBoxMultiplier.Text); int threshold = Convert.ToInt16(textBoxThreshold.Text); var imageConvertedCopy = new Image <Gray, byte>(imageOriginal.Cols, imageOriginal.Rows); for (int row = 0; row < imageOriginal.Rows - 1; row++) { for (int col = 0; col < imageOriginal.Cols - 1; col++) { int pixel = imageConverted.Data[row, col, 0]; pixel = (int)((pixel - threshold) * multiplier); pixel += threshold; if (pixel < 0) { pixel = 0; } else if (pixel > 255) { pixel = 255; } imageConvertedCopy.Data[row, col, 0] = (byte)pixel; } } imageBoxShapen.Image = imageConvertedCopy; var imageConverted2 = new Image <Gray, byte>(imageOriginal.Cols, imageOriginal.Rows); CvInvoke.Add(imageConvertedCopy, imageDifference, imageConverted2, null, DepthType.Default); imageBox2.Image = imageConverted2; }
//Not sure why cv:AddS went away public static void AddS(Mat input, double amount, IOutputArray output) { Mat scale = new Mat(input.Size, input.Depth, 1); scale.SetTo(new MCvScalar(amount)); CvInvoke.Add(input, scale, output); }
private void ProcessImage(List <string> lines) { Mat imageOriginal = CvInvoke.Imread(ImageRecievedName, LoadImageType.AnyColor); var imageWithHitsBgr = CreateHitImage(imageOriginal.Size, lines); // create mask to have white circles wherever hits exist and to be black on all other parts var mask = new Mat(); CvInvoke.Threshold(imageWithHitsBgr, mask, 1, 255, ThresholdType.Binary); var inverseMask = new Mat(); CvInvoke.BitwiseNot(mask, inverseMask); // mapping level of gray to ColorMap CvInvoke.ApplyColorMap(imageWithHitsBgr, imageWithHitsBgr, ColorMapType.Jet); // from mapped image remove everything except hits var imageWithHitsWithoutBackground = new Mat(); CvInvoke.BitwiseAnd(imageWithHitsBgr, imageWithHitsBgr, imageWithHitsWithoutBackground, mask); // from original image remove only parts where hits happended var imageOriginalWithoutHits = new Mat(); CvInvoke.BitwiseAnd(imageOriginal, imageOriginal, imageOriginalWithoutHits, inverseMask); // result is combination of original image without hits and image with hits mapped to certain ColorMap var result = new Mat(); CvInvoke.Add(imageOriginalWithoutHits, imageWithHitsWithoutBackground, result); result.Save(ImageProcessedName); }
/// <summary> /// Maximizing image contrast /// </summary> /// <param name="imgGrayscale">Grayscale input image</param> /// <param name="errorCode">Error code</param> /// <returns>Grayscale image with maximum contrast</returns> public static Mat MaximizeContrast(Mat imgGrayscale, ref int errorCode) { try { // Initiation of images that will be used in this method Mat imgTopHat = new Mat(); Mat imgBlackHat = new Mat(); Mat imgGrayscalePlusTopHat = new Mat(); Mat imgGrayscalePlusTopHatMinusBlackHat = new Mat(); // Structuring element used for morphological operation Mat structuringElement = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(9, 7), new Point(-1, -1)); // Morphological operation of tophap CvInvoke.MorphologyEx(imgGrayscale, imgTopHat, MorphOp.Tophat, structuringElement, new Point(-1, -1), 1, BorderType.Default, new MCvScalar()); // Morphological operation of blackhat CvInvoke.MorphologyEx(imgGrayscale, imgBlackHat, MorphOp.Blackhat, structuringElement, new Point(-1, -1), 1, BorderType.Default, new MCvScalar()); // Add grayscale image with tophat image CvInvoke.Add(imgGrayscale, imgTopHat, imgGrayscalePlusTopHat); // Substract grayscale plus tophat image from blackhat image CvInvoke.Subtract(imgGrayscalePlusTopHat, imgBlackHat, imgGrayscalePlusTopHatMinusBlackHat); // Return input image with maximum contrast return(imgGrayscalePlusTopHatMinusBlackHat); } catch (Exception ex) { Console.WriteLine(ex); errorCode = 5; return(null); } }
public void run(Image <Gray, Byte> image) { Stopwatch sw = new Stopwatch(); sw.Start(); imageInput = image; Image <Gray, byte> _image; if (0 != actionBrightCorrectData.InputAOIWidth && 0 != actionBrightCorrectData.InputAOIHeight) { Rectangle rectROI = new Rectangle(actionBrightCorrectData.InputAOIX, actionBrightCorrectData.InputAOIY, actionBrightCorrectData.InputAOIWidth, actionBrightCorrectData.InputAOIHeight); _imageInput.ROI = rectROI; _image = new Image <Gray, byte>(new Size(actionBrightCorrectData.InputAOIWidth, actionBrightCorrectData.InputAOIHeight)); } else { _image = new Image <Gray, byte>(_imageInput.Size); } _imageResult = _imageInput.Clone(); ///<summary>亮度叠加 if (null != _imageBright) { if (_imageBright.Size == _image.Size) { _imageBright.CopyTo(_image); if (actionBrightCorrectData.bDirect) { CvInvoke.Subtract(_imageInput, _image, _imageResult); } else { CvInvoke.Add(_imageInput, _image, _imageResult); } } else { _imageBright = null; } } ///<summary>亮度均衡化 if (actionBrightCorrectData.bEqualize) { CvInvoke.EqualizeHist(_imageResult, _imageResult); } ///<summary>ROI清除 if (actionBrightCorrectData.bROIReset) { CvInvoke.cvResetImageROI(_imageResult); } CvInvoke.cvResetImageROI(_imageInput); sw.Stop(); }
public static void Add(this Mat mat, double value0, double value1, double value2) { //mat.ConvertTo(mat, mat.Depth, 0, value); Mat op = new Mat(mat.Size, mat.Depth, mat.NumberOfChannels); op.SetTo(new MCvScalar(value0, value1, value2)); CvInvoke.Add(mat, op, mat); op.Dispose(); }
public override void ActionExcute() { Stopwatch sw = new Stopwatch(); sw.Start(); _imageInput = VisionManage.listScene[VisionManage.iCurrSceneIndex].listAction[actionData.imageSrc - 1].imageResult.Clone(); ActionBase action = VisionManage.listScene[VisionManage.iCurrSceneIndex].listAction[actionBrightCorrectData.imageSrc]; Image <Gray, byte> _image; if (0 != actionBrightCorrectData.InputAOIWidth && 0 != actionBrightCorrectData.InputAOIHeight) { Rectangle rectROI = new Rectangle(actionBrightCorrectData.InputAOIX, actionBrightCorrectData.InputAOIY, actionBrightCorrectData.InputAOIWidth, actionBrightCorrectData.InputAOIHeight); _imageInput.ROI = rectROI; _image = new Image <Gray, byte>(new Size(actionBrightCorrectData.InputAOIWidth, actionBrightCorrectData.InputAOIHeight)); } else { _image = new Image <Gray, byte>(_imageInput.Size); } _imageResult = _imageInput.Clone(); if (null != _imageBright) { if (_imageBright.Size == _image.Size) { _imageBright.CopyTo(_image); if (actionBrightCorrectData.bDirect) { CvInvoke.Subtract(_imageInput, _image, _imageResult); } else { CvInvoke.Add(_imageInput, _image, _imageResult); } } else { _imageBright = null; } } if (actionBrightCorrectData.bEqualize) { CvInvoke.EqualizeHist(_imageResult, _imageResult); } if (actionBrightCorrectData.bROIReset) { CvInvoke.cvResetImageROI(_imageResult); } CvInvoke.cvResetImageROI(_imageInput); sw.Stop(); }
void processFrameAndUpdateGUI(object sender, EventArgs arg) { Mat imgOriginal; imgOriginal = capWebcam.QueryFrame(); if (imgOriginal == null) { MessageBox.Show("unable to read from webcam" + Environment.NewLine + Environment.NewLine + "exiting program"); Environment.Exit(0); return; } Mat imgHSV = new Mat(imgOriginal.Size, DepthType.Cv8U, 3); Mat imgThreshLow = new Mat(imgOriginal.Size, DepthType.Cv8U, 1); Mat imgThreshHigh = new Mat(imgOriginal.Size, DepthType.Cv8U, 1); Mat imgThresh = new Mat(imgOriginal.Size, DepthType.Cv8U, 1); CvInvoke.CvtColor(imgOriginal, imgHSV, ColorConversion.Bgr2Hsv); CvInvoke.InRange(imgHSV, new ScalarArray(new MCvScalar(0, 155, 155)), new ScalarArray(new MCvScalar(18, 255, 255)), imgThreshLow); CvInvoke.InRange(imgHSV, new ScalarArray(new MCvScalar(165, 155, 155)), new ScalarArray(new MCvScalar(179, 255, 255)), imgThreshHigh); // //CvInvoke.FindContours(imgOutput, ); // CvInvoke.Add(imgThreshLow, imgThreshHigh, imgThresh); CvInvoke.GaussianBlur(imgThresh, imgThresh, new Size(3, 3), 0); Mat structuringElement = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(3, 3), new Point(-1, -1)); CvInvoke.Dilate(imgThresh, imgThresh, structuringElement, new Point(-1, -1), 1, BorderType.Default, new MCvScalar(0, 0, 0)); CvInvoke.Erode(imgThresh, imgThresh, structuringElement, new Point(-1, -1), 1, BorderType.Default, new MCvScalar(0, 0, 0)); CircleF[] circles = CvInvoke.HoughCircles(imgThresh, HoughType.Gradient, 2.0, imgThresh.Rows / 4, 100, 50, 10, 400); foreach (CircleF circle in circles) { if (txtXYRadius.Text != "") { // if we are not on the first line in the text box txtXYRadius.AppendText(Environment.NewLine); // then insert a new line char } txtXYRadius.AppendText("ball position x = " + circle.Center.X.ToString().PadLeft(4) + ", y = " + circle.Center.Y.ToString().PadLeft(4) + ", radius = " + circle.Radius.ToString("###.000").PadLeft(7)); txtXYRadius.ScrollToCaret(); // scroll down in text box so most recent line added (at the bottom) will be shown CvInvoke.Circle(imgOriginal, new Point((int)circle.Center.X, (int)circle.Center.Y), (int)circle.Radius, new MCvScalar(0, 0, 255), 2); CvInvoke.Circle(imgOriginal, new Point((int)circle.Center.X, (int)circle.Center.Y), 3, new MCvScalar(0, 255, 0), -1); } ibOriginal.Image = imgOriginal; ibThresh.Image = imgThresh; }
public List <Geometry> Parse() { geometries.Clear(); IsolateInstance(); AlignFaceWithBody(); AlignHandleWithBody(); #region save image //Debug.Log("saving images...."); if (geometries.Count == 0) { Debug.Log("No body detected!"); } Image <Rgb, byte> instance_img = new Image <Rgb, byte>(img.Width, img.Height); //instance_img.SetValue(new Rgba(255, 255, 255, 255)); instance_img.SetZero(); for (int j = 0; j < geometries.Count; j++) { Image <Rgb, byte> body_img = geometries[j].GetBodyImage(); Image <Rgb, byte> bound_img = geometries[j].GetBoundaryImage(); List <Image <Rgb, byte> > face_imgs = geometries[j].GetFaceImage(); List <Image <Rgb, byte> > handle_imgs = geometries[j].GetHandleImage(); //// checking if (body_img == null) { Debug.Log("No body!"); geometries.RemoveAt(j); j--; continue; } if (face_imgs.Count == 0) { Debug.Log("No Face!"); geometries.RemoveAt(j); j--; continue; } instance_img.SetValue(new Rgb(31, 120, 180), body_img.Convert <Gray, byte>()); for (int i = 0; i < face_imgs.Count; i++) { instance_img.SetValue(new Rgb(166, 206, 227), face_imgs[i].Convert <Gray, byte>()); } for (int i = 0; i < handle_imgs.Count; i++) { instance_img.SetValue(new Rgb(178, 223, 138), handle_imgs[i].Convert <Gray, byte>()); } CvInvoke.Add(instance_img, bound_img, instance_img); } //CvInvoke.AddWeighted(instance_img, 1.0, img, 0.8, 0, instance_img); img.Save("instance_mask.png"); instance_img.Save("instance_labelling.png"); #endregion save image return(this.geometries); }
private void buttonSharp_Click(object sender, EventArgs e) { if (imageConverted.Data == null) { imageConverted = new Image <Gray, byte>(imageOriginal.Cols, imageOriginal.Rows); } float[,] sharpMask = new float[, ] { { 0, -sharpFactor, 0 }, { -sharpFactor, 1 + 4 * sharpFactor, -sharpFactor }, { 0, -sharpFactor, 0 } }; for (int row = 1; row < imageOriginal.Rows - 1; row++) { for (int col = 1; col < imageOriginal.Cols - 1; col++) { //for sharpening Mask 3x3 int pixelConverted = 0; for (int rowm = 0; rowm < 3; rowm++) { for (int colm = 0; colm < 3; colm++) { pixelConverted += (int)(imageZoomed.Data[row - 1 + rowm, col - 1 + colm, 0] * sharpMask[rowm, colm]); // pixelConverted += (int)(imageOriginal.Data[row - 1 + rowm, col - 1 + colm, 0] *(1/sharpFactor)); } } if (pixelConverted > 255) { pixelConverted = 255; } else if (pixelConverted < 0) { pixelConverted = 0; } imageConverted.Data[row, col, 0] = (byte)pixelConverted; } }//Sharpening Done imageBoxShapen.Image = imageConverted; textBoxErrors.AppendText("Sharpening Done at factor " + Convert.ToString(sharpFactor) + "\n"); textBoxSharpFactor.Text = Convert.ToString(sharpFactor); try { var imageConverted2 = new Image <Gray, byte>(imageOriginal.Cols, imageOriginal.Rows); CvInvoke.Add(imageConverted, imageDifference, imageConverted2, null, DepthType.Default); imageBox2.Image = imageConverted2; } catch (Exception) {} }
//输入图像为单通道,预估的透射率图 引导图像为单通道,原图像的灰度图 输出图像为单通道,导向滤波后的透射图 private static Image <Gray, byte> GuidedFilter(Image <Gray, Byte> p, Image <Gray, Byte> I, int r, double e) { //int r, r; //w = h = 2 * r + 1; Image <Gray, byte> mean_p = new Image <Gray, byte>(p.Width, p.Height); Image <Gray, byte> mean_I = new Image <Gray, byte>(I.Width, I.Height); Image <Gray, byte> II = new Image <Gray, byte>(I.Width, I.Height); Image <Gray, byte> Ip = new Image <Gray, byte>(I.Width, I.Height); Image <Gray, byte> corr_II = new Image <Gray, byte>(I.Width, I.Height); Image <Gray, byte> corr_Ip = new Image <Gray, byte>(I.Width, I.Height); Image <Gray, byte> var_II = new Image <Gray, byte>(I.Width, I.Height); Image <Gray, byte> cov_Ip = new Image <Gray, byte>(I.Width, I.Height); Image <Gray, byte> a = new Image <Gray, byte>(I.Width, I.Height); Image <Gray, byte> b = new Image <Gray, byte>(I.Width, I.Height); Image <Gray, byte> mean_a = new Image <Gray, byte>(I.Width, I.Height); Image <Gray, byte> mean_b = new Image <Gray, byte>(I.Width, I.Height); Image <Gray, byte> q = new Image <Gray, byte>(p.Width, p.Height); //利用 boxFilter 计算均值 原始均值 导向均值 自相关均值 互相关均值 CvInvoke.BoxFilter(p, mean_p, DepthType.Cv8U, new Size(r, r), new Point(-1, -1), true, BorderType.Reflect101); CvInvoke.BoxFilter(I, mean_I, DepthType.Cv8U, new Size(r, r), new Point(-1, -1), true, BorderType.Reflect101); CvInvoke.Multiply(I, I, II); CvInvoke.Multiply(I, p, Ip); CvInvoke.BoxFilter(II, corr_II, DepthType.Cv8U, new Size(r, r), new Point(-1, -1), true, BorderType.Reflect101); CvInvoke.BoxFilter(Ip, corr_Ip, DepthType.Cv8U, new Size(r, r), new Point(-1, -1), true, BorderType.Reflect101); CvInvoke.Multiply(mean_I, mean_I, var_II); CvInvoke.Subtract(corr_II, var_II, var_II); CvInvoke.Multiply(mean_I, mean_p, cov_Ip); CvInvoke.Subtract(corr_Ip, cov_Ip, cov_Ip); CvInvoke.Divide(cov_Ip, var_II + e, a); CvInvoke.Multiply(a, mean_I, b); CvInvoke.Subtract(mean_p, b, b); CvInvoke.BoxFilter(a, mean_a, DepthType.Cv8U, new Size(r, r), new Point(-1, -1), true, BorderType.Reflect101); CvInvoke.BoxFilter(b, mean_b, DepthType.Cv8U, new Size(r, r), new Point(-1, -1), true, BorderType.Reflect101); CvInvoke.Multiply(mean_a, I, q); CvInvoke.Add(mean_b, q, q); return(q); }
public static Bitmap SuperPositionedImage(Bitmap Mask, Bitmap StandardImage) { Image <Hsv, Byte> GrayOrigin = new Image <Gray, Byte>(StandardImage).Convert <Hsv, Byte>(); Image <Hsv, Byte> HSVOrigin = new Image <Hsv, Byte>(StandardImage); Image <Gray, Byte> MaskImage = new Image <Gray, Byte>(Mask); Mat ResultHolder = new Mat(); CvInvoke.BitwiseAnd(GrayOrigin, HSVOrigin, ResultHolder, MaskImage); Mat TrueResultHolder = new Mat(); CvInvoke.Add(GrayOrigin, ResultHolder, TrueResultHolder, MaskImage); return(TrueResultHolder.Bitmap); }
private void btnRefesh_Click(object sender, EventArgs e) { Image <Gray, byte> image = new Image <Gray, byte>(_actionBrightCorrect.imageBright.Width, _actionBrightCorrect.imageBright.Height, new Gray(_actionBrightCorrectData.iInputScale)); if (_actionBrightCorrectData.bBrightDirect) { CvInvoke.Subtract(_actionBrightCorrect.imageBright, image, _actionBrightCorrect.imageBright); } else { CvInvoke.Add(_actionBrightCorrect.imageBright, image, _actionBrightCorrect.imageBright); } imageBox3.Image = _actionBrightCorrect.imageBright; }
public Image <Bgr, byte> GetDiagramImage() { this.ProcessingGerberImage.ROI = this.ROI; Image <Bgr, byte> Img = new Image <Bgr, byte>(this.ProcessingGerberImage.Size); using (Image <Gray, byte> imgTemp = this.ProcessingGerberImage.Copy()) using (Image <Bgr, byte> imgAdd = new Image <Bgr, byte>(Img.Size.Width, Img.Size.Height, new Bgr(0, 50, 0))) { CvInvoke.BitwiseNot(imgTemp, imgTemp); CvInvoke.CvtColor(this.ProcessingGerberImage, Img, Emgu.CV.CvEnum.ColorConversion.Gray2Bgr); CvInvoke.Add(Img, imgAdd, Img, mask: imgTemp); } return(Img); }
public Mat OverlayImageMats(Mat paramObscuredImgMat, Mat paramOverlayedImgMat) { var obscuredImgMat = paramObscuredImgMat.CreateNewHardCopyFromMat(); var overlayImgMat = paramOverlayedImgMat.CreateNewHardCopyFromMat(); var matToReturn = new Mat(); try { //Valami azt súgja, hogy //https://answers.opencv.org/question/24463/how-to-remove-black-background-from-grabcut-output-image-in-opencv-android/?comment=24786#comment-24786 //Ez kell majd var tempImgMat = new Mat(); var alpha = new Mat(); //Amúgy is fekete fehér nem? - most már biztos, mert már csak 1 csatornája van if (overlayImgMat.NumberOfChannels > 1) { CvInvoke.CvtColor(overlayImgMat, tempImgMat, ColorConversion.Bgr2Gray); } else { tempImgMat = overlayImgMat.CreateNewHardCopyFromMat(); } CvInvoke.Threshold(tempImgMat, alpha, 100, 255, ThresholdType.Binary); matToReturn = Mat.Zeros(overlayImgMat.Size.Width, overlayImgMat.Size.Height, overlayImgMat.Depth, overlayImgMat.NumberOfChannels); // Multiply the foreground with the alpha matte CvInvoke.Multiply(alpha, tempImgMat, tempImgMat); //new PopupImage(tempImgMat, "tempImgMat_2").Show(); // Multiply the background with ( 1 - alpha ) //http://www.emgu.com/wiki/files/3.0.0/document/html/e3f8abb7-3706-0ccd-46f0-8c57c2232585.htm var tempScalarAlphaMat = new MCvScalar(1.0, 1.0, 1.0, 1.0) - alpha; //var tempScalarAlphaMat = new MCvScalar(1.0) - alpha; var tempScalarAlphaScalar = tempScalarAlphaMat; //CvInvoke.Multiply(tempScalarAlphaScalar, obscuredImgMat, obscuredImgMat); CvInvoke.Multiply(obscuredImgMat, tempScalarAlphaScalar, obscuredImgMat); CvInvoke.Add(tempImgMat, obscuredImgMat, matToReturn); //https://www.learnopencv.com/alpha-blending-using-opencv-cpp-python/ //https://stackoverflow.com/questions/11958473/opencv-emgu-cv-compositing-images-with-alpha //https://github.com/karlphillip/GraphicsProgramming/blob/master/cvDisplacementMapFilter/main.cpp //https://stackoverflow.com/questions/45660427/emgu-c-sharp-opencv-make-color-black-transparent //https://www.learnopencv.com/alpha-blending-using-opencv-cpp-python/ } catch (Exception e) { MessageBox.Show(e.Message); } return(matToReturn); }
///'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' public static Mat maximizeContrast(Mat imgGrayscale) { Mat imgTopHat = new Mat(); Mat imgBlackHat = new Mat(); Mat imgGrayscalePlusTopHat = new Mat(); Mat imgGrayscalePlusTopHatMinusBlackHat = new Mat(); Mat structuringElement = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(3, 3), new Point(-1, -1)); CvInvoke.MorphologyEx(imgGrayscale, imgTopHat, MorphOp.Tophat, structuringElement, new Point(-1, -1), 1, BorderType.Default, new MCvScalar()); CvInvoke.MorphologyEx(imgGrayscale, imgBlackHat, MorphOp.Blackhat, structuringElement, new Point(-1, -1), 1, BorderType.Default, new MCvScalar()); CvInvoke.Add(imgGrayscale, imgTopHat, imgGrayscalePlusTopHat); CvInvoke.Subtract(imgGrayscalePlusTopHat, imgBlackHat, imgGrayscalePlusTopHatMinusBlackHat); return(imgGrayscalePlusTopHatMinusBlackHat); }
private void Color_FrameArrived(object sender, ColorFrameArrivedEventArgs e) { using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) { if (colorFrame != null) { if ((colorFrameDesc.Width == colorBitmap.PixelWidth) && (colorFrameDesc.Height == colorBitmap.PixelHeight)) { using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer()) { Mat img = new Mat(colorFrameDesc.Height, colorFrameDesc.Width, Emgu.CV.CvEnum.DepthType.Cv8U, 4); colorFrame.CopyConvertedFrameDataToIntPtr(img.DataPointer, (uint)(colorFrameDesc.Width * colorFrameDesc.Height * 4), ColorImageFormat.Bgra); CvInvoke.CvtColor(img, img, Emgu.CV.CvEnum.ColorConversion.Bgra2Gray); if (priorFrame != null) { CvInvoke.Subtract(priorFrame, img, priorFrame); CvInvoke.Threshold(priorFrame, priorFrame, 20, 255, Emgu.CV.CvEnum.ThresholdType.Binary); CvInvoke.GaussianBlur(priorFrame, priorFrame, new System.Drawing.Size(3, 3), 5); subtractedMats.Enqueue(priorFrame); } if (subtractedMats.Count > 4) { subtractedMats.Dequeue().Dispose(); Mat[] subtractedMatsArray = subtractedMats.ToArray(); culmativeFrame = subtractedMatsArray[0]; for (int i = 1; i < 4; i++) { CvInvoke.Add(culmativeFrame, subtractedMatsArray[i], culmativeFrame); } colorBitmap.Lock(); CopyMemory(colorBitmap.BackBuffer, culmativeFrame.DataPointer, (uint)(colorFrameDesc.Width * colorFrameDesc.Height)); colorBitmap.AddDirtyRect(new Int32Rect(0, 0, colorBitmap.PixelWidth, colorBitmap.PixelHeight)); colorBitmap.Unlock(); } priorFrame = img.Clone(); img.Dispose(); } } } } }
/// <summary> /// 计算小球之间的两两距离,输入为N X dim的位置信息矩阵,返回N X N的距离矩阵 /// 注:该方法对于4000个二维小球仅需800ms,如果采用for循环的话,需要100s左右 /// </summary> /// <param name="pos"></param> /// <returns></returns> public static void ComputeMatDist(Matrix <double> pos, ref Matrix <double> dists) { int num = pos.Rows; Matrix <double> xsquare = new Matrix <double>(num, pos.Cols); Matrix <double> xsquareSum = new Matrix <double>(num, 1); dists.SetZero(); CvInvoke.Multiply(pos, pos, xsquare); for (int i = 0; i < pos.Cols; i++) { CvInvoke.Add(xsquare.GetCol(i), xsquareSum, xsquareSum); } CvInvoke.Repeat(xsquareSum, 1, num, dists); dists = dists + dists.Transpose(); dists -= 2 * pos.Mul(pos.Transpose()); CvInvoke.Sqrt(dists, dists); }
private Mat MagnitudeInverse(Mat fftData) { Mat Real = new Mat(fftData.Size, DepthType.Cv32F, 1); Mat Imaginary = new Mat(fftData.Size, DepthType.Cv32F, 1); VectorOfMat channels = new VectorOfMat(); CvInvoke.Split(fftData, channels); Real = channels.GetOutputArray().GetMat(0); Imaginary = channels.GetOutputArray().GetMat(1); CvInvoke.Pow(Real, 2.0, Real); CvInvoke.Pow(Imaginary, 2.0, Imaginary); CvInvoke.Add(Real, Imaginary, Real); CvInvoke.Pow(Real, 0.5, Real); Console.WriteLine(Real); return(Real); }
private Mat Magnitude(Mat fftData) { Mat Real = new Mat(fftData.Size, DepthType.Cv32F, 1); Mat Imaginary = new Mat(fftData.Size, DepthType.Cv32F, 1); VectorOfMat channels = new VectorOfMat(); CvInvoke.Split(fftData, channels); //将多通道mat分离成几个单通道mat Real = channels.GetOutputArray().GetMat(0); Imaginary = channels.GetOutputArray().GetMat(1); CvInvoke.Pow(Real, 2.0, Real); CvInvoke.Pow(Imaginary, 2.0, Imaginary); CvInvoke.Add(Real, Imaginary, Real); CvInvoke.Pow(Real, 0.5, Real); Mat onesMat = Mat.Ones(Real.Rows, Real.Cols, DepthType.Cv32F, 1); CvInvoke.Add(Real, onesMat, Real); CvInvoke.Log(Real, Real); //求自然对数 return(Real); }
private Image <Gray, float> Mag_FFT2D(Image <Gray, float> SourceGrayDoubleImage) { //This function returnss the Magnitude image of the discrete Fourier Transform of the input image Image <Gray, float> FFTImage = new Image <Gray, float>(SourceGrayDoubleImage.Width, SourceGrayDoubleImage.Height); Matrix <float> dft = new Matrix <float>(SourceGrayDoubleImage.Rows, SourceGrayDoubleImage.Cols, 1); CvInvoke.Dft(SourceGrayDoubleImage, dft, Emgu.CV.CvEnum.DxtType.Forward, 0); Matrix <float> shiftDft = new Matrix <float>(dft.Size); cvShiftDFT(dft, shiftDft); shiftDft.GetSubRect(new Rectangle(Point.Empty, dft.Size)).CopyTo(FFTImage); Image <Gray, float> realDft = FFTImage.Rotate(180, new Gray(255), false); Image <Gray, float> q1 = realDft.GetSubRect(new Rectangle(0, 0, FFTImage.Width / 2, FFTImage.Height / 2)); Image <Gray, float> q2 = realDft.GetSubRect(new Rectangle(0, FFTImage.Height / 2, (FFTImage.Width / 2), FFTImage.Height / 2)); Image <Gray, float> d1 = FFTImage.GetSubRect(new Rectangle(0, 0, FFTImage.Width / 2, FFTImage.Height / 2)); Image <Gray, float> d2 = FFTImage.GetSubRect(new Rectangle(0, FFTImage.Height / 2, (FFTImage.Width / 2), FFTImage.Height / 2)); q1.GetSubRect(new Rectangle(Point.Empty, q1.Size)).CopyTo(d1); q2.GetSubRect(new Rectangle(Point.Empty, q2.Size)).CopyTo(d2); Image <Gray, float> imDft = FFTImage.Rotate(180, new Gray(255), false); Image <Gray, float> q3 = imDft.GetSubRect(new Rectangle(0, 0, FFTImage.Width / 2, FFTImage.Height / 2)); Image <Gray, float> q4 = imDft.GetSubRect(new Rectangle(0, FFTImage.Height / 2, (FFTImage.Width / 2), FFTImage.Height / 2)); Image <Gray, float> d3 = FFTImage.GetSubRect(new Rectangle(0, 0, FFTImage.Width / 2, FFTImage.Height / 2)); Image <Gray, float> d4 = FFTImage.GetSubRect(new Rectangle(0, FFTImage.Height / 2, (FFTImage.Width / 2), FFTImage.Height / 2)); q3.GetSubRect(new Rectangle(Point.Empty, q3.Size)).CopyTo(d3); q4.GetSubRect(new Rectangle(Point.Empty, q4.Size)).CopyTo(d4); //imDisplay("Real", realDft); //imDisplay("Img", imDft); CvInvoke.Pow(realDft, 2, realDft); CvInvoke.Pow(imDft, 2, imDft); CvInvoke.Add(realDft, imDft, FFTImage); CvInvoke.Pow(FFTImage, 0.5, FFTImage); return(FFTImage); }
public static Bitmap SuperPositionedImage(Bitmap Mask, Bitmap StandardImage, bool ColorOnly) { Image <Hsv, Byte> GrayOrigin = new Image <Gray, Byte>(StandardImage).Convert <Hsv, Byte>(); //Gray Copy Image <Hsv, Byte> HSVOrigin = new Image <Hsv, Byte>(StandardImage); //HSV Copy Image <Gray, Byte> MaskImageT = new Image <Gray, Byte>(Mask); //Mask Image <Hsv, Byte> MaskImage = new Image <Hsv, Byte>(Mask); //HSV Mask Image <Hsv, Byte> TrueImage = HSVOrigin.Copy(MaskImageT); //HSV through mask if (ColorOnly == false) { CvInvoke.Multiply(TrueImage, MaskImage, TrueImage); CvInvoke.Multiply(1 - MaskImage, GrayOrigin, GrayOrigin); CvInvoke.Add(TrueImage, GrayOrigin, TrueImage); Image <Hsv, Byte> DeathImage = new Image <Hsv, Byte>(StandardImage); CvInvoke.Add(TrueImage, HSVOrigin.Copy(MaskImageT), DeathImage); //Adding color and gray copy return(DeathImage.Bitmap); } else { return(TrueImage.Bitmap); } }
private void button3_Click(object sender, EventArgs e) { if (srcImg == null) { MessageBox.Show("请选择原图片!"); return; } Image <Bgr, Byte> dstImg = srcImg.CopyBlank(); Image <Bgr, Byte> dstImg2 = srcImg.CopyBlank(); CvInvoke.Sobel(srcImg, dstImg, DepthType.Default, 1, 0); imageBox2.Image = dstImg; dstImg2 = srcImg.Add(dstImg); imageBox3.Image = dstImg2; //显示梯度图2 Image <Bgr, Byte> grad_x = srcImg.CopyBlank(); Image <Bgr, Byte> grad_y = srcImg.CopyBlank(); Image <Bgr, Byte> abs_x = srcImg.CopyBlank(); Image <Bgr, Byte> abs_y = srcImg.CopyBlank(); Image <Bgr, Byte> gradImg = srcImg.CopyBlank(); CvInvoke.Sobel(srcImg, grad_x, DepthType.Default, 1, 0); CvInvoke.Sobel(srcImg, grad_y, DepthType.Default, 0, 1); //绝对值 CvInvoke.ConvertScaleAbs(grad_x, abs_x, 1, 0); CvInvoke.ConvertScaleAbs(grad_y, abs_y, 1, 0); //两个方向平方 abs_x.Pow(2); abs_y.Pow(2); //结果是梯度平方 CvInvoke.Add(abs_x, abs_y, gradImg); imageBox4.Image = gradImg; }
private static Mat ExtractTables(Mat image) { Mat horizontal = image.Clone(); Mat vertical = image.Clone(); int scale = 10; int horizontalSize = horizontal.Cols / scale; Mat horizontalStructure = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(horizontalSize, 1), new Point(-1, -1)); CvInvoke.Erode(horizontal, horizontal, horizontalStructure, new Point(-1, -1), 1, BorderType.Default, default(MCvScalar)); CvInvoke.Dilate(horizontal, horizontal, horizontalStructure, new Point(-1, -1), 1, BorderType.Default, default(MCvScalar)); int verticalSize = vertical.Rows / scale; Mat verticalStructure = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(1, verticalSize), new Point(-1, -1)); CvInvoke.Erode(vertical, vertical, verticalStructure, new Point(-1, -1), 1, BorderType.Default, default(MCvScalar)); CvInvoke.Dilate(vertical, vertical, verticalStructure, new Point(-1, -1), 1, BorderType.Default, default(MCvScalar)); var mask = new Mat(); CvInvoke.Add(horizontal, vertical, mask); var joints = new Mat(); CvInvoke.BitwiseAnd(horizontal, vertical, joints); var res = new Mat(); CvInvoke.AbsDiff(image, mask, res); CvInvoke.AbsDiff(res, joints, res); var element = CvInvoke.GetStructuringElement(ElementShape.Cross, new Size(3, 3), new Point(-1, -1)); CvInvoke.Erode(res, res, element, new Point(-1, -1), 1, BorderType.Default, default(MCvScalar)); CvInvoke.Dilate(res, res, element, new Point(-1, -1), 1, BorderType.Default, default(MCvScalar)); return(res); }
public void DFTFromMat(Bitmap Image) { Image <Bgr, Single> imageCV_spl = new Image <Bgr, Single>(Image); //приняли RGB Image <Gray, Single>[] imageCV = imageCV_spl.Split(); //разделили на 3 канала var image = new Mat(imageCV[0].Mat, ROI); var extended = new Mat(); CvInvoke.CopyMakeBorder(image, extended, 0, optRows - image.Rows, 0, optCols - image.Cols, BorderType.Constant); extended.ConvertTo(extended, DepthType.Cv32F); var vec = new VectorOfMat(extended, new Mat(extended.Size, DepthType.Cv32F, 1)); var complex = new Mat(); CvInvoke.Merge(vec, complex); CvInvoke.Dft(complex, complex, DxtType.Forward, 0); //dft CvInvoke.Split(complex, vec); //разделение реальной и комплексной матрицы var outReal = vec[0]; var outIm = vec[1]; CvInvoke.Pow(outReal, 2.0, outReal); //Re^2 CvInvoke.Pow(outIm, 2.0, outIm); //Im^2 CvInvoke.Add(outReal, outIm, outReal); //Re^2+Im^2 CvInvoke.Sqrt(outReal, outReal); //sqrt(Re^2+Im^2) CvInvoke.Log(outReal, outReal); //для нормального отображения outReal.CopyTo(finalmatrix); finalmatrix = finalmatrix.Clone(); SwitchQuadrants(ref finalmatrix); CvInvoke.Normalize(finalmatrix, finalmatrix, 0.0, 255.0, Emgu.CV.CvEnum.NormType.MinMax); RefreshDFT(finalmatrix.Mat); }
public void Norm4Disp(UMat i, UMat o, bool log = false) { if (log) //we add 1 to the image with only positive values to //prevent 0s being turned into -infinities by logging //set temporary variable to a 1 array { this.img32f1c.Create(i.Rows, i.Cols, i.Depth, i.NumberOfChannels); this.img32f1c.SetTo(new MCvScalar(1)); //elementwise add 1 to the input and store it in tmp //not in input because we don't wanna change the input, //not in output because output is 8-bit depth, we don't want //conversion as it takes too long. CvInvoke.Add(i, this.img32f1c, this.img32f1c); //log the tmp CvInvoke.Log(this.img32f1c, this.img32f1c); //normalise tmp and store in output. CvInvoke.Normalize(this.img32f1c, o, 0, 255, normType: NormType.MinMax, dType: DepthType.Cv8U); } else { CvInvoke.Normalize(i, o, 0, 255, normType: NormType.MinMax, dType: DepthType.Cv8U); } }
/// <summary> /// 对数组进行限幅 /// 每一行作为一个向量,对其幅值进行限幅 /// </summary> /// <param name="data">矩阵</param> /// <param name="max">最大值</param> private void ApplySaturation(ref Matrix <double> data, double max) { Matrix <double> square = new Matrix <double>(data.Rows, 3); //平方 Matrix <double> norm = new Matrix <double>(data.Rows, 1); //平方和 Matrix <double> res = new Matrix <double>(data.Rows, 1); //平方和的开方 CvInvoke.AccumulateSquare(data, square); CvInvoke.Add(square.GetCol(0), square.GetCol(1), norm); CvInvoke.Add(square.GetCol(2), norm, norm); CvInvoke.Sqrt(norm, res); for (int i = 0; i < data.Rows; i++) { if (res[i, 0] > max) { for (int j = 0; j < data.Cols; j++) { data[i, j] = data[i, j] * max / res[i, 0]; } } } }
/// <summary> /// 对速度限幅 /// 奈何ref参数不能放在lambda表达式中。。 /// </summary> private void ApplySaturationVel() { Matrix <double> square = new Matrix <double>(rtVel.Rows, 3); //平方 Matrix <double> norm = new Matrix <double>(rtVel.Rows, 1); //平方和 Matrix <double> res = new Matrix <double>(rtVel.Rows, 1); //平方和的开方 CvInvoke.AccumulateSquare(rtVel, square); CvInvoke.Add(square.GetCol(0), square.GetCol(1), norm); CvInvoke.Add(square.GetCol(2), norm, norm); CvInvoke.Sqrt(norm, res); Parallel.For(0, rtVel.Rows, (i) => { if (res[i, 0] > MaxVel) { for (int j = 0; j < rtVel.Cols; j++) { rtVel[i, j] = rtVel[i, j] * MaxVel / res[i, 0]; } } }); }