OpenCvSharp Mat 访问像素点

Get/Set (slow)

Mat mat = new Mat("lenna.png", LoadMode.Color);

for (int y = 0; y < mat.Height; y++)
{
    for (int x = 0; x < mat.Width; x++)
    {
        Vec3b color = mat.Get(y, x);
        byte temp = color.Item0;
        color.Item0 = color.Item2; // B <- R
        color.Item2 = temp;        // R <- B
        mat.Set(y, x, color);
    }
}

GenericIndexer (reasonably fast)

Mat mat = new Mat("lenna.png", LoadMode.Color);

var indexer = mat.GetGenericIndexer();
for (int y = 0; y < mat.Height; y++)
{
    for (int x = 0; x < mat.Width; x++)
    {
        Vec3b color = indexer[y, x];
        byte temp = color.Item0;
        color.Item0 = color.Item2; // B <- R
        color.Item2 = temp;        // R <- B
        indexer[y, x] = color;
    }
}

TypeSpecificMat (faster)

Mat mat = new Mat("lenna.png", LoadMode.Color);

var mat3 = new Mat(mat); // cv::Mat_
var indexer = mat3.GetIndexer();

for (int y = 0; y < mat.Height; y++)
{    
    for (int x = 0; x < mat.Width; x++)
    {
        Vec3b color = indexer[y, x];
        byte temp = color.Item0;
        color.Item0 = color.Item2; // B <- R
        color.Item2 = temp;        // R <- B
        indexer[y, x] = color;
    }
}

你可能感兴趣的:(OpenCvSharp,前端,javascript,c#,opencv,人工智能)