简体   繁体   English

如何在EmguCV中将图像转换为矩阵,然后将矩阵转换为位图?

[英]How can I convert Image to Matrix and then Matrix to Bitmap in EmguCV?

I am trying to do something like the following: 我正在尝试执行以下操作:

public partial class Form1 : Form
{
const string path = @"lena.png";

public Form1()
{
    InitializeComponent();

    Image<Bgr, byte> color = new Image<Bgr, byte>(path);

    Matrix<byte> matrix = new Matrix<byte>(color.Rows, color.Cols);

    matrix.Data = color.ToMatrix();// just an analogy

    pictureBox1.Image = col.Bitmap;
    pictureBox2.Image = gray.Bitmap;
}
}

How can I convert Image to Matrix in EmguCV? 如何在EmguCV中将Image转换为Matrix

How can I convert Matrix to Bitmap ? 如何将Matrix转换为Bitmap

Converting Image to Matrix Image转换为Matrix

The important thing to notice here is that both Image and Matrix inherit from CvArray . 这里要注意的重要一点是ImageMatrix都从CvArray继承。 That means it is possible to use the (inherited) method CopyTo to copy the data from an Image instance to a Matrix instance of identical depth and dimensions. 这意味着可以使用(继承的) CopyTo方法将数据从Image实例复制到具有相同深度和尺寸的Matrix实例。

NB: There are 3 dimensions of relevance -- width, height, and channel count. 注意:相关性分为3个维度-宽度,高度和频道数。

Image<Bgr, byte> color = ... ; // Initialized in some manner

Matrix<byte> matrix = new Matrix<byte>(color.Rows, color.Cols, color.NumberOfChannels);

color.CopyTo(matrix);

NB: There's a cost involved with this approach, due to the necessity to make a copy of the entire data array. 注意:这种方法涉及成本,因为有必要复制整个数据阵列。

Converting Matrix to Bitmap Matrix转换为Bitmap

This one is actually quite simple. 这实际上很简单。 Matrix inherits the property Mat , which returns a Mat header (meaning a thin wrapper around an existing data array) for the array data. Matrix继承属性Mat ,该属性为该数组数据返回一个Mat头(意味着在现有数据数组周围的薄包装)。 Since this just creates a header, it's very quick (no copies involved). 由于这只是创建标题,因此非常快(不涉及副本)。

NB: Due to being a header, I assume based on my experience with the C++ API (even though this doesn't seem documented) that the Mat object is valid as long as the underlying Matrix object stays in scope. 注意:由于是标头,因此根据我对C ++ API的经验(即使似乎没有记录),我假设只要基础Matrix对象保持作用域, Mat对象就有效。

Mat provides a Bitmap property, which behaves identically to Image.Bitmap . Mat提供了一个Bitmap属性,其行为与Image.Bitmap相同。

Bitmap b = matrix.Mat.Bitmap;

The other option is to use the same approach as before to copy the data back to the Image instance. 另一个选择是使用与以前相同的方法将数据复制回Image实例。

matrix.CopyTo(color);

Then you could use the Bitmap property (fast, but requires the Image instance to live as long as you use the Bitmap ). 然后你可以使用Bitmap属性(快,但要求Image的实例,只要您使用活Bitmap )。

Bitmap b = color.Bitmap;

Another alternative would be to use the ToBitmap method, which copies the data, and therefore doesn't carry the dependency on the source Image instance. 另一种选择是使用ToBitmap方法,该方法将复制数据,因此不携带对源Image实例的依赖关系。

Bitmap b = color.ToBitmap();

Source used for testing: 用于测试的源:

using System;
using System.Drawing;
using Emgu.CV;
using Emgu.CV.Structure;
// ============================================================================
namespace CS1 {
// ============================================================================
class Test
{
    static void Main()
    {
        Image<Bgr, byte> color = new Image<Bgr, byte>(2, 2);
        for (int r = 0; r < color.Rows; r++) {
            for (int c = 0; c < color.Cols; c++) {
                int n = (c + r * color.Cols) * 3;
                color[r, c] = new Bgr(n, n+1, n+2);
            }
        }

        Matrix<byte> matrix = new Matrix<byte>(color.Rows, color.Cols, color.NumberOfChannels);

        color.CopyTo(matrix);

        Bitmap b = matrix.Mat.Bitmap;

        matrix.CopyTo(color);

        b = color.Bitmap;

        b = color.ToBitmap();
    }
}
// ============================================================================
} // namespace CS1
// ============================================================================

The CMake file I used to generate a solution to compile this: 我用来生成解决方案的CMake文件可以对此进行编译:

cmake_minimum_required(VERSION 3.11)
project(CS1 VERSION 0.1.0 LANGUAGES CSharp)

add_executable(cs1
    src/test.cs
)

set_property(TARGET cs1
    PROPERTY VS_DOTNET_TARGET_FRAMEWORK_VERSION "v4.6.1"
)

set_property(TARGET cs1 
    PROPERTY VS_DOTNET_REFERENCES
    "System"
    "System.Drawing"
)

set_target_properties(cs1 PROPERTIES 
    VS_DOTNET_REFERENCE_emgu_cv_world "deps/Emgu.CV.World.dll"
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM