public static WriteableBitmap ToColor(this Pixel <int> src, ColorConversionCodes cc, byte[] buf = null, WriteableBitmap dst = null) { byte[] bufraw = null; if (buf == null) { buf = new byte[src.Width * src.Height * 3]; } if (bufraw == null) { bufraw = new byte[src.Width * src.Height]; } if (dst == null) { dst = new WriteableBitmap(src.Width, src.Height, 96, 96, PixelFormats.Bgr24, null); } var matrix = new float[] { 2, 0, 0, 0, 1, 0, 0, 0, 1.8F }; for (int i = 0; i < src.pixel.Length; i++) { var hoge = (byte)(src.pixel[i] > 255 ? 255 : src.pixel[i] < 0 ? 0 : src.pixel[i]); bufraw[i] = hoge; } using (Mat matmatrix = new Mat(3, 3, MatType.CV_32FC1, matrix)) using (Mat matraw = new Mat(src.Height, src.Width, MatType.CV_8UC1, bufraw)) using (Mat mat = new Mat(src.Height, src.Width, MatType.CV_8UC3, buf)) { Cv2.CvtColor(matraw, mat, cc); Cv2.Transform(mat, mat, matmatrix); WriteableBitmapConverter.ToWriteableBitmap(mat, dst); } return(dst); }
public Mat cvtColor(Mat img, ColorConversionCodes code, int dstCnt = 0) { var dst = new Mat(); cv2_native_api.imgproc_cvtColor(img.InputArray, dst.OutputArray, (int)code, dstCnt); return(dst); }
public static Mat GetCVMat <TElement>(this MatrixImage <TElement> image, ColorConversionCodes conversion) where TElement : unmanaged, IComparable <TElement> { Mat <TElement>[] mats = image.Split(false).Select(a => GetCVMat(a)).ToArray(); Mat <TElement> mat = new Mat <TElement>(image.Height, image.Width); Cv2.Merge(mats, mat); return(mat.CvtColor(conversion)); }
public static void CvtColor(ColorConversionCodes code = ColorConversionCodes.BGR2GRAY) { Glb.DrawMatAndHist0(Glb.matSrc); var matDst = Glb.matSrc.CvtColor(code); Glb.DrawMatAndHist1(matDst); Glb.DrawMatAndHist2(null); matDst.Dispose(); }
public static Mat Extract(Mat srcMat, ColorConversionCodes code, ColorVariation color) { // int a = Enum.GetNames(typeof(ColorConversion)).Length; ColorTable table; if (code == ColorConversionCodes.BGR2HSV) { switch (color) { // case ColorVariation.Black: // table = new ColorTable(0, 0, 0, 0, 0, 0); // break; // case ColorVariation.DarkRed: // table = new ColorTable(0, 0, 0, 0, 0, 0); // break; case ColorVariation.Green: table = new ColorTable(50, 70, 80, 255, 0, 255); break; // case ColorVariation.Orange: // table = new ColorTable(0, 0, 0, 0, 0, 0); // break; case ColorVariation.Red: table = new ColorTable(170, 10, 80, 255, 0, 255); break; case ColorVariation.Skin: table = new ColorTable(0, 10, 80, 255, 0, 255); break; // case ColorVariation.White: // table = new ColorTable(0, 0, 0, 0, 0, 0); // break; default: table = new ColorTable(0, 0, 0, 0, 0, 0); break; } return Extract( srcMat, code, table.ch1Lower, table.ch1Upper, table.ch2Lower, table.ch2Upper, table.ch3Lower, table.ch3Upper ); } else { throw new NotImplementedException("HSV 値以外を用いた抽出は実装されていません"); } }
/// <summary> /// Converts an image from Bayer pattern to RGB or grayscale. /// </summary> /// <param name="src">Source image (8-bit or 16-bit single channel).</param> /// <param name="dst">Destination image.</param> /// <param name="code">Color space conversion code (see the description below). /// The function can do the following transformations: /// - Demosaicing using bilinear interpolation /// > - COLOR_BayerBG2GRAY , COLOR_BayerGB2GRAY , COLOR_BayerRG2GRAY , COLOR_BayerGR2GRAY /// > - COLOR_BayerBG2BGR , COLOR_BayerGB2BGR , COLOR_BayerRG2BGR , COLOR_BayerGR2BGR /// - Demosaicing using Malvar-He-Cutler algorithm(@cite MHT2011) /// > - COLOR_BayerBG2GRAY_MHT , COLOR_BayerGB2GRAY_MHT , COLOR_BayerRG2GRAY_MHT , /// > COLOR_BayerGR2GRAY_MHT /// > - COLOR_BayerBG2BGR_MHT , COLOR_BayerGB2BGR_MHT , COLOR_BayerRG2BGR_MHT , /// > COLOR_BayerGR2BGR_MHT</param> /// <param name="dcn">Number of channels in the destination image. If the parameter is 0, the number of the /// channels is derived automatically from src and the code.</param> /// <param name="stream">Stream for the asynchronous version.</param> public static void demosaicing( InputArray src, OutputArray dst, ColorConversionCodes code, int dcn = -1, Stream stream = null) { if (src == null) { throw new ArgumentNullException(nameof(src)); } if (dst == null) { throw new ArgumentNullException(nameof(dst)); } src.ThrowIfDisposed(); dst.ThrowIfNotReady(); NativeMethods.cuda_imgproc_demosaicing(src.CvPtr, dst.CvPtr, (int)code, dcn, stream?.CvPtr ?? Stream.Null.CvPtr); GC.KeepAlive(src); GC.KeepAlive(dst); dst.Fix(); }
public static WriteableBitmap ToColor(this Pixel <float> src, ColorConversionCodes cc, byte[] buf = null, WriteableBitmap dst = null) { byte[] bufraw = null; if (buf == null) { buf = new byte[src.Width * src.Height * 3]; } if (bufraw == null) { bufraw = new byte[src.Width * src.Height]; } if (dst == null) { dst = new WriteableBitmap(src.Width, src.Height, 96, 96, PixelFormats.Bgr24, null); } var matrix = new float[] { 2, 0, 0, 0, 1, 0, 0, 0, 1.8F }; using (Mat matmatrix = new Mat(3, 3, MatType.CV_32FC1, matrix)) using (Mat matraw = new Mat(src.Height, src.Width, MatType.CV_8UC1, bufraw)) using (Mat mat = new Mat(src.Height, src.Width, MatType.CV_8UC3, buf)) { int c = 0; for (int y = 0; y < src.Height; y++) { for (int x = 0; x < src.Width; x++) { var hoge = src[x, y].ConvertToByte(); bufraw[c++] = hoge; } } Cv2.CvtColor(matraw, mat, cc); Cv2.Transform(mat, mat, matmatrix); WriteableBitmapConverter.ToWriteableBitmap(mat, dst); } return(dst); }
public static void ConvertColor(this Mat self, ColorConversionCodes convert) { ConvertColor(self, self, convert); }
public static Mat ExtractMask(Mat srcMat, ColorConversionCodes code, ColorTable table) { return ExtractMask( srcMat, code, table.ch1Lower, table.ch1Upper, table.ch2Lower, table.ch2Upper, table.ch3Lower, table.ch3Upper ); }
public static void ConvertColor(this Mat self, Mat output, ColorConversionCodes convert) { Cv2.CvtColor(self, output, convert); }
/// <summary> /// 画像の色空間を変換します. /// </summary> /// <param name="src">8ビット符号なし整数型,16ビット符号なし整数型,または単精度浮動小数型の入力画像</param> /// <param name="dst">src と同じサイズ,同じタイプの出力画像</param> /// <param name="code">色空間の変換コード.</param> /// <param name="dstCn">出力画像のチャンネル数.この値が 0 の場合,チャンネル数は src と code から自動的に求められます</param> #else /// <summary> /// Converts image from one color space to another /// </summary> /// <param name="src">The source image, 8-bit unsigned, 16-bit unsigned or single-precision floating-point</param> /// <param name="dst">The destination image; will have the same size and the same depth as src</param> /// <param name="code">The color space conversion code</param> /// <param name="dstCn">The number of channels in the destination image; if the parameter is 0, the number of the channels will be derived automatically from src and the code</param> #endif public static void CvtColor(InputArray src, OutputArray dst, ColorConversionCodes code, int dstCn = 0) { if (src == null) throw new ArgumentNullException(nameof(src)); if (dst == null) throw new ArgumentNullException(nameof(dst)); src.ThrowIfDisposed(); dst.ThrowIfNotReady(); NativeMethods.imgproc_cvtColor(src.CvPtr, dst.CvPtr, (int)code, dstCn); GC.KeepAlive(src); dst.Fix(); }
public static Mat ExtractMask(Mat srcMat, ColorConversionCodes code, int ch1Lower, int ch1Upper, int ch2Lower, int ch2Upper, int ch3Lower, int ch3Upper) { if (srcMat == null) throw new ArgumentNullException("srcMat"); var colorMat = srcMat.CvtColor(code); var lut = new Mat(256, 1, MatType.CV_8UC3); var lower = new int[3] { ch1Lower, ch2Lower, ch3Lower }; var upper = new int[3] { ch1Upper, ch2Upper, ch3Upper }; // cv::Mat_<cv::Vec3b> var mat3 = new MatOfByte3(lut); var indexer = mat3.GetIndexer(); for (int i = 0; i < 256; i++) { var color = indexer[i]; byte temp; for (int k = 0; k < 3; k++) { if (lower[k] <= upper[k]) { if ((lower[k] <= i) && (i <= upper[k])) { temp = 255; } else { temp = 0; } } else { if ((i <= upper[k]) || (lower[k] <= i)) { temp = 255; } else { temp = 0; } } color[k] = temp; } indexer[i] = color; } Cv2.LUT(colorMat, lut, colorMat); var channelMat = colorMat.Split(); var maskMat = new Mat(); Cv2.BitwiseAnd(channelMat[0], channelMat[1], maskMat); Cv2.BitwiseAnd(maskMat, channelMat[2], maskMat); return maskMat; }
public static Mat Extract(Mat srcMat, ColorConversionCodes code, int ch1Lower, int ch1Upper, int ch2Lower, int ch2Upper, int ch3Lower, int ch3Upper) { var maskMat = ExtractMask(srcMat, code, ch1Lower, ch1Upper, ch2Lower, ch2Upper, ch3Lower, ch3Upper ); srcMat.CopyTo(maskMat, maskMat); return maskMat; }
public static Mat ExtractRegularFormat(Mat srcMat, ColorConversionCodes code, ColorTable table) { /* Console.WriteLine("{0} : {1} : {2} : {3} : {4} : {5}", ch1LowerAngle / 2, ch1UpperAngle / 2, ch2LowerPer * 255 / 100, ch2UpperPer * 255 / 100, ch3LowerPer * 255 / 100, ch3UpperPer * 255 / 100);*/ return ExtractRegularFormat( srcMat, code, table.ch1Lower, table.ch1Upper, table.ch2Lower, table.ch2Upper, table.ch3Lower, table.ch3Upper ); }
public override void Demosaic(int w, int h, ushort[] img16uc1, ushort[] img16uc3, byte[] img8uc3, float[] m, ColorConversionCodes bayerpattern) { using (Mat mat16uc1 = new Mat(h, w, MatType.CV_16UC1, img16uc1)) using (Mat mat16uc3 = new Mat(h, w, MatType.CV_16UC3, img16uc3)) using (Mat mat8uc3 = new Mat(h, w, MatType.CV_8UC3, img8uc3)) using (Mat matrix = new Mat(3, 3, MatType.CV_32FC1, m)) { Quadrant(img16uc1, img16uc3, w, h); //mat16uc3.ConvertTo(mat8uc3, MatType.CV_8UC3); } }
public CvtColorLayer(ColorConversionCodes code, int dstCn) { Code = code; DstCn = dstCn; }
//3. 彩色图像灰度处理 public static Mat GrayProcessingMethod(Mat inMat, Mat outMat, ColorConversionCodes codeType, int dstCn) { Cv2.CvtColor(inMat, outMat, codeType, dstCn); return(outMat); }
public override void Demosaic(int w, int h, ushort[] img16uc1, ushort[] img16uc3, byte[] img8uc3, float[] m, ColorConversionCodes bayerpattern) { using (Mat mat16uc1 = new Mat(h, w, MatType.CV_16UC1, img16uc1)) using (Mat mat16uc3 = new Mat(h, w, MatType.CV_16UC3, img16uc3)) using (Mat mat8uc3 = new Mat(h, w, MatType.CV_8UC3, img8uc3)) using (Mat matrix = new Mat(3, 3, MatType.CV_32FC1, m)) { switch (bayerpattern) { case ColorConversionCodes.BayerBG2BGR: Cv2.CvtColor(mat16uc1, mat16uc3, OpenCvSharp.ColorConversionCodes.BayerBG2BGR); break; case ColorConversionCodes.BayerGB2BGR: Cv2.CvtColor(mat16uc1, mat16uc3, OpenCvSharp.ColorConversionCodes.BayerGB2BGR); break; case ColorConversionCodes.BayerGR2BGR: Cv2.CvtColor(mat16uc1, mat16uc3, OpenCvSharp.ColorConversionCodes.BayerGR2BGR); break; case ColorConversionCodes.BayerRG2BGR: Cv2.CvtColor(mat16uc1, mat16uc3, OpenCvSharp.ColorConversionCodes.BayerRG2BGR); break; default: Cv2.CvtColor(mat16uc1, mat16uc3, OpenCvSharp.ColorConversionCodes.BayerRG2BGR); break; } Cv2.Transform(mat16uc3, mat16uc3, matrix); //mat16uc3.ConvertTo(mat8uc3, MatType.CV_8UC3); } }
/// <summary> /// 画像の色空間を変換します. /// </summary> /// <param name="code">色空間の変換コード.</param> /// <param name="dstCn">出力画像のチャンネル数.この値が 0 の場合,チャンネル数は src と code から自動的に求められます</param> /// <returns>src と同じサイズ,同じタイプの出力画像</returns> #else /// <summary> /// Converts image from one color space to another /// </summary> /// <param name="code">The color space conversion code</param> /// <param name="dstCn">The number of channels in the destination image; if the parameter is 0, the number of the channels will be derived automatically from src and the code</param> /// <returns>The destination image; will have the same size and the same depth as src</returns> #endif public Mat CvtColor(ColorConversionCodes code, int dstCn = 0) { var dst = new Mat(); Cv2.CvtColor(this, dst, code, dstCn); return dst; }
public override void Demosaic(int w, int h, ushort[] img16uc1, ushort[] img16uc3, byte[] img8uc3, float[] m, ColorConversionCodes bayerpattern) { using (Mat mat16uc1 = new Mat(h, w, MatType.CV_16UC1, img16uc1)) using (Mat mat16uc3 = new Mat(h, w, MatType.CV_16UC3, img16uc3)) using (Mat mat8uc3 = new Mat(h, w, MatType.CV_8UC3, img8uc3)) using (Mat matrix = new Mat(3, 3, MatType.CV_32FC1, m)) { switch (bayerpattern) { case ColorConversionCodes.BayerBG2BGR: QuadColorBG(img16uc1, img16uc3, w, h); break; case ColorConversionCodes.BayerGB2BGR: QuadColorGB(img16uc1, img16uc3, w, h); break; case ColorConversionCodes.BayerGR2BGR: QuadColorGR(img16uc1, img16uc3, w, h); break; case ColorConversionCodes.BayerRG2BGR: QuadColorRG(img16uc1, img16uc3, w, h); break; default: QuadColorRG(img16uc1, img16uc3, w, h); break; } //mat16uc3.ConvertTo(mat8uc3, MatType.CV_8UC3); } }
public CvtColorImagePreprocessor(ColorConversionCodes colorConversionCodes) { ColorConversionCodes = colorConversionCodes; }
/// <summary> /// oilPainting. /// See the book @cite Holzmann1988 for details. /// </summary> /// <param name="src">Input three-channel or one channel image (either CV_8UC3 or CV_8UC1)</param> /// <param name="dst">Output image of the same size and type as src.</param> /// <param name="size">neighbouring size is 2-size+1</param> /// <param name="dynRatio">image is divided by dynRatio before histogram processing</param> /// <param name="code">color space conversion code(see ColorConversionCodes). Histogram will used only first plane</param> public static void OilPainting(InputArray src, OutputArray dst, int size, int dynRatio, ColorConversionCodes code = ColorConversionCodes.BGR2GRAY) { if (src == null) { throw new ArgumentNullException(nameof(src)); } if (dst == null) { throw new ArgumentNullException(nameof(dst)); } src.ThrowIfDisposed(); dst.ThrowIfNotReady(); NativeMethods.HandleException( NativeMethods.xphoto_oilPainting(src.CvPtr, dst.CvPtr, size, dynRatio, (int)code)); GC.KeepAlive(src); GC.KeepAlive(dst); }
public static ColorConvart COLORCONVERT(ColorConversionCodes code) => new ColorConvart(code);