使用给定的 NumPy 数组进行规范化(从 Python 到 C#)
Normalize using a given NumPy array (From Python To C#)
我正在尝试将一些 Python 代码转换为 C#,但代码实现的差异使我无法实现我的目标。
我尝试创建不同的数组和不同的函数来使用另一个数组规范化 C# 浮点数组。
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.'"
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];
}
}
}
我正在尝试将一些 Python 代码转换为 C#,但代码实现的差异使我无法实现我的目标。
我尝试创建不同的数组和不同的函数来使用另一个数组规范化 C# 浮点数组。
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.'"
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];
}
}
}