简体   繁体   中英

Struggling to set the average brightness of a grayscale image

I am having an issue passing a JUnit test that I know is implemented correctly and I'm not sure why it is failing. The code below should grab each pixel (pixel = a point in the array exe: 2dArray[0][0]). Each pixel holds a brightness value and the goal of my method is to change every pixels brightness value in the entire array to 127 or close to it at least.

Below is the JUnit test in question that I can't seem to pass as well as my current code for the method that the JUnit test is testing. The method below will pass most JUnit test but fails when ever it goes to run the assertEquals() portion of the JUnit test. As it stands now when ever I System.out.println() the normalizedImage.averageBrightness() it is correct and says that the average brightness of the image is 127 or close to it. I find this strange and have been working on this problem for quite awhile now and am unsure of what else to try. Any help would be appreciated.

JUnit Test:

@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");
            }
        }
    }

My Current Code:

   /**
     * 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;
    }

Here is the current output of the JUnit test: 在此处输入图像描述

I was able to fix my code the issue was that normalized2DArray[y][x] = newPixelValue; should have actually been normalized2DArray[x][y] = newPixelValue;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM