Ahmed Farid
Ahmed Farid

Reputation: 128

Different Image processing results from Java and Matlab

I was doing a simple image processing task which I prototyped on Matlab, then made a port to Java. After executing the task on Java, I noticed different results. Is this a platform specific issue?

The task I was doing so far involves obtaining an image's green channel, computing the percentage of a number of pixels with values of a certain range, and computing the arithmetic mean of that channel. Code and results are shown:

Matlab

   a = imread('large.jpg');
   g = a(:,:,2);
   [x,y] = size(b);
   count = 0;
for i=1:x
    for j=1:y
        if g(i,j) < 90 & g(i,j) > 30
            count = count + 1;
            %Mistake part for arithmetic mean. Thanks to james_alvarez
            g(i,j) = 255;
        end
    end
end
 count
 (count/(3000*2400))*100
 mean2(g)

----------

1698469
23.5898
165.9823

Java

BufferedImage img = ImageIO.read(Function.class.getResource("large.jpg"));
int gCount = 0;
int meanCount = 0;

int w = img.getWidth();

int h = img.getHeight();
for(int i = 0 ; i < w ; i++){

        for(int j = 0 ; j< h ; j++){

            Color c = new Color(img.getRGB(i,j));
            int g = c.getGreen();

            meanCount += g;

            if(g < 90 && g > 30)
                gCount++;
    }
}

System.out.println(gCount);
System.out.println((gCount/(3000.0*2400.0))*100.0);
System.out.println(meanCount/(3000*2400));

----------

1706642
23.70336111111111
121

Why do they give considerably different results?

Upvotes: 1

Views: 139

Answers (1)

James Alvarez
James Alvarez

Reputation: 7219

In the Matlab code you also add the line:

g(i,j) = 255;

Which could be the possibility for why the mean 'g' is higher in the matlab version. Otherwise are your indexes right for the counting? e.g. in the matlab you use size(b), rather than getting the width/height directly from the image.

Upvotes: 1

Related Questions