gpt4 book ai didi

c# - 在 emgu cv 中使用矩阵

转载 作者:太空宇宙 更新时间:2023-11-03 22:09:23 25 4
gpt4 key购买 nike

我在 EMGU cv 中有一个图像的矩阵 (2D),我如何用零填充矩阵的其余部分,同时保留原始数据的特定区域(矩形)?

最佳答案

方法一

实现您的目标的一种方法是直接访问矩阵的数据并将值设置为“0”,以下代码会将“My_Matrix”的下四分之一设置为 0,其中所有其他值将保持为 20。

Matrix<double> My_Matrix_Image = new Matrix<double>(8,10);

My_Matrix_Image.SetValue(20); //set all values of Matrix to 20

for(int i = 4; i< My_Matrix_Image.Height; i++)
{
for (int j = 5; j < My_Matrix_Image.Width; j++)
{
My_Matrix_Image.Data[i,j] = 0;
}
}

要从您的评论中实现您想要的,您仍然必须采用相同的方法 My_Matrix_Image 将包含 0-255 的图像数据值我将为灰度和彩色图像提供解决方案。我将保留图像的前 30 行,并将其余部分设置为零,图像的大小将是 100x100 像素。更改 j = 30 以更改行数。

第一个彩色图像矩阵将有 3 个 channel 红色绿色蓝色:

for(int i = 0; i< My_Matrix_Image.Height; i++)
{
for (int j = 30; j < My_Matrix_Image.Width; j++)
{
My_Matrix_Image.Data[i,j,0] = 0; //RED
My_Matrix_Image.Data[i,j,1] = 0; //GREEN
My_Matrix_Image.Data[i,j,2] = 0; //BLUE

}
}

灰度图像矩阵更容易,因为将有 1 个 channel :

for(int i = 0; i< My_Matrix_Image.Height; i++)
{
for (int j = 30; j < My_Matrix_Image.Width; j++)
{
My_Matrix_Image.Data[i,j,0] = 0;
}
}

现在假设您想要保留Middle 40 Rows,您将不得不添加一个额外的循环记住这是使用矩阵的唯一方法我只提供了如您所见,彩色图像示例开始变得有些困惑,方法 2 可能更好:

for(int i = 0; i< My_Matrix_Image.Height; i++)
{
//LOOP 1 SET THE FRONT ROWS
for (int j = 0; j<40; j++)
{
My_Matrix_Image.Data[i,j,0] = 0; //RED
My_Matrix_Image.Data[i,j,1] = 0; //GREEN
My_Matrix_Image.Data[i,j,2] = 0; //BLUE
}
// LOOP 2 SET THE BACK ROWS
for (int j = 60; j < My_Matrix_Image.Width; j++)
{
My_Matrix_Image.Data[i,j,0] = 0; //RED
My_Matrix_Image.Data[i,j,1] = 0; //GREEN
My_Matrix_Image.Data[i,j,2] = 0; //BLUE
}
}

方法二

现在假设您确实想要保留一个数据矩形。创建 6 个循环既复杂又低效,因此您可以这样做。

//Make a Blank Copy of your Image this will be automatically full of zeros
Matrix<double> My_Image_Copy = My_Image_Matrix.CopyBlank();

//Now copy the data you want to keep from your original image into you blank copy

for(int i = 40; i< 60; i++)
{
for (int j = 40; j < 60; j++)
{
My_Image_Copy.Data[i,j,0] = My_Matrix_Image.Data[i,j,0]; //RED
My_Image_Copy.Data[i,j,1] = My_Matrix_Image.Data[i,j,1]; //GREEN
My_Image_Copy.Data[i,j,2] = My_Matrix_Image.Data[i,j,2]; //BLUE

}
}

上面的代码将从图像中复制中心 20x20 像素,您显然可以通过使用 for(int i = 0; i< My_Matrix_Image.Height; i++ 将其更改为复制整行)

好多了我相信你会同意的。

备选

现在,当您使用 Matrix 来存储数据时,使用 Image 结构可以使编码更简单一些。虽然这可能与您无关,但可能与其他人相关。

如果您使用图像或替代方案来存储图像数据,则可以通过以下方式实现:

Image<Gray, Byte> My_Image = new Image<Gray, byte>(openfile.FileName);
Image<Gray, Byte> My_Image_Copy = My_Image.CopyBlank();

Rectangle Store_ROI = my_image.ROI; //Only need one as both images same size

My_Image.ROI = new Rectangle(50, 50, 100, 100);
My_Image_Copy.ROI = new Rectangle(50, 50, 100, 100);

My_Image_Copy = My_Image.Copy(); //This will only copy the Region Of Interest

//Reset the Regions Of Interest so you will now operate on the whole image
My_Image.ROI = Store_ROI;
My_Image_Copy.ROI = Store_ROI;

现在这与方法 2 相同,但您不需要对可能发生错误的写出循环进行排序。

希望这个更正能回答您的问题,

干杯

克里斯

关于c# - 在 emgu cv 中使用矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7197153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com