4

I am trying to determine differences between two images during an integration test. After doing some research on the web, I stumbled over the MarvinProject and tried to create a UnitTest with it, see below.

As far as I understood the plugin DifferentRegions, it will fill the passed ImageMask differenceMask with the regions that contain the differences. The two images I pass for the test do differ, so it should print out something.

Unfortunately it does not.

I have written other tests that compare those two images byte-wise and those succeed. For those who want to try the problem, I have created a repository on GitHub and here is the ImageCompareTest.

@Test
public void tryMarvinProject() {
    // init images
    String root = "src/test/resources/";
    MarvinImage assertedImg = MarvinImageIO.loadImage(root + "image1.bmp");
    MarvinImage actualImg = MarvinImageIO.loadImage(root + "image2.bmp");

    // init diff-regions plugin
    DifferentRegions regions = new DifferentRegions();
    regions.load();
    regions.setAttribute("comparisonImage", assertedImg);

    int width = assertedImg.getWidth();
    int height = assertedImg.getHeight();
    int type = assertedImg.getType();

    // process the images and retrieve differences from the ImageMask
    MarvinImageMask differenceMask = new MarvinImageMask();
    regions.process(
            actualImg,
            new MarvinImage(new BufferedImage(width, height, type)),
            new MarvinAttributes(),
            differenceMask,
            false);

    // should contain the differences, but does not
    System.out.println(differenceMask.getMaskArray());
    assertNotNull(differenceMask.getMaskArray());
}
cheffe
  • 9,345
  • 2
  • 46
  • 57

1 Answers1

4

The plug-in DifferentRegions was developed for real-time video processing. The idea is to find the regions in the scene which are changing in a sequence of video frames, as shown in this Example

For image-to-image comparison, you should try DifferenceColor plug-in. Basically, it compare the two images analysing the color intensity of pixels in the same position. If the difference of two given pixels is higher than the attribute colorRange, the two pixels are considered distinct in color. The plug-in renders the different pixels in a different color to show them in the output image. If you pass an MarvinAttributes object in the process(...) method, you can get the number of different pixels, stored in the key total.

Example:

    MarvinAttributes attrOut = new MarvinAttributes();
    MarvinImagePlugin diff = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.difference.differenceColor");
    diff.process(imageA, imageB, attrOut);
    System.out.println("Distinct pixels:"+attrOut.get("total"));

PS: In order to use MarvinPluginLoader you must configure the development environment following the instructions presented here.


However, since you are trying to use image comparison for Unit Testing, you should take a look at MarvinTestCase class. It extends JUnit TestCase and provide methods to compare MarvinImage objects.

Input:

enter image description here

The source code below implements two test cases, one comparing imageA to imageB, and the other comparing imageA to imageC.

import marvin.image.MarvinImage;
import marvin.io.MarvinImageIO;
import marvin.test.MarvinTestCase;

public class UnitTesting extends MarvinTestCase {

    private MarvinImage imageA, imageB, imageC;

    public UnitTesting(){
        imageA = MarvinImageIO.loadImage("./res/penguin.jpg");
        imageB = MarvinImageIO.loadImage("./res/penguin.jpg");
        imageC = MarvinImageIO.loadImage("./res/penguin_dark.jpg");
    }

    public void testSameImage(){
        assertEquals(imageA, imageB);
    }

    public void testDistinctImages(){
        assertEquals(imageA, imageC);
    }
}

Using Eclipse or another IDE, run the class above as a JUnit Test. Below the output in the JUnit tab on Eclipse.

enter image description here

Gabriel Archanjo
  • 4,547
  • 2
  • 34
  • 41