我的代码包含:我打开要上传的图像,然后将其转换为灰度,稍后转换为二进制图像。但我有一个问题。我如何得到二进制图像的值(0,1),以便使用emgucv c创建一个具有该值的矩阵#??
OpenFileDialog Openfile = new OpenFileDialog();
if (Openfile.ShowDialog() == DialogResult.OK)
{
Image<Gray, Byte> My_Image = new Image<Gray, byte>(Openfile.FileName);
pictureBox1.Image = My_Image.ToBitmap();
My_Image = My_Image.ThresholdBinary(new Gray(69), new Gray(255));
pictureBox2.Image = My_Image.ToBitmap();
}
}
答案 0 :(得分:1)
我想我误解了这个问题,抱歉给出了错误的信息。但我想你可以从这篇文章中得到一些理解? Work with matrix in emgu cv
通过将ThresholdBinary()之后的结果My_Image传递给跟随函数,可以使数组为零,只有一个关于二进制图像。
public int[] ZeroOneArray(Image<Gray, byte> binaryImage)
{
//get the bytes value after Image.ThresholdBinary()
//either 0 or 255
byte[] imageBytes = binaryImage.Bytes;
//change 255 to 1 and remain 0
var binary_0_Or_255 = from byteInt in imageBytes select byteInt / 255;
//convert to array
int[] arrayOnlyOneOrZero = binary_0_Or_255.ToArray();
//checking the array content
foreach (var bin in arrayOnlyOneOrZero)
{
Console.WriteLine(bin);
}
return arrayOnlyOneOrZero;
}
这是你想要的吗?感谢
通过理解chris answer in error copying image to array,我为您编写了一个函数,将灰色二进制图像转换为灰色矩阵图像
public Image<Gray, double> GrayBinaryImageToMatrixImage(Image<Gray, byte> binaryImage)
{
byte[] imageBytes = binaryImage.Bytes;
Image<Gray, double> gray_image_div = new Image<Gray, double>(binaryImage.Size);//empty image method one
//or
Image<Gray, double> gray_image_div_II = binaryImage.Convert<Gray, double>().CopyBlank();//empty image method two
//transfer binaryImage array to gray image matrix
for (int i = 0; i < binaryImage.Width; i++)
{
for (int j = 0; j < binaryImage.Height; j++)
{
if (imageBytes[i*binaryImage.Width+j] == 0)
{
//grey image only one channel
gray_image_div.Data[i, j, 0] = 0;
}
else if (imageBytes[i*binaryImage.Width+j] == 255)
{
gray_image_div.Data[i, j, 0] = 255;
}
}
}
return gray_image_div;
}