本文整理汇总了C#中OpenCvSharp.Mat.Clone方法的典型用法代码示例。如果您正苦于以下问题:C# Mat.Clone方法的具体用法?C# Mat.Clone怎么用?C# Mat.Clone使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OpenCvSharp.Mat
的用法示例。
在下文中一共展示了Mat.Clone方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SelectStitchingImages
private static Mat[] SelectStitchingImages(int width, int height, int count)
{
Mat source = new Mat(@"Data\Image\lenna.png", ImreadModes.Color);
Mat result = source.Clone();
var rand = new Random();
var mats = new List<Mat>();
for (int i = 0; i < count; i++)
{
int x1 = rand.Next(source.Cols - width);
int y1 = rand.Next(source.Rows - height);
int x2 = x1 + width;
int y2 = y1 + height;
result.Line(new Point(x1, y1), new Point(x1, y2), new Scalar(0, 0, 255));
result.Line(new Point(x1, y2), new Point(x2, y2), new Scalar(0, 0, 255));
result.Line(new Point(x2, y2), new Point(x2, y1), new Scalar(0, 0, 255));
result.Line(new Point(x2, y1), new Point(x1, y1), new Scalar(0, 0, 255));
Mat m = source[new Rect(x1, y1, width, height)];
mats.Add(m.Clone());
}
using (new Window(result))
{
Cv2.WaitKey();
}
return mats.ToArray();
}
示例2: DetectFace
/// <summary>
///
/// </summary>
/// <param name="cascade"></param>
/// <returns></returns>
private Mat DetectFace(CascadeClassifier cascade)
{
Mat result;
using (var src = new Mat(FilePath.Image.Yalta, ImreadModes.Color))
using (var gray = new Mat())
{
result = src.Clone();
Cv2.CvtColor(src, gray, ColorConversionCodes.BGR2GRAY);
// Detect faces
Rect[] faces = cascade.DetectMultiScale(
gray, 1.08, 2, HaarDetectionType.ScaleImage, new Size(30, 30));
// Render all detected faces
foreach (Rect face in faces)
{
var center = new Point
{
X = (int)(face.X + face.Width * 0.5),
Y = (int)(face.Y + face.Height * 0.5)
};
var axes = new Size
{
Width = (int)(face.Width * 0.5),
Height = (int)(face.Height * 0.5)
};
Cv2.Ellipse(result, center, axes, 0, 0, 360, new Scalar(255, 0, 255), 4);
}
}
return result;
}
示例3: Run
public void Run()
{
using (Mat src = new Mat(FilePath.Image.Distortion, ImreadModes.Color))
using (Mat gray = new Mat())
using (Mat dst = src.Clone())
{
Cv2.CvtColor(src, gray, ColorConversionCodes.BGR2GRAY);
CppStyleMSER(gray, dst); // C++ style
using (new Window("MSER src", src))
using (new Window("MSER gray", gray))
using (new Window("MSER dst", dst))
{
Cv2.WaitKey();
}
}
}
示例4: SampleCpp
/// <summary>
/// sample of new C++ style wrapper
/// </summary>
private void SampleCpp()
{
// (1) Load the image
using (Mat imgGray = new Mat(FilePath.Image.Goryokaku, ImreadModes.GrayScale))
using (Mat imgStd = new Mat(FilePath.Image.Goryokaku, ImreadModes.Color))
using (Mat imgProb = imgStd.Clone())
{
// Preprocess
Cv2.Canny(imgGray, imgGray, 50, 200, 3, false);
// (3) Run Standard Hough Transform
LineSegmentPolar[] segStd = Cv2.HoughLines(imgGray, 1, Math.PI / 180, 50, 0, 0);
int limit = Math.Min(segStd.Length, 10);
for (int i = 0; i < limit; i++ )
{
// Draws result lines
float rho = segStd[i].Rho;
float theta = segStd[i].Theta;
double a = Math.Cos(theta);
double b = Math.Sin(theta);
double x0 = a * rho;
double y0 = b * rho;
Point pt1 = new Point { X = (int)Math.Round(x0 + 1000 * (-b)), Y = (int)Math.Round(y0 + 1000 * (a)) };
Point pt2 = new Point { X = (int)Math.Round(x0 - 1000 * (-b)), Y = (int)Math.Round(y0 - 1000 * (a)) };
imgStd.Line(pt1, pt2, Scalar.Red, 3, LineTypes.AntiAlias, 0);
}
// (4) Run Probabilistic Hough Transform
LineSegmentPoint[] segProb = Cv2.HoughLinesP(imgGray, 1, Math.PI / 180, 50, 50, 10);
foreach (LineSegmentPoint s in segProb)
{
imgProb.Line(s.P1, s.P2, Scalar.Red, 3, LineTypes.AntiAlias, 0);
}
// (5) Show results
using (new Window("Hough_line_standard", WindowMode.AutoSize, imgStd))
using (new Window("Hough_line_probabilistic", WindowMode.AutoSize, imgProb))
{
Window.WaitKey(0);
}
}
}
示例5: Run
public void Run()
{
using (Mat imgSrc = new Mat(FilePath.Image.Lenna, ImreadModes.Color))
using (Mat imgGray = new Mat())
using (Mat imgDst = imgSrc.Clone())
{
Cv2.CvtColor(imgSrc, imgGray, ColorConversionCodes.BGR2GRAY, 0);
KeyPoint[] keypoints = Cv2.FAST(imgGray, 50, true);
foreach (KeyPoint kp in keypoints)
{
imgDst.Circle(kp.Pt, 3, Scalar.Red, -1, LineTypes.AntiAlias, 0);
}
Cv2.ImShow("FAST", imgDst);
Cv2.WaitKey(0);
Cv2.DestroyAllWindows();
}
}
示例6: PutEllipseMaskOnFace
//.........这里部分代码省略.........
int fy1 = (int)(y1 - height * 0.1);
fy1 = fy1 > 0 ? fy1 : 0;
int fy2 = (int)(y2 + height * 0.1);
fy2 = fy2 < srcHeight ? fy2 : srcHeight;
int fwidth = x2 - x1;
int fheight = y2 - y1;
/*
var detectedContours = contours.Where(c =>
{
var cc = c.Count();
return cc > 150 && cc < 1000;
});
foreach(var con in detectedContours)
{
var rotateRect = Cv2.FitEllipse(con);
}*/
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);
Mat faceAroundMat = Mat.Zeros(srcMat.Size(), MatType.CV_8UC1);
faceAroundMat[fy1, fy2, fx1, fx2] = binaryMat[fy1, fy2, fx1, fx2];
// faceAroundMat[y1, y2, x1, x2] = binaryMat[y1, y2, x1, x2];
//var countours = new
// 単純な輪郭抽出のみでは、傾きがわからない
// 元のAPIが破壊的な関数なので clone http://opencv.jp/opencv-2svn/cpp/imgproc_structural_analysis_and_shape_descriptors.html#cv-findcontours
//var contours = faceAroundMat.Clone().FindContoursAsArray(RetrievalModes.List, ContourApproximationModes.ApproxNone);
var contours = binaryMat.Clone().FindContoursAsArray(RetrievalModes.List, ContourApproximationModes.ApproxNone);
/*
Mat mat = Mat.Zeros(srcMat.Size(), MatType.CV_8UC3);
Cv2.DrawContours(mat, contours.Where(c => Cv2.ContourArea(c) > 100),-1, new Scalar(0, 255, 0));
*/
var detectedContour = contours.FindMax(c => Cv2.ContourArea(c));
// var rotateRect = new RotatedRect();
/*
if (Cv2.ContourArea(detectedContour) > Cv2.ContourArea(polygons[0]) * 0.3)
{
rotateRect = Cv2.FitEllipse(detectedContour);
}
else
{
rotateRect = new RotatedRect(center, new Size2f(faceSize.Width, faceSize.Height), 0);
}
rotateRect = Cv2.FitEllipse(detectedContour);
Debug.WriteLine(rotateRect.Angle);*/
var rotateRect = Cv2.FitEllipse(detectedContour);
rotateRect.Size = new Size2f(faceSize.Width, faceSize.Height);
float angle = Math.Abs(rotateRect.Angle) > 20 ? -rotateRect.Angle % 20 : -rotateRect.Angle;
float scale = 1.0f;
// 回転
Mat matrix = Cv2.GetRotationMatrix2D(center, angle, scale);
//画像を回転させる
Cv2.WarpAffine(put1, put1, matrix, put1.Size());
Cv2.Ellipse(mask, rotateRect, new Scalar(255, 255, 255), -1, LineTypes.AntiAlias);
// Cv2.FillPoly(mask, polygons, new Scalar(255, 255, 255));
Cv2.SeamlessClone(put1, srcMat, mask, center, srcMat, SeamlessCloneMethods.NormalClone);
}
return srcMat;
}
示例7: CvDct
public void CvDct(ref Mat DST, Mat SRC, int N)
{
Mat dct, idct;
Mat dct2, dct3;
int width = SRC.Width;//N;
int height = SRC.Height;//N;
DST = SRC.Clone();
//DCT,IDCT用の行列作成(double)
dct = new Mat(height, width, MatType.CV_64FC1);
idct = new Mat(height, width, MatType.CV_64FC1);
dct2 = new Mat(height, width, MatType.CV_64FC1);
dct3 = new Mat(height, width, MatType.CV_64FC1);
var indexer_DST = new MatOfByte3(DST).GetIndexer();
var indexer_dct = new MatOfDouble3(dct).GetIndexer();
//行列dctに画像データをコピー
//double fcos;
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
{
Vec3d color = indexer_dct[y, x];
color.Item0= indexer_DST[y, x].Item0 / 256.0;
indexer_dct[y,x] = color;
}
//DCT…dctをコサイン変換してdct2を作成します
Cv2.Dct(dct, dct2, DctFlags.None);
//dct2をDenomで割りdct3を作成します
PerformDenom(ref dct3, dct2);
//IDCT…dct3を逆コサイン変換します
Cv2.Dct(dct3, idct, DctFlags.Inverse);
var indexer_idct = new MatOfDouble3(idct).GetIndexer();
//逆変換用画像にデータをコピー
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
{
Vec3b color = indexer_DST[y, x];
color.Item0= (byte)(indexer_idct[y,x].Item0 * 256.0);
indexer_DST[y,x]=color;
}
////正規化
//double min, max;
//min = 4000000000000;
//max = -4000000000000;
//double offset = 0.0;
////輝度値の最大と最小を取得
//DST.MinMaxIdx(out min, out max);
////for (int x = 0; x < width; x++)
//// for (int y = 0; y < height; y++)
//// {
//// double data = indexer_DST[y,x].Item0;
//// if (data < min) min = data;
//// if (data > max) max = data;
//// }
//for (int x = 0; x < width; x++)
// for (int y = 0; y < height; y++)
// {
// Vec3b color = indexer_DST[y, x];
// double data = indexer_DST[y, x].Item0;
// if (data < min + offset) data = min + offset;
// color.Item0 = (byte)( (((data / (max - min + offset))) * 255.0) - (((min + offset) / (max - min + offset)) * 255.0) );
// indexer_DST[y,x] = color;
// }
////DST = idct.Clone();
//行列メモリを開放します
dct.Dispose();
dct2.Dispose();
dct3.Dispose();
idct.Dispose();
indexer_dct = null;
indexer_DST = null;
indexer_idct = null;
}
示例8: PerformDenom
private void PerformDenom(ref Mat DST, Mat SRC)
{
double PI = 3.1416;
int width = SRC.Width;
int height = SRC.Height;
//XとYを準備
double[,] meshX = new double[height,width];
double[,] meshY = new double[height,width];
double[,] denom = new double[height,width];
DST = SRC.Clone();
var indexer = new MatOfDouble3(DST).GetIndexer();
//メッシュグリッドの作成
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
{
meshX[y, x] = x;
meshY[y, x] = y;
}
//固有値計算
for (int y = 0; y < height; y++)
for (int x = 0;x < width; x++)
denom[y, x] = (2.0 * Math.Cos(PI * (double)x / ((double)width)) - 2.0) + (2.0 * Math.Cos(PI * (double)y / ((double)height)) - 2.0);
//計算
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
{
//data_d[j][i] = data_s[j][i] / denom[j][i];
Vec3d color = indexer[y, x];
color.Item0 = indexer[y, x].Item0;
//ゼロ割防止
if (!(x == 0 && y == 0)) color.Item0 = color.Item0 / denom[y, x];
indexer[y, x] = color;
}
indexer = null;
}
示例9: infilterY
public void infilterY(ref Mat DST,Mat SRC)
{
int width = SRC.Width;
int height = SRC.Height;
DST = SRC.Clone();
var indexer = new MatOfByte3(DST).GetIndexer();
for (int x = 0; x < width; x++)
for (int y = 0; y < height - 1; y++)
{
Vec3b color = indexer[y, x];
double val = indexer[y+1, x].Item0 - indexer[y, x].Item0;
if (val > 255) color.Item0 = 255;
else if (val < 0) color.Item0 = 0;
else color.Item0 = (byte)val;
indexer[y, x] = color;
}
indexer = null;
}
示例10: MatOfByte3
private void OnClick実行(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("OnClick実行 開始");
if (is4Image)
{
int width = 入力画像[0].Width;
int height = 入力画像[0].Height;
SGx = new OpenCvSharp.Mat(height,width, MatType.CV_8UC1);
SGy = new OpenCvSharp.Mat(height, width, MatType.CV_8UC1);
for (int num = 0; num < 4; num++)
{
Gx[num] = 入力画像[num].Clone();
Gy[num] = 入力画像[num].Clone();
}
for (int num = 0; num < 4; num++)
{//infilterX,Y
mCV.infilterX(ref Gx[num], 入力画像[num]);
mCV.infilterY(ref Gy[num], 入力画像[num]);
}
mCV.Median(Gx, ref SGx);
mCV.Median(Gy, ref SGy);
//Gxxを作る.とりあえず外周1ピクセルやらない方向で.
Gxx = new OpenCvSharp.Mat(height, width, MatType.CV_8UC1);
Gyy = new OpenCvSharp.Mat(height, width, MatType.CV_8UC1);
mCV.infilterX(ref Gxx, SGx);
mCV.infilterY(ref Gyy, SGy);
//SP作成(仮の出力画像)
Mat SP = new OpenCvSharp.Mat(height,width, MatType.CV_8UC1);
var indexer_sp = new MatOfByte3(SP).GetIndexer();
var indexer_Gxx= new MatOfByte3(Gxx).GetIndexer();
var indexer_Gyy = new MatOfByte3(Gyy).GetIndexer();
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
{
Vec3b color = indexer_sp[y, x];
double val = indexer_Gxx[y, x].Item0+ indexer_Gyy[y, x].Item0;
if (val > 255) color.Item0 = 255;
else if (val < 0) color.Item0 = 0;
else color.Item0 = (byte)val;
indexer_sp[y, x] = color;
}
indexer_sp =null;
indexer_Gxx =null;
indexer_Gyy = null;
Mat DCT_dst = new OpenCvSharp.Mat(height, width, MatType.CV_8UC1);
mCV.CvDct(ref DCT_dst,SP, 1024);//第3引数使われてない件
//DCT_dst = SP.Clone();
//mCV.吉岡反射光除去処理(入力画像, ref DCT_dst,int.Parse(textBox_Gaus.Text),int.Parse(textBox_Bright.Text));
出力画像=DCT_dst.Clone();
}
else System.Diagnostics.Debug.WriteLine("no 4 images");
System.Diagnostics.Debug.WriteLine("OnClick実行 終了");
}