123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760 |
- using OpenCvSharp;
- using PaintDotNet.Base;
- using System;
- using System.Collections.Generic;
- namespace PaintDotNet.Adjust
- {
- /// <summary>
- /// 图像间操作
- /// </summary>
- public class InterImageIntent
- {
- /// <summary>
- /// 图像间操作-最小值
- /// </summary>
- /// <param name="src"></param>
- /// <returns></returns>
- public static Mat ImageMin(Mat src, List<Args> lists)
- {
- Mat dst = new Mat();
- Mat mat1 = new Mat();
- Mat mat2 = new Mat();
- try
- {
- for (int i = 0; i < lists.Count; i++)
- {
- Args args = lists[i];
- switch (args.Key)
- {
- case "Src2":
- mat1 = (Mat)args.Value;
- break;
- default:
- break;
- }
- }
- int w = src.Width;
- int h = src.Height;
- int w1 = mat1.Width;
- int h1 = mat1.Height;
- mat2 = new Mat(h, w, src.Type(), Scalar.All(0));
- Rect rect = new Rect(0, 0, Math.Min(w, w1), Math.Min(h, h1));
- Mat tempMat = mat1.Clone(rect);
- //复制用户选中的区域到原图像
- Mat mask = tempMat.CvtColor(ColorConversionCodes.RGBA2GRAY);
- Mat pos = new Mat(mat2, rect);
- tempMat.CopyTo(pos, mask);
- //Cv2.CopyMakeBorder(src, src, 0, maxh - src.Height, 0, maxw - src.Width, BorderTypes.Isolated);
- //Cv2.CopyMakeBorder(mat1, mat1, 0, maxh - mat1.Height, 0, maxw - mat1.Width, BorderTypes.Isolated);
- Mat tempMat0 = src.Clone(rect);
- //复制用户选中的区域到原图像
- Mat mask0 = tempMat0.CvtColor(ColorConversionCodes.RGBA2GRAY);
- Mat mat0 = new Mat(h, w, src.Type(), Scalar.All(/*255*/0));
- Mat pos0 = new Mat(mat0, rect);
- tempMat0.CopyTo(pos0, mask0);
- Cv2.Min(mat0, mat2, dst);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- src.CopyTo(dst);
- }
- finally
- {
- if (src != null) dst.CopyTo(src);
- if (dst != null) dst.Dispose();
- }
- return src;
- }
- /// <summary>
- /// 图像间操作-最大值
- /// </summary>
- /// <param name="src"></param>
- /// <returns></returns>
- public static Mat ImageMax(Mat src, List<Args> lists)
- {
- Mat dst = new Mat();
- Mat mat1 = new Mat();
- Mat mat2 = new Mat();
- try
- {
- for (int i = 0; i < lists.Count; i++)
- {
- Args args = lists[i];
- switch (args.Key)
- {
- case "Src2":
- mat1 = (Mat)args.Value;
- break;
- default:
- break;
- }
- }
- int w = src.Width;
- int h = src.Height;
- int w1 = mat1.Width;
- int h1 = mat1.Height;
- mat2 = new Mat(h, w, src.Type(), Scalar.All(0));
- Rect rect = new Rect(0, 0, Math.Min(w, w1), Math.Min(h, h1));
- Mat tempMat = mat1.Clone(rect);
- //复制用户选中的区域到原图像
- Mat mask = tempMat.CvtColor(ColorConversionCodes.RGBA2GRAY);
- Mat pos = new Mat(mat2, rect);
- tempMat.CopyTo(pos, mask);
- //Cv2.CopyMakeBorder(src, src, 0, maxh - src.Height, 0, maxw - src.Width, BorderTypes.Isolated);
- //Cv2.CopyMakeBorder(mat1, mat1, 0, maxh - mat1.Height, 0, maxw - mat1.Width, BorderTypes.Isolated);
- Mat tempMat0 = src.Clone(rect);
- //复制用户选中的区域到原图像
- Mat mask0 = tempMat0.CvtColor(ColorConversionCodes.RGBA2GRAY);
- Mat mat0 = new Mat(h, w, src.Type(), Scalar.All(/*255*/0));
- Mat pos0 = new Mat(mat0, rect);
- tempMat0.CopyTo(pos0, mask0);
- Cv2.Max(mat0, mat2, dst);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- src.CopyTo(dst);
- }
- finally
- {
- if (src != null) dst.CopyTo(src);
- if (dst != null) dst.Dispose();
- }
- return src;
- }
- /// <summary>
- /// 图像间操作-指数
- /// </summary>
- /// <param name="src"></param>
- /// <returns></returns>
- public static Mat ImageIndex(Mat src)
- {
- Mat dst = new Mat();
- try
- {
- Mat resultImage = new Mat(src.Size(), src.Type());
- src.ConvertTo(resultImage, MatType.CV_64F, 1.0 / 255, 0);
- Cv2.Pow(resultImage, 5.5, resultImage);
- Cv2.ConvertScaleAbs(resultImage * 255, resultImage);
- Cv2.Normalize(resultImage, resultImage, 1, 255, NormTypes.MinMax);
- resultImage.CopyTo(dst);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- src.CopyTo(dst);
- }
- finally
- {
- if (src != null) dst.CopyTo(src);
- if (dst != null) dst.Dispose();
- }
- return src;
- }
- /// <summary>
- /// 图像间操作-对数
- /// </summary>
- /// <param name="src"></param>
- /// <returns></returns>
- public static Mat ImageLogarithm(Mat src)
- {
- bool isFourChannels = false;
- if (src.Channels() == 4)
- {
- Cv2.CvtColor(src, src, OpenCvSharp.ColorConversionCodes.BGRA2BGR);
- isFourChannels = true;
- }
- Mat dst = new Mat();
- try
- {
- Mat resultImage = new Mat(src.Size(), src.Type());
- for (int y = 0; y < src.Height; y++)//行
- {
- for (int x = 0; x < src.Width; x++)//列
- {
- Vec3b pix = src.At<Vec3b>(y, x);
- pix[0] = (byte)(Math.Log(pix[0] + 1) * 9);
- pix[1] = (byte)(Math.Log(pix[1] + 1) * 9);
- pix[2] = (byte)(Math.Log(pix[2] + 1) * 9);
- //if (pix[0] > 255) pix[0] = 255;
- //if (pix[0] < 0) pix[0] = 0;
- //if (pix[1] > 255) pix[1] = 255;
- //if (pix[1] < 0) pix[1] = 0;
- //if (pix[2] > 255) pix[2] = 255;
- //if (pix[2] < 0) pix[2] = 0;
- resultImage.Set<Vec3b>(y, x, pix);
- }
- }
- Cv2.ConvertScaleAbs(resultImage, resultImage);
- Cv2.Normalize(resultImage, resultImage, 1, 255, NormTypes.MinMax);
- resultImage.CopyTo(dst);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- src.CopyTo(dst);
- }
- finally
- {
- if (src != null) dst.CopyTo(src);
- if (dst != null) dst.Dispose();
- }
- if (isFourChannels)
- {
- Cv2.CvtColor(src, src, OpenCvSharp.ColorConversionCodes.BGR2BGRA);
- }
- return src;
- }
- /// <summary>
- /// 图像间操作-平方根
- /// </summary>
- /// <param name="src"></param>
- /// <returns></returns>
- public static Mat ImageSquareBoot(Mat src)
- {
- Mat dst = new Mat();
- bool isFourChannels = false;
- if (src.Channels() == 4)
- {
- Cv2.CvtColor(src, src, OpenCvSharp.ColorConversionCodes.BGRA2BGR);
- isFourChannels = true;
- }
- try
- {
- Mat resultImage = new Mat(src.Size(), src.Type());
- for (int y = 0; y < src.Height; y++)//行
- {
- for (int x = 0; x < src.Width; x++)//列
- {
- Vec3b pix = src.At<Vec3b>(y, x);
- pix[0] = (byte)(Math.Sqrt(pix[0]) * 15);
- pix[1] = (byte)(Math.Sqrt(pix[1]) * 15);
- pix[2] = (byte)(Math.Sqrt(pix[2]) * 15);
- //if (pix[0] > 255) pix[0] = 255;
- //if (pix[0] < 0) pix[0] = 0;
- //if (pix[1] > 255) pix[1] = 255;
- //if (pix[1] < 0) pix[1] = 0;
- //if (pix[2] > 255) pix[2] = 255;
- //if (pix[2] < 0) pix[2] = 0;
- resultImage.Set<Vec3b>(y, x, pix);
- }
- }
- Cv2.ConvertScaleAbs(resultImage, resultImage);
- Cv2.Normalize(resultImage, resultImage, 1, 255, NormTypes.MinMax);
- resultImage.CopyTo(dst);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- src.CopyTo(dst);
- }
- finally
- {
- if (src != null) dst.CopyTo(src);
- if (dst != null) dst.Dispose();
- }
- if (isFourChannels)
- {
- Cv2.CvtColor(src, src, OpenCvSharp.ColorConversionCodes.BGR2BGRA);
- }
- return src;
- }
- /// <summary>
- /// 图像间操作-平方
- /// </summary>
- /// <param name="src"></param>
- /// <returns></returns>
- public static Mat ImageSquare(Mat src)
- {
- Mat dst = new Mat();
- try
- {
- Mat resultImage = new Mat(src.Size(), src.Type());
- src.ConvertTo(resultImage, MatType.CV_64F, 1.0 / 255, 0);
- Cv2.Pow(resultImage, 2, resultImage);
- Cv2.ConvertScaleAbs(resultImage * 255, resultImage);
- Cv2.Normalize(resultImage, resultImage, 1, 255, NormTypes.MinMax);
- resultImage.CopyTo(dst);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- src.CopyTo(dst);
- }
- finally
- {
- if (src != null) dst.CopyTo(src);
- if (dst != null) dst.Dispose();
- }
- return src;
- }
- /// <summary>
- /// 图像间操作-平均
- /// </summary>
- /// <param name="src"></param>
- /// <returns></returns>
- public static Mat ImageAverage(Mat src, List<Args> lists)
- {
- Mat dst = new Mat();
- Mat mat1 = new Mat();
- Mat mat2;// = new Mat();
- try
- {
- for (int i = 0; i < lists.Count; i++)
- {
- Args args = lists[i];
- switch (args.Key)
- {
- case "Src2":
- mat1 = (Mat)args.Value;
- break;
- default:
- break;
- }
- }
- int w = src.Width;
- int h = src.Height;
- int w1 = mat1.Width;
- int h1 = mat1.Height;
- mat2 = new Mat(h, w, src.Type(), Scalar.All(0));
- Rect rect = new Rect(0, 0, Math.Min(w, w1), Math.Min(h, h1));
- Mat tempMat = mat1.Clone(rect);
- //复制用户选中的区域到原图像
- Mat mask = tempMat.CvtColor(ColorConversionCodes.RGBA2GRAY);
- Mat pos = new Mat(mat2, rect);
- tempMat.CopyTo(pos, mask);
- //Cv2.CopyMakeBorder(src, src, 0, maxh - src.Height, 0, maxw - src.Width, BorderTypes.Isolated);
- //Cv2.CopyMakeBorder(mat1, mat1, 0, maxh - mat1.Height, 0, maxw - mat1.Width, BorderTypes.Isolated);
- Mat tempMat0 = src.Clone(rect);
- //复制用户选中的区域到原图像
- Mat mask0 = tempMat0.CvtColor(ColorConversionCodes.RGBA2GRAY);
- Mat mat0 = new Mat(h, w, src.Type(), Scalar.All(/*255*/0));
- Mat pos0 = new Mat(mat0, rect);
- tempMat0.CopyTo(pos0, mask0);
- Cv2.AddWeighted(src/*mat0*/, 0.5, mat2, 0.5, 1, dst);// (mat0, mat2, dst);
- //Cv2.Add(mat0, mat2, dst);
- //dst = dst / 2;
- ////Cv2.Normalize(dst, dst, 1, 255, NormTypes.MinMax);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- src.CopyTo(dst);
- }
- finally
- {
- if (src != null) dst.CopyTo(src);
- if (dst != null) dst.Dispose();
- }
- return src;
- }
- /// <summary>
- /// 图像间操作-除
- /// </summary>
- /// <param name="src"></param>
- /// <returns></returns>
- public static Mat ShadingDivide(Mat src, List<Args> lists)
- {
- Mat dst = new Mat();
- Mat mat1 = new Mat();
- Mat mat2;// = new Mat();
- double factor = 1;
- bool isFourChannels = false;
- if (src.Channels() == 4)
- {
- Cv2.CvtColor(src, src, OpenCvSharp.ColorConversionCodes.BGRA2BGR);
- isFourChannels = true;
- }
- try
- {
- for (int i = 0; i < lists.Count; i++)
- {
- Args args = lists[i];
- switch (args.Key)
- {
- case "Src2":
- var mat = (Mat)args.Value;
- if (mat.Channels() == 4)
- {
- Cv2.CvtColor(mat, mat, OpenCvSharp.ColorConversionCodes.BGRA2BGR);
- }
- mat1 = mat;
- break;
- case "Factor":
- if (double.TryParse(args.Value.ToString(), out factor))
- {
- if (factor == 0)
- {
- factor = 1;
- }
- else
- {
- factor = 1.0 / factor;
- }
- }
- break;
- default:
- break;
- }
- }
- int w = src.Width;
- int h = src.Height;
- int w1 = mat1.Width;
- int h1 = mat1.Height;
- mat2 = new Mat(h, w, src.Type(), Scalar.All(0));
- Rect rect = new Rect(0, 0, Math.Min(w, w1), Math.Min(h, h1));
- Mat tempMat = mat1.Clone(rect);
- //复制用户选中的区域到原图像
- Mat mask = tempMat.CvtColor(ColorConversionCodes.RGBA2GRAY);
- Mat pos = new Mat(mat2, rect);
- tempMat.CopyTo(pos, mask);
- //Cv2.CopyMakeBorder(src, src, 0, maxh - src.Height, 0, maxw - src.Width, BorderTypes.Isolated);
- //Cv2.CopyMakeBorder(mat1, mat1, 0, maxh - mat1.Height, 0, maxw - mat1.Width, BorderTypes.Isolated);
- Mat tempMat0 = src.Clone(rect);
- //复制用户选中的区域到原图像
- Mat mask0 = tempMat0.CvtColor(ColorConversionCodes.RGBA2GRAY);
- Mat mat0 = new Mat(h, w, src.Type(), Scalar.All(/*255*/0));
- Mat pos0 = new Mat(mat0, rect);
- tempMat0.CopyTo(pos0, mask0);
- Cv2.Divide(src/*mat0*/, mat2, dst, factor);
- //Cv2.Normalize(dst, dst, 1, 255, NormTypes.MinMax);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- src.CopyTo(dst);
- }
- finally
- {
- if (src != null) dst.CopyTo(src);
- if (dst != null) dst.Dispose();
- }
- if (isFourChannels)
- {
- Cv2.CvtColor(src, src, OpenCvSharp.ColorConversionCodes.BGR2BGRA);
- }
- return src;
- }
- /// <summary>
- /// 图像间操作-乘
- /// </summary>
- /// <param name="src"></param>
- /// <returns></returns>
- public static Mat ShadingMultiply(Mat src, List<Args> lists)
- {
- bool isFourChannels = false;
- if(src.Channels() == 4)
- {
- Cv2.CvtColor(src, src, OpenCvSharp.ColorConversionCodes.BGRA2BGR);
- isFourChannels = true;
- }
- Mat dst = new Mat();
- Mat mat1 = new Mat();
- Mat mat2;//待优化: = new Mat();
- double factor = 1;
- try
- {
- for (int i = 0; i < lists.Count; i++)
- {
- Args args = lists[i];
- switch (args.Key)
- {
- case "Src2":
- var mat = (Mat)args.Value;
- if (mat.Channels() == 4)
- {
- Cv2.CvtColor(mat, mat, OpenCvSharp.ColorConversionCodes.BGRA2BGR);
- }
- mat1 = mat;
- break;
- case "Factor":
- if (double.TryParse(args.Value.ToString(), out factor))
- {
- if (factor == 0)
- {
- factor = 1;
- }
- else
- {
- factor = 1.0 / factor;
- }
- }
- break;
- default:
- break;
- }
- }
- int w = src.Width;
- int h = src.Height;
- int w1 = mat1.Width;
- int h1 = mat1.Height;
- mat2 = new Mat(h, w, src.Type(), Scalar.All(0));
- Rect rect = new Rect(0, 0, Math.Min(w, w1), Math.Min(h, h1));
- Mat tempMat = mat1.Clone(rect);
- //复制用户选中的区域到原图像
- Mat mask = tempMat.CvtColor(ColorConversionCodes.RGBA2GRAY);
- Mat pos = new Mat(mat2, rect);
- tempMat.CopyTo(pos, mask);
- //Cv2.CopyMakeBorder(src, src, 0, maxh - src.Height, 0, maxw - src.Width, BorderTypes.Isolated);
- //Cv2.CopyMakeBorder(mat1, mat1, 0, maxh - mat1.Height, 0, maxw - mat1.Width, BorderTypes.Isolated);
- Mat tempMat0 = src.Clone(rect);
- //复制用户选中的区域到原图像
- Mat mask0 = tempMat0.CvtColor(ColorConversionCodes.RGBA2GRAY);
- Mat mat0 = new Mat(h, w, src.Type(), Scalar.All(/*255*/0));
- Mat pos0 = new Mat(mat0, rect);
- tempMat0.CopyTo(pos0, mask0);
- Cv2.Multiply(src/*mat0*/, mat2, dst, factor);
- //Cv2.Normalize(dst, dst, 1, 255, NormTypes.MinMax);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- src.CopyTo(dst);
- }
- finally
- {
- if (src != null) dst.CopyTo(src);
- if (dst != null) dst.Dispose();
- }
- if (isFourChannels)
- {
- Cv2.CvtColor(src, src, OpenCvSharp.ColorConversionCodes.BGR2BGRA);
- }
- return src;
- }
- /// <summary>
- /// 图像间操作-减
- /// </summary>
- /// <param name="src"></param>
- /// <returns></returns>
- public static Mat ShadingSubtract(Mat src, List<Args> lists)
- {
- Mat dst = new Mat();
- Mat mat1 = new Mat();
- Mat mat2 = new Mat();
- bool isFourChannels = false;
- if(src.Channels() == 4)
- {
- Cv2.CvtColor(src, src, OpenCvSharp.ColorConversionCodes.BGRA2BGR);
- isFourChannels = true;
- }
- try
- {
- for (int i = 0; i < lists.Count; i++)
- {
- Args args = lists[i];
- switch (args.Key)
- {
- case "Src2":
- var mat = (Mat)args.Value;
- if (mat.Channels() == 4)
- {
- Cv2.CvtColor(mat, mat, OpenCvSharp.ColorConversionCodes.BGRA2BGR);
- }
- mat1 = mat;
- break;
- default:
- break;
- }
- }
- int w = src.Width;
- int h = src.Height;
- int w1 = mat1.Width;
- int h1 = mat1.Height;
- mat2 = new Mat(h, w, src.Type(), Scalar.All(0));
- Rect rect = new Rect(0, 0, Math.Min(w, w1), Math.Min(h, h1));
- Mat tempMat = mat1.Clone(rect);
- //复制用户选中的区域到原图像
- Mat mask = tempMat.CvtColor(ColorConversionCodes.RGBA2GRAY);
- Mat pos = new Mat(mat2, rect);
- tempMat.CopyTo(pos, mask);
- //Cv2.CopyMakeBorder(src, src, 0, maxh - src.Height, 0, maxw - src.Width, BorderTypes.Isolated);
- //Cv2.CopyMakeBorder(mat1, mat1, 0, maxh - mat1.Height, 0, maxw - mat1.Width, BorderTypes.Isolated);
- Mat tempMat0 = src.Clone(rect);
- //复制用户选中的区域到原图像
- Mat mask0 = tempMat0.CvtColor(ColorConversionCodes.RGBA2GRAY);
- Mat mat0 = new Mat(h, w, src.Type(), Scalar.All(/*255*/0));
- Mat pos0 = new Mat(mat0, rect);
- tempMat0.CopyTo(pos0, mask0);
- Cv2.Subtract(src/*待确认效果:mat0*/, mat2, dst);
- //Cv2.Normalize(dst, dst, 1, 255, NormTypes.MinMax);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- src.CopyTo(dst);
- }
- finally
- {
- if (src != null) dst.CopyTo(src);
- if (dst != null) dst.Dispose();
- }
- if (isFourChannels) {
- Cv2.CvtColor(src, src, OpenCvSharp.ColorConversionCodes.BGR2BGRA);
- }
- return src;
- }
- /// <summary>
- /// 图像间操作-加
- /// </summary>
- /// <param name="src"></param>
- /// <returns></returns>
- public static Mat ShadingAdd(Mat src, List<Args> lists)
- {
- Mat dst = new Mat();
- Mat mat1 = new Mat();
- Mat mat2 = new Mat(); ;
- try
- {
- for (int i = 0; i < lists.Count; i++)
- {
- Args args = lists[i];
- switch (args.Key)
- {
- case "Src2":
- mat1 = (Mat)args.Value;
- break;
- default:
- break;
- }
- }
- int w = src.Width;
- int h = src.Height;
- int w1 = mat1.Width;
- int h1 = mat1.Height;
- int maxw = w;// w > w1 ? w : w1;
- int maxh = h;// h > h1 ? h : h1;
- //Scalar s = new Scalar(0, 0, 255);//创建一个颜色对象
- //mat2 = new Mat(w, h, MatType.CV_16SC1, s);
- mat2 = new Mat(maxh, maxw, src.Type(), Scalar.All(/*255*/0));
- Rect rect = new Rect(0, 0, Math.Min(w, w1), Math.Min(h, h1));
- Mat tempMat = mat1.Clone(rect);
- //复制用户选中的区域到原图像
- Mat mask = tempMat.CvtColor(ColorConversionCodes.RGBA2GRAY);
- Mat pos = new Mat(mat2, rect);
- tempMat.CopyTo(pos, mask);
- //Cv2.CopyMakeBorder(src, src, 0, maxh - src.Height, 0, maxw - src.Width, BorderTypes.Isolated);
- //Cv2.CopyMakeBorder(mat1, mat1, 0, maxh - mat1.Height, 0, maxw - mat1.Width, BorderTypes.Isolated);
- Mat tempMat0 = src.Clone(rect);
- //复制用户选中的区域到原图像
- Mat mask0 = tempMat0.CvtColor(ColorConversionCodes.RGBA2GRAY);
- Mat mat0 = new Mat(maxh, maxw, src.Type(), Scalar.All(/*255*/0));
- Mat pos0 = new Mat(mat0, rect);
- tempMat0.CopyTo(pos0, mask0);
- Cv2.Add(/*mat0*/src, mat2, dst);
- //Cv2.Normalize(dst, dst, 1, 255, NormTypes.MinMax);
- }
- catch(Exception e)
- {
- Console.WriteLine(e.ToString());
- src.CopyTo(dst);
- }
- finally
- {
- if (src != null) dst.CopyTo(src);
- if (dst != null) dst.Dispose();
- if (mat2 != null)
- {
- mat2.Dispose();
- }
- }
- return src;
- }
- /// <summary>
- /// 图像任意角度旋转,进行插值(仿射变换)
- /// </summary>
- /// <param name="src">源</param>
- /// <param name="dst">目标</param>
- /// <param name="angle">角度</param>
- public static void ImageRotate(Mat src, Mat dst, float angle, InterpolationFlags flags)
- {
- Mat dst1 = new Mat();
- Point2f center = new Point2f(src.Cols / 2, src.Rows / 2);
- Mat rot = Cv2.GetRotationMatrix2D(center, -angle, 1);
- Size2f s2f = new Size2f(src.Size().Width, src.Size().Height);
- Rect box = new RotatedRect(new Point2f(0, 0), s2f, -angle).BoundingRect();
- double xx = rot.At<double>(0, 2) + box.Width / 2 - src.Cols / 2;
- double zz = rot.At<double>(1, 2) + box.Height / 2 - src.Rows / 2;
- rot.Set(0, 2, xx);
- rot.Set(1, 2, zz);
- //对图片进行仿射变换
- Cv2.WarpAffine(src, dst1, rot, box.Size, flags);
- dst1.CopyTo(dst);
- }
- }
- }
|