How can I convert Image to Matrix and then Matrix to Bitmap in EmguCV?
up vote
1
down vote
favorite
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?
c# opencv emgucv
|
show 2 more comments
up vote
1
down vote
favorite
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?
c# opencv emgucv
If I read the docs right, they both inherit fromCvArray, so maybe theCopyTomember? Btw, since the Image is 3-channel (BGR), then the Matrix should be as well for it to work.
– Dan Mašek
Nov 5 at 20:35
OK, so which one is it --ImagetoMatrixorMatrixtoBitmap? ;) I just got my C# + EmguCV env set up (as I haven't really touched C# since the early days) to be able to give you a proper answer, and the shifting target is honestly a bit discouraging (I recall reading a meta post of yours where someone pointed this out as well).
– Dan Mašek
Nov 7 at 0:46
@DanMašek, your 1st comment solved my 1st problem. Now, I am stuck with the second one.
– stackoverflow.com
Nov 7 at 0:50
@DanMašek, I am rolling back the question to previous one.
– stackoverflow.com
Nov 7 at 0:51
@DanMašek, by the way, I am deleting my profile. So, don't be so tensed about meta.
– stackoverflow.com
Nov 7 at 0:51
|
show 2 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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?
c# opencv 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?
c# opencv emgucv
c# opencv emgucv
edited Nov 7 at 3:14
Dan Mašek
8,30632445
8,30632445
asked Nov 5 at 18:06
stackoverflow.com
3,52642155310
3,52642155310
If I read the docs right, they both inherit fromCvArray, so maybe theCopyTomember? Btw, since the Image is 3-channel (BGR), then the Matrix should be as well for it to work.
– Dan Mašek
Nov 5 at 20:35
OK, so which one is it --ImagetoMatrixorMatrixtoBitmap? ;) I just got my C# + EmguCV env set up (as I haven't really touched C# since the early days) to be able to give you a proper answer, and the shifting target is honestly a bit discouraging (I recall reading a meta post of yours where someone pointed this out as well).
– Dan Mašek
Nov 7 at 0:46
@DanMašek, your 1st comment solved my 1st problem. Now, I am stuck with the second one.
– stackoverflow.com
Nov 7 at 0:50
@DanMašek, I am rolling back the question to previous one.
– stackoverflow.com
Nov 7 at 0:51
@DanMašek, by the way, I am deleting my profile. So, don't be so tensed about meta.
– stackoverflow.com
Nov 7 at 0:51
|
show 2 more comments
If I read the docs right, they both inherit fromCvArray, so maybe theCopyTomember? Btw, since the Image is 3-channel (BGR), then the Matrix should be as well for it to work.
– Dan Mašek
Nov 5 at 20:35
OK, so which one is it --ImagetoMatrixorMatrixtoBitmap? ;) I just got my C# + EmguCV env set up (as I haven't really touched C# since the early days) to be able to give you a proper answer, and the shifting target is honestly a bit discouraging (I recall reading a meta post of yours where someone pointed this out as well).
– Dan Mašek
Nov 7 at 0:46
@DanMašek, your 1st comment solved my 1st problem. Now, I am stuck with the second one.
– stackoverflow.com
Nov 7 at 0:50
@DanMašek, I am rolling back the question to previous one.
– stackoverflow.com
Nov 7 at 0:51
@DanMašek, by the way, I am deleting my profile. So, don't be so tensed about meta.
– stackoverflow.com
Nov 7 at 0:51
If I read the docs right, they both inherit from
CvArray, so maybe the CopyTo member? Btw, since the Image is 3-channel (BGR), then the Matrix should be as well for it to work.– Dan Mašek
Nov 5 at 20:35
If I read the docs right, they both inherit from
CvArray, so maybe the CopyTo member? Btw, since the Image is 3-channel (BGR), then the Matrix should be as well for it to work.– Dan Mašek
Nov 5 at 20:35
OK, so which one is it --
Image to Matrix or Matrix to Bitmap? ;) I just got my C# + EmguCV env set up (as I haven't really touched C# since the early days) to be able to give you a proper answer, and the shifting target is honestly a bit discouraging (I recall reading a meta post of yours where someone pointed this out as well).– Dan Mašek
Nov 7 at 0:46
OK, so which one is it --
Image to Matrix or Matrix to Bitmap? ;) I just got my C# + EmguCV env set up (as I haven't really touched C# since the early days) to be able to give you a proper answer, and the shifting target is honestly a bit discouraging (I recall reading a meta post of yours where someone pointed this out as well).– Dan Mašek
Nov 7 at 0:46
@DanMašek, your 1st comment solved my 1st problem. Now, I am stuck with the second one.
– stackoverflow.com
Nov 7 at 0:50
@DanMašek, your 1st comment solved my 1st problem. Now, I am stuck with the second one.
– stackoverflow.com
Nov 7 at 0:50
@DanMašek, I am rolling back the question to previous one.
– stackoverflow.com
Nov 7 at 0:51
@DanMašek, I am rolling back the question to previous one.
– stackoverflow.com
Nov 7 at 0:51
@DanMašek, by the way, I am deleting my profile. So, don't be so tensed about meta.
– stackoverflow.com
Nov 7 at 0:51
@DanMašek, by the way, I am deleting my profile. So, don't be so tensed about meta.
– stackoverflow.com
Nov 7 at 0:51
|
show 2 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
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"
)
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
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"
)
add a comment |
up vote
0
down vote
accepted
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"
)
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
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"
)
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"
)
answered Nov 7 at 3:03
Dan Mašek
8,30632445
8,30632445
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53159851%2fhow-can-i-convert-image-to-matrix-and-then-matrix-to-bitmap-in-emgucv%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
If I read the docs right, they both inherit from
CvArray, so maybe theCopyTomember? Btw, since the Image is 3-channel (BGR), then the Matrix should be as well for it to work.– Dan Mašek
Nov 5 at 20:35
OK, so which one is it --
ImagetoMatrixorMatrixtoBitmap? ;) I just got my C# + EmguCV env set up (as I haven't really touched C# since the early days) to be able to give you a proper answer, and the shifting target is honestly a bit discouraging (I recall reading a meta post of yours where someone pointed this out as well).– Dan Mašek
Nov 7 at 0:46
@DanMašek, your 1st comment solved my 1st problem. Now, I am stuck with the second one.
– stackoverflow.com
Nov 7 at 0:50
@DanMašek, I am rolling back the question to previous one.
– stackoverflow.com
Nov 7 at 0:51
@DanMašek, by the way, I am deleting my profile. So, don't be so tensed about meta.
– stackoverflow.com
Nov 7 at 0:51