我在通过JUnit测试时遇到了问题,我知道该测试是正确实现的,但我不确定为什么会失败。下面的代码应该获取每个像素(像素=数组exe中的一个点:2dArray[0][0])。每个像素都有一个亮度值,我的方法的目标是将整个数组中的每个像素的亮度值更改为127或至少接近127。
下面是我讨论的JUnit测试,我似乎无法像JUnit测试正在测试的方法的当前代码那样通过测试。下面的方法将通过大多数JUnit测试,但在运行assertEquals时失败JUnit测试的()部分。当我使用System.out.println()标准化图像平均亮度()是正确的,表示图像的平均亮度为127或接近127。我觉得这很奇怪,并且已经在这个问题上工作了很长一段时间,现在不确定还能尝试什么。任何帮助都将不胜感激。
JUnit测试:
@Test
void normalized() {
var smallNorm = smallSquare.normalized();
assertEquals(smallNorm.averageBrightness(), 127, 127 * .001);
var scale = 127 / 2.5;
var expectedNorm = new GrayscaleImage(new double[][] { { scale, 2 * scale }, { 3 * scale, 4 * scale } });
for (var row = 0; row < 2; row++) {
for (var col = 0; col < 2; col++) {
assertEquals(smallNorm.getPixel(col, row), expectedNorm.getPixel(col, row), expectedNorm.getPixel(col, row) * .001, "pixel at row: " + row + " col: " + col + " incorrect");
}
}
}
我的当前代码:
/**
* Return a new GrayScale image where the average new average brightness is 127
* To do this, uniformly scale each pixel (ie, multiply each imageData entry by the same value)
* Due to rounding, the new average brightness will not be 127 exactly, but should be very close
* The original image should not be modified
* @return a GrayScale image with pixel data uniformly rescaled so that its averageBrightness() is 127
*/
public GrayscaleImage normalized() {
// First we make a 2D array that is the same size as the original image.
double[][] normalized2DArray = new double[imageData[0].length][imageData.length];
// Then we figure out what we need to multiple the pixel by to make sure our average brightness is equal to 127
double scaleFactor = 127 / averageBrightness();
// Then we will set the brightness of the specified pixel below.
for (int y = 0; y < imageData[0].length; y++) {
for (int x = 0; x < imageData.length; x++) {
double newPixelValue = getPixel(y, x) * scaleFactor;
normalized2DArray[y][x] = newPixelValue;
}
}
// We will then turn the 2DArray into a GrayscaleImage
GrayscaleImage normalizedImage = new GrayscaleImage(normalized2DArray);
System.out.println(normalizedImage.averageBrightness());
return normalizedImage;
}
下面是JUnit测试的当前输出:
1条答案
按热度按时间kgsdhlau1#
我能够修复我的代码,问题是归一化2DArray [y][x] = newPixelValue;实际上应该被规格化2DArray [x][y] =新像素值;