本文整理汇总了C#中OpenCvSharp.Mat.Size方法的典型用法代码示例。如果您正苦于以下问题:C# Mat.Size方法的具体用法?C# Mat.Size怎么用?C# Mat.Size使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OpenCvSharp.Mat
的用法示例。
在下文中一共展示了Mat.Size方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Run
public void Run()
{
Mat src = new Mat(FilePath.Image.Girl, ImreadModes.Color);
Mat dst = new Mat(FilePath.Image.Lenna, ImreadModes.Color);
Mat src0 = src.Resize(dst.Size(), 0, 0, InterpolationFlags.Lanczos4);
Mat mask = Mat.Zeros(src0.Size(), MatType.CV_8UC3);
mask.Circle(200, 200, 100, Scalar.White, -1);
Mat blend1 = new Mat();
Mat blend2 = new Mat();
Mat blend3 = new Mat();
Cv2.SeamlessClone(
src0, dst, mask, new Point(260, 270), blend1,
SeamlessCloneMethods.NormalClone);
Cv2.SeamlessClone(
src0, dst, mask, new Point(260, 270), blend2,
SeamlessCloneMethods.MonochromeTransfer);
Cv2.SeamlessClone(
src0, dst, mask, new Point(260, 270), blend3,
SeamlessCloneMethods.MixedClone);
using (new Window("src", src0))
using (new Window("dst", dst))
using (new Window("mask", mask))
using (new Window("blend NormalClone", blend1))
using (new Window("blend MonochromeTransfer", blend2))
using (new Window("blend MixedClone", blend3))
{
Cv2.WaitKey();
}
}
示例2: PutEllipseEyeMaskOnFace
/// <summary>
/// Eye見て傾き検出
/// </summary>
/// <param name="srcMat"></param>
/// <param name="putMat"></param>
/// <returns></returns>
public Mat PutEllipseEyeMaskOnFace(Mat srcMat, Mat putMat)
{
var grayMat = new Mat();
Cv2.CvtColor(srcMat, grayMat, ColorConversionCodes.BGR2GRAY);
Cv2.EqualizeHist(grayMat, grayMat);
var faces = Cascade.DetectMultiScale(grayMat);
if (faces == null) return srcMat;
var polygons = new List<List<Point>>();
var faceCount = faces.Count(); // O(n)
for (int d = 0; d < faceCount; d++)
{
polygons = new List<List<Point>>();
int x1 = faces[d].X;
int y1 = faces[d].Y;
int width = faces[d].Width;
int height = faces[d].Height;
int x2 = x1 + width;
int y2 = y1 + height;
int pwidth = putMat.Width;
int pheight = putMat.Height;
int srcWidth = srcMat.Width;
int srcHeight = srcMat.Height;
polygons.Add(new List<Point>() {
new Point(x1,y1),
new Point(x2,y1),
new Point(x2,y2),
new Point(x1,y2),
});
var faceSize = new Size(width, height);
//重ねるファイルは少し拡大したほうが良いかな?
/* Mat put0 = putMat[(int)(pwidth * 0.1) ,
(int)(pwidth * 0.9),
(int)(pheight * 0.1),
(int)(pheight * 0.9)]
.Resize(new Size(width, heigh), 0, 0, InterpolationFlags.Lanczos4);
*/
Mat put0 = putMat.Resize(faceSize, 0, 0, InterpolationFlags.Lanczos4);
//真ん中編の色を適当に抽出
// 改良の余地あり(肌色領域の平均取ったり?)
MatOfByte3 mat3 = new MatOfByte3(put0); // cv::Mat_<cv::Vec3b>
var indexer = mat3.GetIndexer();
Vec3b color = indexer[(int)(put0.Width * 0.5), (int)(put0.Height * 0.5)];
//抽出した色で埋める
Mat put1 = new Mat(srcMat.Size(), MatType.CV_8UC3, new Scalar(color.Item0, color.Item1, color.Item2));
//重ねる範囲にコピー
put1[y1, y2, x1, x2] = put0;
Mat put1gray = Mat.Zeros(srcMat.Size(), MatType.CV_8UC1);
put1gray[y1, y2, x1, x2] = grayMat[y1, y2, x1, x2];
var eyes = EyeCascade.DetectMultiScale(put1gray);
/*
Debug.WriteLine(eyes.Count());
var cccc = new Point(eyes[0].X + eyes[0].Width * 0.5, eyes[0].Y + eyes[0].Height * 0.5);
put1gray.Circle(cccc,(int)(eyes[0].Width * 0.5), new Scalar(0, 255, 255));
return put1gray;*/
var eyeCount = eyes.Count();
if (eyeCount >= 2)
{
var eyePpints = new List<Point>();
var orderedEyes = eyes.OrderByDescending(x => x.Width * x.Height).ToArray();
while (true)
{
for (int i = 0; i < 2; i++)
{
eyePpints.Add(new Point(eyes[i].X + eyes[i].Width * 0.5, eyes[i].Y + eyes[i].Height * 0.5));
}
var wrapRect = Cv2.MinAreaRect(eyePpints);
if (Math.Abs(wrapRect.Angle % 180) < 20)
{
var scale = 1.0;
var angle = -wrapRect.Angle % 180;
var eyedx = (eyePpints[0].X + eyePpints[1].X) * 0.5 - wrapRect.Center.X;
var eyedy = (eyePpints[0].Y + eyePpints[1].Y) * 0.5 - wrapRect.Center.Y;
//中心はここ
var center = new Point(
(faces[d].X + faces[d].Width * 0.5) + eyedx,
(faces[d].Y + faces[d].Height * 0.5) + eyedy);
//.........这里部分代码省略.........
示例3: PutMaskOnFace
/// <summary>
/// Poisson Image Editing
/// </summary>
/// <param name="srcMat">顔がある方</param>
/// <param name="putMat">重ねる顔</param>
/// <returns></returns>
public Mat PutMaskOnFace(Mat srcMat, Mat putMat)
{
var grayMat = new Mat();
Cv2.CvtColor(srcMat, grayMat, ColorConversionCodes.BGR2GRAY);
Cv2.EqualizeHist(grayMat, grayMat);
var faces = Cascade.DetectMultiScale(grayMat);
if (faces == null) return srcMat;
var binaryMat = new Mat();
int blockSize = 7;
double k = 0.15;
double R = 32;
Binarizer.Sauvola(grayMat, binaryMat, blockSize, k, R);
Cv2.BitwiseNot(binaryMat, binaryMat);
var polygons = new List<List<Point>>();
var faceCount = faces.Count(); // O(n)
for (int d = 0; d < faceCount; d++)
{
polygons = new List<List<Point>>();
int x1 = faces[d].X;
int y1 = faces[d].Y;
int width = faces[d].Width;
int heigh = faces[d].Height;
int x2 = x1 + width;
int y2 = y1 + heigh;
polygons.Add(new List<Point>() {
new Point(x1,y1),
new Point(x2,y1),
new Point(x2,y2),
new Point(x1,y2),
});
var pwidth = putMat.Width;
var pheight = putMat.Height;
//重ねるファイルは少し拡大したほうが良いかな?
/* Mat put0 = putMat[(int)(pwidth * 0.1) ,
(int)(pwidth * 0.9),
(int)(pheight * 0.1),
(int)(pheight * 0.9)]
.Resize(new Size(width, heigh), 0, 0, InterpolationFlags.Lanczos4);
*/
Mat put0 = putMat.Resize(new Size(width, heigh), 0, 0, InterpolationFlags.Lanczos4);
//真ん中編の色を適当に抽出
// 改良の余地あり(肌色領域の平均取ったり?)
MatOfByte3 mat3 = new MatOfByte3(put0); // cv::Mat_<cv::Vec3b>
var indexer = mat3.GetIndexer();
Vec3b color = indexer[(int)(put0.Width * 0.5), (int)(put0.Height * 0.5)];
//抽出した色で埋める
Mat put1 = new Mat(srcMat.Size(), MatType.CV_8UC3, new Scalar(color.Item0, color.Item1, color.Item2));
//重ねる範囲にコピー
put1[y1, y2, x1, x2] = put0;
Mat mask = Mat.Zeros(srcMat.Size(), MatType.CV_8UC3);
Cv2.FillPoly(mask, polygons, new Scalar(255, 255, 255));
//中心はここ
var center = new Point(faces[d].X + faces[d].Width * 0.5, faces[d].Y + faces[d].Height * 0.5);
Cv2.SeamlessClone(put1, srcMat, mask, center, srcMat, SeamlessCloneMethods.NormalClone);
}
return srcMat;
}
示例4: PutEllipseMaskOnFace2
public Mat PutEllipseMaskOnFace2(Mat srcMat, Mat putMat)
{
var grayMat = new Mat();
Cv2.CvtColor(srcMat, grayMat, ColorConversionCodes.BGR2GRAY);
Cv2.EqualizeHist(grayMat, grayMat);
var faces = Cascade.DetectMultiScale(grayMat);
if (faces == null) return srcMat;
var binaryMat = new Mat();
// binaryMat = ColorExtractor.ExtractMask(srcMat,ColorConversionCodes.BGR2HSV,ColorVariation.Skin);
// return binaryMat;
int blockSize = 7;
double k = 1.5;
double R = 100;
Binarizer.Sauvola(grayMat, binaryMat, blockSize, k, R);
Cv2.BitwiseNot(binaryMat, binaryMat);
return binaryMat;
var polygons = new List<List<Point>>();
var faceCount = faces.Count(); // O(n)
for (int d = 0; d < faceCount; d++)
{
polygons = new List<List<Point>>();
int x1 = faces[d].X;
int y1 = faces[d].Y;
int width = faces[d].Width;
int height = faces[d].Height;
int x2 = x1 + width;
int y2 = y1 + height;
int pwidth = putMat.Width;
int pheight = putMat.Height;
int srcWidth = srcMat.Width;
int srcHeight = srcMat.Height;
polygons.Add(new List<Point>() {
new Point(x1,y1),
new Point(x2,y1),
new Point(x2,y2),
new Point(x1,y2),
});
// f = fixed
/* int fx1 = (int)(x1 - width * 0.01);
fx1 = fx1 > 0 ? fx1 : 0;
int fx2 = (int)(x2 + width * 0.01);
fx2 = fx2 < srcWidth ? fx2 : srcWidth;
int fy1 = (int)(y1 - height * 0.01);
fy1 = fy1 > 0 ? fy1 : 0;
int fy2 = (int)(y2 + height * 0.01);
fy2 = fy2 < srcHeight ? fy2 : srcHeight;
*/
int fx1 = (int)(x1 + width * 0.1);
int fx2 = (int)(x2 - width * 0.1);
int fy1 = (int)(y1 + height * 0.1);
int fy2 = (int)(y2 - height * 0.1);
int fwidth = x2 - x1;
int fheight = y2 - y1;
var faceSize = new Size(fwidth, fheight);
//重ねるファイルは少し拡大したほうが良いかな?
/* Mat put0 = putMat[(int)(pwidth * 0.1) ,
(int)(pwidth * 0.9),
(int)(pheight * 0.1),
(int)(pheight * 0.9)]
.Resize(new Size(width, heigh), 0, 0, InterpolationFlags.Lanczos4);
*/
Mat put0 = putMat.Resize(faceSize, 0, 0, InterpolationFlags.Lanczos4);
//真ん中編の色を適当に抽出
// 改良の余地あり(肌色領域の平均取ったり?)
MatOfByte3 mat3 = new MatOfByte3(put0); // cv::Mat_<cv::Vec3b>
var indexer = mat3.GetIndexer();
Vec3b color = indexer[(int)(put0.Width * 0.5), (int)(put0.Height * 0.5)];
//抽出した色で埋める
Mat put1 = new Mat(srcMat.Size(), MatType.CV_8UC3, new Scalar(color.Item0, color.Item1, color.Item2));
//重ねる範囲にコピー
put1[y1, y2, x1, x2] = put0;
Mat mask = Mat.Zeros(srcMat.Size(), MatType.CV_8UC3);
//中心はここ
var center = new Point(faces[d].X + faces[d].Width * 0.5, faces[d].Y + faces[d].Height * 0.5);
//.........这里部分代码省略.........
示例5: ConvertImage
/// <summary>
/// utility function: convert one image to another with optional vertical flip
/// </summary>
/// <param name="src"></param>
/// <param name="dst"></param>
/// <param name="flags"></param>
public static void ConvertImage(Mat src, Mat dst, ConvertImageModes flags = ConvertImageModes.None)
{
if (src == null)
throw new ArgumentNullException("src");
if (dst == null)
throw new ArgumentNullException("dst");
src.ThrowIfDisposed();
dst.ThrowIfDisposed();
dst.Create(src.Size(), MatType.CV_8UC3);
NativeMethods.imgcodecs_cvConvertImage_Mat(src.CvPtr, dst.CvPtr, (int)flags);
GC.KeepAlive(src);
GC.KeepAlive(dst);
}
示例6: Run
public void Run()
{
Mat img = Cv2.ImRead(FilePath.Image.Lenna, ImreadModes.GrayScale);
// expand input image to optimal size
Mat padded = new Mat();
int m = Cv2.GetOptimalDFTSize(img.Rows);
int n = Cv2.GetOptimalDFTSize(img.Cols); // on the border add zero values
Cv2.CopyMakeBorder(img, padded, 0, m - img.Rows, 0, n - img.Cols, BorderTypes.Constant, Scalar.All(0));
// Add to the expanded another plane with zeros
Mat paddedF32 = new Mat();
padded.ConvertTo(paddedF32, MatType.CV_32F);
Mat[] planes = { paddedF32, Mat.Zeros(padded.Size(), MatType.CV_32F) };
Mat complex = new Mat();
Cv2.Merge(planes, complex);
// this way the result may fit in the source matrix
Mat dft = new Mat();
Cv2.Dft(complex, dft);
// compute the magnitude and switch to logarithmic scale
// => log(1 + sqrt(Re(DFT(I))^2 + Im(DFT(I))^2))
Mat[] dftPlanes;
Cv2.Split(dft, out dftPlanes); // planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
// planes[0] = magnitude
Mat magnitude = new Mat();
Cv2.Magnitude(dftPlanes[0], dftPlanes[1], magnitude);
magnitude += Scalar.All(1); // switch to logarithmic scale
Cv2.Log(magnitude, magnitude);
// crop the spectrum, if it has an odd number of rows or columns
Mat spectrum = magnitude[
new Rect(0, 0, magnitude.Cols & -2, magnitude.Rows & -2)];
// rearrange the quadrants of Fourier image so that the origin is at the image center
int cx = spectrum.Cols / 2;
int cy = spectrum.Rows / 2;
Mat q0 = new Mat(spectrum, new Rect(0, 0, cx, cy)); // Top-Left - Create a ROI per quadrant
Mat q1 = new Mat(spectrum, new Rect(cx, 0, cx, cy)); // Top-Right
Mat q2 = new Mat(spectrum, new Rect(0, cy, cx, cy)); // Bottom-Left
Mat q3 = new Mat(spectrum, new Rect(cx, cy, cx, cy)); // Bottom-Right
// swap quadrants (Top-Left with Bottom-Right)
Mat tmp = new Mat();
q0.CopyTo(tmp);
q3.CopyTo(q0);
tmp.CopyTo(q3);
// swap quadrant (Top-Right with Bottom-Left)
q1.CopyTo(tmp);
q2.CopyTo(q1);
tmp.CopyTo(q2);
// Transform the matrix with float values into a
Cv2.Normalize(spectrum, spectrum, 0, 1, NormTypes.MinMax);
// Show the result
Cv2.ImShow("Input Image" , img);
Cv2.ImShow("Spectrum Magnitude", spectrum);
// calculating the idft
Mat inverseTransform = new Mat();
Cv2.Dft(dft, inverseTransform, DftFlags.Inverse | DftFlags.RealOutput);
Cv2.Normalize(inverseTransform, inverseTransform, 0, 1, NormTypes.MinMax);
Cv2.ImShow("Reconstructed by Inverse DFT", inverseTransform);
Cv2.WaitKey();
}