Rename expected to reference in SsimHelper.

Reference is clearer when used in conjunction with actual, and matches
other SSIM documentation.

PiperOrigin-RevId: 449486177
This commit is contained in:
samrobinson 2022-05-18 16:15:24 +01:00 committed by Ian Baker
parent c78c1f5891
commit 2a45dd3930

View File

@ -63,44 +63,44 @@ public final class SsimHelper {
private static final int DECODED_IMAGE_CHANNEL_COUNT = 3; private static final int DECODED_IMAGE_CHANNEL_COUNT = 3;
/** /**
* Returns the mean SSIM score between the expected and the actual video. * Returns the mean SSIM score between the reference and the actual video.
* *
* <p>The method compares every {@link #DEFAULT_COMPARISON_INTERVAL n-th} frame from both videos. * <p>The method compares every {@link #DEFAULT_COMPARISON_INTERVAL n-th} frame from both videos.
* *
* @param context The {@link Context}. * @param context The {@link Context}.
* @param expectedVideoPath The path to the expected video file, must be in {@link * @param referenceVideoPath The path to the reference video file, must be in {@link
* Context#getAssets() Assets}. * Context#getAssets() Assets}.
* @param actualVideoPath The path to the actual video file. * @param actualVideoPath The path to the actual video file.
* @throws IOException When unable to open the provided video paths. * @throws IOException When unable to open the provided video paths.
*/ */
public static double calculate(Context context, String expectedVideoPath, String actualVideoPath) public static double calculate(Context context, String referenceVideoPath, String actualVideoPath)
throws IOException, InterruptedException { throws IOException, InterruptedException {
VideoDecodingWrapper expectedDecodingWrapper = VideoDecodingWrapper referenceDecodingWrapper =
new VideoDecodingWrapper(context, expectedVideoPath, DEFAULT_COMPARISON_INTERVAL); new VideoDecodingWrapper(context, referenceVideoPath, DEFAULT_COMPARISON_INTERVAL);
VideoDecodingWrapper actualDecodingWrapper = VideoDecodingWrapper actualDecodingWrapper =
new VideoDecodingWrapper(context, actualVideoPath, DEFAULT_COMPARISON_INTERVAL); new VideoDecodingWrapper(context, actualVideoPath, DEFAULT_COMPARISON_INTERVAL);
@Nullable byte[] expectedLumaBuffer = null; @Nullable byte[] referenceLumaBuffer = null;
@Nullable byte[] actualLumaBuffer = null; @Nullable byte[] actualLumaBuffer = null;
double accumulatedSsim = 0.0; double accumulatedSsim = 0.0;
int comparedImagesCount = 0; int comparedImagesCount = 0;
try { try {
while (true) { while (true) {
@Nullable Image expectedImage = expectedDecodingWrapper.runUntilComparisonFrameOrEnded(); @Nullable Image referenceImage = referenceDecodingWrapper.runUntilComparisonFrameOrEnded();
@Nullable Image actualImage = actualDecodingWrapper.runUntilComparisonFrameOrEnded(); @Nullable Image actualImage = actualDecodingWrapper.runUntilComparisonFrameOrEnded();
if (expectedImage == null) { if (referenceImage == null) {
assertThat(actualImage).isNull(); assertThat(actualImage).isNull();
break; break;
} }
checkNotNull(actualImage); checkNotNull(actualImage);
int width = expectedImage.getWidth(); int width = referenceImage.getWidth();
int height = expectedImage.getHeight(); int height = referenceImage.getHeight();
assertThat(actualImage.getWidth()).isEqualTo(width); assertThat(actualImage.getWidth()).isEqualTo(width);
assertThat(actualImage.getHeight()).isEqualTo(height); assertThat(actualImage.getHeight()).isEqualTo(height);
if (expectedLumaBuffer == null || expectedLumaBuffer.length != width * height) { if (referenceLumaBuffer == null || referenceLumaBuffer.length != width * height) {
expectedLumaBuffer = new byte[width * height]; referenceLumaBuffer = new byte[width * height];
} }
if (actualLumaBuffer == null || actualLumaBuffer.length != width * height) { if (actualLumaBuffer == null || actualLumaBuffer.length != width * height) {
actualLumaBuffer = new byte[width * height]; actualLumaBuffer = new byte[width * height];
@ -108,20 +108,20 @@ public final class SsimHelper {
try { try {
accumulatedSsim += accumulatedSsim +=
SsimCalculator.calculate( SsimCalculator.calculate(
extractLumaChannelBuffer(expectedImage, expectedLumaBuffer), extractLumaChannelBuffer(referenceImage, referenceLumaBuffer),
extractLumaChannelBuffer(actualImage, actualLumaBuffer), extractLumaChannelBuffer(actualImage, actualLumaBuffer),
/* offset= */ 0, /* offset= */ 0,
/* stride= */ width, /* stride= */ width,
width, width,
height); height);
} finally { } finally {
expectedImage.close(); referenceImage.close();
actualImage.close(); actualImage.close();
} }
comparedImagesCount++; comparedImagesCount++;
} }
} finally { } finally {
expectedDecodingWrapper.close(); referenceDecodingWrapper.close();
actualDecodingWrapper.close(); actualDecodingWrapper.close();
} }
assertWithMessage("Input had no frames.").that(comparedImagesCount).isGreaterThan(0); assertWithMessage("Input had no frames.").that(comparedImagesCount).isGreaterThan(0);
@ -347,7 +347,7 @@ public final class SsimHelper {
/** /**
* Calculates the Structural Similarity Index (SSIM) between two images. * Calculates the Structural Similarity Index (SSIM) between two images.
* *
* @param expected The luma channel (Y) bitmap of the expected image. * @param reference The luma channel (Y) bitmap of the reference image.
* @param actual The luma channel (Y) bitmap of the actual image. * @param actual The luma channel (Y) bitmap of the actual image.
* @param offset The offset. * @param offset The offset.
* @param stride The stride of the bitmap. * @param stride The stride of the bitmap.
@ -356,23 +356,23 @@ public final class SsimHelper {
* @return The SSIM score between the input images. * @return The SSIM score between the input images.
*/ */
public static double calculate( public static double calculate(
byte[] expected, byte[] actual, int offset, int stride, int width, int height) { byte[] reference, byte[] actual, int offset, int stride, int width, int height) {
double totalSsim = 0; double totalSsim = 0;
int windowsCount = 0; int windowsCount = 0;
// X refers to the expected image, while Y refers to the actual image. // X refers to the reference image, while Y refers to the actual image.
for (int currentWindowY = 0; currentWindowY < height; currentWindowY += WINDOW_SIZE) { for (int currentWindowY = 0; currentWindowY < height; currentWindowY += WINDOW_SIZE) {
int windowHeight = computeWindowSize(currentWindowY, height); int windowHeight = computeWindowSize(currentWindowY, height);
for (int currentWindowX = 0; currentWindowX < width; currentWindowX += WINDOW_SIZE) { for (int currentWindowX = 0; currentWindowX < width; currentWindowX += WINDOW_SIZE) {
windowsCount++; windowsCount++;
int windowWidth = computeWindowSize(currentWindowX, width); int windowWidth = computeWindowSize(currentWindowX, width);
int start = getGlobalCoordinate(currentWindowX, currentWindowY, stride, offset); int start = getGlobalCoordinate(currentWindowX, currentWindowY, stride, offset);
double meanX = getMean(expected, start, stride, windowWidth, windowHeight); double meanX = getMean(reference, start, stride, windowWidth, windowHeight);
double meanY = getMean(actual, start, stride, windowWidth, windowHeight); double meanY = getMean(actual, start, stride, windowWidth, windowHeight);
double[] variances = double[] variances =
getVariancesAndCovariance( getVariancesAndCovariance(
expected, actual, meanX, meanY, start, stride, windowWidth, windowHeight); reference, actual, meanX, meanY, start, stride, windowWidth, windowHeight);
// varX is the variance of window X, covXY is the covariance between window X and Y. // varX is the variance of window X, covXY is the covariance between window X and Y.
double varX = variances[0]; double varX = variances[0];
double varY = variances[1]; double varY = variances[1];