[Free] Extension Compare Images pixel by pixel. Source code. Bitmap

Hello friends,

this extension compares two images pixel by pixel (rgb) and returns a number from 0 to 100 based on their differences.

It just calculates the average percentage of colors, so you don't always get a useful result. Just try it.

1.- Block. Example. Extension.

compara_imagenes

p120_compararimagen.aia (1.4 MB)

com.KIO4_CompareImages.aix (6.3 KB)

8 Likes

2.- Source code.

package com.KIO4_CompareImages;
// Juan Antonio Villalpando - kio4.com

import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.DesignerProperty;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.UsesPermissions; // Permisos
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.*;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory; 

@DesignerComponent(version = 1,
    description = "Compare pixel to pixel two images. Juan Antonio Villalpando - KIO4.COM ",
    category = ComponentCategory.EXTENSION,
    nonVisible = true,
    iconName = "") 
@SimpleObject(external = true)
@UsesPermissions(permissionNames = "android.permission.READ_EXTERNAL_STORAGE")
public class KIO4_CompareImages extends AndroidNonvisibleComponent implements Component {

    public static final int VERSION = 1;
	private ComponentContainer container;

	 private double percentage = 0.0;
	 private Bitmap bmp1;
	 private Bitmap bmp2;

    public KIO4_CompareImages(ComponentContainer container) {
        super(container.$form());
        this.container = container;
    }

////////////////////// FUNCIONES //////////////////////////////////////////////////////////////////////
@SimpleFunction(description = "pathImage, absolute address, example: /mnt/sdcard/image.png or start with file:// example file:///mnt/sdcard/image.png. Get a decimal number 0 to 100.")
public double CompareImages(String pathImage1, String pathImage2){	
     pathImage1 = pathImage1.replaceFirst("file://", "");
	 bmp1 = BitmapFactory.decodeFile(pathImage1);  
	 bmp1 = bmp1.copy(Bitmap.Config.ARGB_8888 , true);
		
	 pathImage2 = pathImage2.replaceFirst("file://", "");
	 bmp2 = BitmapFactory.decodeFile(pathImage2);  
	 bmp2 = bmp2.copy(Bitmap.Config.ARGB_8888 , true);	
		
        // Assigning dimensions to image
		 int width1 = bmp1.getWidth();
         int height1 = bmp1.getHeight();
		 int width2 = bmp2.getWidth();
         int height2 = bmp2.getHeight();

        // Checking whether the images are of same size or not
        if ((width1 != width2) || (height1 != height2))
		   percentage = 0.0;
        else {
            // By now, images are of same size
            long difference = 0;
  
            // treating images likely 2D matrix
            for (int y = 0; y < height1; y = y + 2) {      // original y++
                for (int x = 0; x < width1; x = x + 2) {   // original x++
				    int rgbA = bmp1.getPixel(x, y);
                    int rgbB = bmp2.getPixel(x, y);

                    int redA = (rgbA >> 16) & 0xff;
                    int greenA = (rgbA >> 8) & 0xff;
                    int blueA = (rgbA)&0xff;
                    int redB = (rgbB >> 16) & 0xff;
                    int greenB = (rgbB >> 8) & 0xff;
                    int blueB = (rgbB)&0xff;
  
                    difference += Math.abs(redA - redB);
                    difference += Math.abs(greenA - greenB);
                    difference += Math.abs(blueA - blueB);
                }
            }
  
            // Total number of red pixels = width * height
            // Total number of blue pixels = width * height
            // Total number of green pixels = width * height
            // So total number of pixels = width * height *
            // 3
            double total_pixels = width1 * height1 * 3;
  
            // Normalizing the value of different pixels for accuracy
            // Note: Average pixels per color component
            double avg_different_pixels = difference / total_pixels;
  
            // There are 255 values of pixels in total
            percentage = (avg_different_pixels / 255) * 100;		   
        }
return percentage;		
    }

}	// ==> FIN
3 Likes

3.- Comments.

I have used Bitmap to work with the images.

I have changed this line to get faster.
for (int y = 0; y < height1; y = y + 2) { // original y++
for (int x = 0; x < width1; x = x + 2) { // original x++

2 Likes

I want to create program for inspection PCB. is it possible ?

Which extension help to me?

Thanks.

Can I open a new topic on this subject?

Thanks

To inspect a PCB, you would have to have an identical image of the perfect PCB and take an identical photo of the damaged PCB, which is probably not possible. I think there would have to be AI to check PCBs.

1 Like

Has there been an artificial intelligence study similar to this before? I can take an example. for other materials

1 Like

but you could compare group pixels using crop method and get result or no?

The camera will be on all the time and is it possible to do this instantly?

Actually I want to make my burglar alarm. This project seems suitable for this. It will compare the continuous images. It will write the difference value to the screen. I will ring the alarm according to the value range. My first problem is can I do this image comparison instantly.

1 Like

Can you help me? Thank you in advance for taking the time to comment.

How do I turn it into an anti-theft alarm app?



[mod edit - do not provide direct links to extensions that are not yours]
p120_compararimagen.aia (1.4 MB)

Thank you for helping.
I saw this app being shared. I want to update it.
How to turn it into an anti-theft alarm app?

Where are you having a problem ?

I want the camera to be on all the time and to sound an alarm when it detects motion. How can I do it? Thank you. I did such a study about the sound but I don't know how to do it about the image. I'm trying to learn, thank you for your understanding.

Well, this extension compares two images, it does not detect motion from a live video feed.

What you can do is take photos on a regular basis, and compare with the original. You might need Taifun's Camera extension for this.