0

I'm Trying to Get Pixel values of Mat Type Images in C# (EmguCV) how can I get Pixel values of Mat images without converting them to Image<> type ?!

I do it in python efficiently with NumPy arrays... but in C# (Emgu) I Don't Know how I can do that...

Mat image = CvInvoke.Imread(img_path);

this is the image that I read...

JavadMH13
  • 33
  • 1
  • 8
  • Does this answer your question? [How can I get and set pixel values of an EmguCV Mat image?](https://stackoverflow.com/questions/32255440/how-can-i-get-and-set-pixel-values-of-an-emgucv-mat-image) – erik7854 Dec 01 '22 at 16:55
  • thank you so much, yes that topic solved my problem – JavadMH13 Dec 31 '22 at 08:16

1 Answers1

0

There are two ways to access Mat Image's pixel value in EmguCV (C#).

1- Convert Mat Image to Image<ColorType, DDepth>
2- Use Image.Data.GetValue() Functions

1- you can convert your Mat image to Image Format and then access the pixel values just like below:

int row = 0;
int col = 5;
int Channel = 0;
Mat ImageMat = CvInvoke.Imread("Image/Path");
Image<Gray, Byte> ImageFormat = ImageMat.ToImage<Gray, Byte>();
int pixel_value = ImageFormat.Data[row, col, Channel];

2- you can directly access pixel value.GetValue method for Mat Class. but this method returns an object you have to convert to a datatype like an int to float eg.

Mat ImageMat = CvInvoke.Imread("Image/Path");
object pixel_value = ImageMat.Data.GetValue(row, col, Channel);
float pixelVal = Convert.ToInt32(pixel_value);
JavadMH13
  • 33
  • 1
  • 8