本文整理汇总了C#中Mat.Step方法的典型用法代码示例。如果您正苦于以下问题:C# Mat.Step方法的具体用法?C# Mat.Step怎么用?C# Mat.Step使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mat
的用法示例。
在下文中一共展示了Mat.Step方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ToMat
/// <summary>
/// System.Drawing.BitmapからOpenCVのMatへ変換して返す.
/// </summary>
/// <param name="src">変換するSystem.Drawing.Bitmap</param>
/// <param name="dst">変換結果を格納するMat</param>
#else
/// <summary>
/// Converts System.Drawing.Bitmap to Mat
/// </summary>
/// <param name="src">System.Drawing.Bitmap object to be converted</param>
/// <param name="dst">A Mat object which is converted from System.Drawing.Bitmap</param>
#endif
public static unsafe void ToMat(this Bitmap src, Mat dst)
{
if (src == null)
throw new ArgumentNullException("src");
if (dst == null)
throw new ArgumentNullException("dst");
if (dst.IsDisposed)
throw new ArgumentException("The specified dst is disposed.", "dst");
if (dst.Depth() != MatType.CV_8U)
throw new NotSupportedException("Mat depth != CV_8U");
if (dst.Dims() != 2)
throw new NotSupportedException("Mat dims != 2");
if (src.Width != dst.Width || src.Height != dst.Height)
throw new ArgumentException("src.Size != dst.Size");
int w = src.Width;
int h = src.Height;
Rectangle rect = new Rectangle(0, 0, w, h);
BitmapData bd = null;
try
{
bd = src.LockBits(rect, ImageLockMode.ReadOnly, src.PixelFormat);
byte* p = (byte*)bd.Scan0.ToPointer();
int sstep = bd.Stride;
int offset = sstep - (w / 8);
uint dstep = (uint)dst.Step();
IntPtr dstData = dst.Data;
byte* dstPtr = (byte*)dstData.ToPointer();
bool submat = dst.IsSubmatrix();
bool continuous = dst.IsContinuous();
switch (src.PixelFormat)
{
case PixelFormat.Format1bppIndexed:
{
if (dst.Channels() != 1)
throw new ArgumentException("Invalid nChannels");
if (submat)
throw new NotImplementedException("submatrix not supported");
int x = 0;
int y;
int bytePos;
byte b;
int i;
for (y = 0; y < h; y++)
{
// 横は必ず4byte幅に切り上げられる。
// この行の各バイトを調べていく
for (bytePos = 0; bytePos < sstep; bytePos++)
{
if (x < w)
{
// 現在の位置のバイトからそれぞれのビット8つを取り出す
b = p[bytePos];
for (i = 0; i < 8; i++)
{
if (x >= w)
{
break;
}
// IplImageは8bit/pixel
dstPtr[dstep * y + x] = ((b & 0x80) == 0x80) ? (byte)255 : (byte)0;
b <<= 1;
x++;
}
}
}
// 次の行へ
x = 0;
p += sstep;
}
}
break;
case PixelFormat.Format8bppIndexed:
case PixelFormat.Format24bppRgb:
{
if (src.PixelFormat == PixelFormat.Format8bppIndexed)
if (dst.Channels() != 1)
throw new ArgumentException("Invalid nChannels");
if (src.PixelFormat == PixelFormat.Format24bppRgb)
if (dst.Channels() != 3)
throw new ArgumentException("Invalid nChannels");
// ステップが同じで連続なら、一気にコピー
//.........这里部分代码省略.........
示例2: Perform
/// <summary>
///
/// </summary>
/// <param name="img"></param>
/// <param name="blobs"></param>
/// <returns></returns>
public static int Perform(Mat img, CvBlobs blobs)
{
if (img == null)
throw new ArgumentNullException(nameof(img));
if (blobs == null)
throw new ArgumentNullException(nameof(blobs));
if (img.Type() != MatType.CV_8UC1)
throw new ArgumentException("'img' must be a 1-channel U8 image.");
LabelData labels = blobs.Labels;
if (labels == null)
throw new ArgumentException("");
//if(labels.GetLength(0) != h || labels.GetLength(1) != w)
if (labels.Rows != img.Height || labels.Cols != img.Width)
throw new ArgumentException("img.Size != labels' size");
int numPixels = 0;
blobs.Clear();
int w = img.Cols;
int h = img.Rows;
int step = (int)img.Step();
byte[] imgIn;
unsafe
{
byte* imgInPtr = (byte*)img.Data;
if ((long) h * step > Int32.MaxValue)
throw new ArgumentException("Too big image (image data > 2^31)");
int length = h * step;
imgIn = new byte[length];
Marshal.Copy(new IntPtr(imgInPtr), imgIn, 0, imgIn.Length);
}
int label = 0;
int lastLabel = 0;
CvBlob lastBlob = null;
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
if (imgIn[x + y * step] == 0)
continue;
bool labeled = labels[y, x] != 0;
if (!labeled && ((y == 0) || (imgIn[x + (y - 1) * step] == 0)))
{
labeled = true;
// Label contour.
label++;
if (label == MarkerValue)
throw new Exception();
labels[y, x] = label;
numPixels++;
// XXX This is not necessary at all. I only do this for consistency.
if (y > 0)
labels[y - 1, x] = MarkerValue;
CvBlob blob = new CvBlob(label, x, y);
blobs.Add(label, blob);
lastLabel = label;
lastBlob = blob;
blob.Contour.StartingPoint = new Point(x, y);
int direction = 1;
int xx = x;
int yy = y;
bool contourEnd = false;
do
{
for (int numAttempts = 0; numAttempts < 3; numAttempts++)
{
bool found = false;
for (int i = 0; i < 3; i++)
{
int nx = xx + MovesE[direction, i, 0];
int ny = yy + MovesE[direction, i, 1];
if ((nx < w) && (nx >= 0) && (ny < h) && (ny >= 0))
{
if (imgIn[nx + ny * step] != 0)
{
found = true;
blob.Contour.ChainCode.Add((CvChainCode)MovesE[direction, i, 3]);
xx = nx;
yy = ny;
direction = MovesE[direction, i, 2];
break;
}
labels[ny, nx] = MarkerValue;
}
//.........这里部分代码省略.........
示例3: ToMat
/// <summary>
/// BitmapSourceをMatに変換する.
/// </summary>
/// <param name="src">変換するBitmapSource</param>
/// <param name="dst">出力先のMat</param>
#else
/// <summary>
/// Converts BitmapSource to Mat
/// </summary>
/// <param name="src">Input BitmapSource</param>
/// <param name="dst">Output Mat</param>
#endif
public static void ToMat(this BitmapSource src, Mat dst)
{
if (src == null)
throw new ArgumentNullException("src");
if (dst == null)
throw new ArgumentNullException("dst");
if (src.PixelWidth != dst.Width || src.PixelHeight != dst.Height)
throw new ArgumentException("size of src must be equal to size of dst");
if (dst.Dims() > 2)
throw new ArgumentException("Mat dimensions must be 2");
int w = src.PixelWidth;
int h = src.PixelHeight;
int bpp = src.Format.BitsPerPixel;
int channels = WriteableBitmapConverter.GetOptimumChannels(src.Format);
if (dst.Channels() != channels)
{
throw new ArgumentException("nChannels of dst is invalid", "dst");
}
bool submat = dst.IsSubmatrix();
bool continuous = dst.IsContinuous();
unsafe
{
byte* p = (byte*)(dst.Data);
long step = dst.Step();
// 1bppは手作業でコピー
if (bpp == 1)
{
if (submat)
throw new NotImplementedException("submatrix not supported");
// BitmapImageのデータを配列にコピー
// 要素1つに横8ピクセル分のデータが入っている。
int stride = (w / 8) + 1;
byte[] pixels = new byte[h * stride];
src.CopyPixels(pixels, stride, 0);
int x = 0;
for (int y = 0; y < h; y++)
{
int offset = y * stride;
// この行の各バイトを調べていく
for (int bytePos = 0; bytePos < stride; bytePos++)
{
if (x < w)
{
// 現在の位置のバイトからそれぞれのビット8つを取り出す
byte b = pixels[offset + bytePos];
for (int i = 0; i < 8; i++)
{
if (x >= w)
{
break;
}
p[step * y + x] = ((b & 0x80) == 0x80) ? (byte)255 : (byte)0;
b <<= 1;
x++;
}
}
}
// 次の行へ
x = 0;
}
}
// 8bpp
/*else if (bpp == 8)
{
int stride = w;
byte[] pixels = new byte[h * stride];
src.CopyPixels(pixels, stride, 0);
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
p[step * y + x] = pixels[y * stride + x];
}
}
}*/
// 24bpp, 32bpp, ...
else
{
int stride = w * ((bpp + 7) / 8);
if (!submat && continuous)
{
long imageSize = dst.DataEnd.ToInt64() - dst.Data.ToInt64();
//.........这里部分代码省略.........
示例4: ToWriteableBitmap
/// <summary>
/// MatをWriteableBitmapに変換する.
/// 返却値を新たに生成せず引数で指定したWriteableBitmapに格納するので、メモリ効率が良い。
/// </summary>
/// <param name="src">変換するMat</param>
/// <param name="dst">変換結果を設定するWriteableBitmap</param>
#else
/// <summary>
/// Converts Mat to WriteableBitmap.
/// This method is more efficient because new instance of WriteableBitmap is not allocated.
/// </summary>
/// <param name="src">Input Mat</param>
/// <param name="dst">Output WriteableBitmap</param>
#endif
public static void ToWriteableBitmap(Mat src, WriteableBitmap dst)
{
if (src == null)
throw new ArgumentNullException("src");
if (dst == null)
throw new ArgumentNullException("dst");
if (src.Width != dst.PixelWidth || src.Height != dst.PixelHeight)
throw new ArgumentException("size of src must be equal to size of dst");
//if (src.Depth != BitDepth.U8)
//throw new ArgumentException("bit depth of src must be BitDepth.U8", "src");
if (src.Dims() > 2)
throw new ArgumentException("Mat dimensions must be 2");
int w = src.Width;
int h = src.Height;
int bpp = dst.Format.BitsPerPixel;
int channels = GetOptimumChannels(dst.Format);
if (src.Channels() != channels)
{
throw new ArgumentException("channels of dst != channels of PixelFormat", "dst");
}
bool submat = src.IsSubmatrix();
bool continuous = src.IsContinuous();
unsafe
{
byte* pSrc = (byte*)(src.Data);
int sstep = (int)src.Step();
if (bpp == 1)
{
if (submat)
throw new NotImplementedException("submatrix not supported");
// 手作業で移し替える
int stride = w / 8 + 1;
if (stride < 2)
stride = 2;
byte[] pixels = new byte[h * stride];
for (int x = 0, y = 0; y < h; y++)
{
int offset = y * stride;
for (int bytePos = 0; bytePos < stride; bytePos++)
{
if (x < w)
{
byte b = 0;
// 現在の位置から横8ピクセル分、ビットがそれぞれ立っているか調べ、1つのbyteにまとめる
for (int i = 0; i < 8; i++)
{
b <<= 1;
if (x < w && pSrc[sstep * y + x] != 0)
{
b |= 1;
}
x++;
}
pixels[offset + bytePos] = b;
}
}
x = 0;
}
dst.WritePixels(new Int32Rect(0, 0, w, h), pixels, stride, 0);
return;
}
// 一気にコピー
if (!submat && continuous)
{
long imageSize = src.DataEnd.ToInt64() - src.Data.ToInt64();
if (imageSize < 0)
throw new OpenCvSharpException("The mat has invalid data pointer");
if (imageSize > Int32.MaxValue)
throw new OpenCvSharpException("Too big mat data");
dst.WritePixels(new Int32Rect(0, 0, w, h), src.Data, (int)imageSize, sstep);
return;
}
// 一列ごとにコピー
try
{
dst.Lock();
//.........这里部分代码省略.........
示例5: QuickDetectBalls_Field
private static int[,] QuickDetectBalls_Field(Mat mat, int ballWidth)
{
var ballWidth_3 = ballWidth / 3;
var detectM = new int[mat.Width / ballWidth_3 + 1, mat.Height / ballWidth_3 + 1];
if (true)
{
var watcher = new System.Diagnostics.Stopwatch();
watcher.Start();
for (var y = 0; y < mat.Height; y += 1)
{
var line = new IntPtr(mat.Data.ToInt64() + mat.Step() * y);
for (var x = 0; x < mat.Width; x += 1)
{
if (System.Runtime.InteropServices.Marshal.ReadByte(line + x) > 0)
detectM[x / ballWidth_3, y / ballWidth_3]++;
}
}
watcher.Stop();
Console.WriteLine(watcher.Elapsed);
}
return detectM;
}