简体   繁体   中英

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?

How can I convert Matrix to Bitmap ?

Converting Image to Matrix

The important thing to notice here is that both Image and Matrix inherit from 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.

NB: There are 3 dimensions of relevance -- width, height, and channel count.

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

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. 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.

Mat provides a Bitmap property, which behaves identically to 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.

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 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.

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_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"
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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