使用给定的NumPy数组进行规范化(从Python到C#)

2024-04-26 20:28:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图将一些Python代码转换成C#,但代码实现上的差异使我无法实现我的目标。你知道吗

我尝试创建不同的数组和函数来使用另一个数组规范化一个C#float数组。你知道吗

Python代码:

mean_vec = np.array([102.9801, 115.9465, 122.7717])
for i in range(image.shape[0]):
     image[i, :, :] = image[i, :, :] - mean_vec[i]

我在C#中尝试的代码:

Image<Bgr, byte> image = new Image<Bgr, byte>(newBitmap);
Mat newMat = image.Mat;
float[] array = new float[(int)newMat.Total];
newMat.CopyTo(array);
float[] mean_vector = new float[] { 102.9801f, 115.9465f, 122.7717f };

for(int i=0; i<bitmapWidth; i++)
{
    for(int j=0; j<3; j++)
        array[i] = array[i] - mean_vector[j];
}

我得到以下错误

"System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'"


Tags: 代码imagenewfor数组bytefloatmean
1条回答
网友
1楼 · 发布于 2024-04-26 20:28:19
                for (int k = 0; k < 3; k++)
                {
                    for (int j = 0; j < newBitmap.Height; j++)
                    {
                        for (int i = 0; i < newBitmap.Width; i++)
                        {
                            floatArray[k, j, i] = (float)Convert.ToDouble(image.Data[j, i, k]) - mean_vec[k];
                        }
                    }
                }

相关问题 更多 >