using OpenCvSharp;
using PaintDotNet.Base;
using System;
using System.Collections.Generic;
namespace PaintDotNet.Adjust
{
///
/// 图像间操作
///
public class InterImageIntent
{
///
/// 图像间操作-最小值
///
///
///
public static Mat ImageMin(Mat src, List 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;
}
///
/// 图像间操作-最大值
///
///
///
public static Mat ImageMax(Mat src, List 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;
}
///
/// 图像间操作-指数
///
///
///
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;
}
///
/// 图像间操作-对数
///
///
///
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(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(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;
}
///
/// 图像间操作-平方根
///
///
///
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(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(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;
}
///
/// 图像间操作-平方
///
///
///
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;
}
///
/// 图像间操作-平均
///
///
///
public static Mat ImageAverage(Mat src, List 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;
}
///
/// 图像间操作-除
///
///
///
public static Mat ShadingDivide(Mat src, List 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;
}
///
/// 图像间操作-乘
///
///
///
public static Mat ShadingMultiply(Mat src, List 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;
}
///
/// 图像间操作-减
///
///
///
public static Mat ShadingSubtract(Mat src, List 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;
}
///
/// 图像间操作-加
///
///
///
public static Mat ShadingAdd(Mat src, List 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;
}
///
/// 图像任意角度旋转,进行插值(仿射变换)
///
/// 源
/// 目标
/// 角度
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(0, 2) + box.Width / 2 - src.Cols / 2;
double zz = rot.At(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);
}
}
}