Founder Lê Trần Đạt asked “Có lẽ đã đến lúc lập trình viên nên học AI/ML”. AI/ML learning is individual and depends on the learner’s abstract imagination. E.g.: AI with fuzzy logic.
First of all: Human intelligence (HI) is fuzzy. This means that we recognize things analogously, while computers work digitally and AI is based on computers. Or in other words, HI is continuous or fuzzy and AI is discrete (click HERE for more information on continuous and discrete). If you want to learn AI, you should first understand the state of an observed thing, which can be clear or fuzzy. Humans mostly think fuzzy, but humans can still draw a correct conclusion from the fuzzy state. For example, we can easily recognize a face with or without a beard. We can do it because we apply the face with certain similarity probability. Anyway, face recognition is a real challenge for AI and it can only be achieved using Fuzzy Logic (invented by Lotfi Zadeh).
I won’t go into detail about Fuzzy Logic or face recognition, but I will present you an example that will show you how Fuzzy Logic can be easily implemented for image recognition. Note: correction after @Stanley00 's hint.
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
// Joe (C)
public class Fuzzy {
public static void main(String... argv) throws Exception {
if (argv.length != 3) {
System.out.println("Usage: java Fuzzy image_file_1 image_file_2 Probability\n"+
"Example: java Fuzzy cat_1.jpg cat_2.jpg 0.2");
System.exit(0);
}
System.out.println("Discrete:"+discreteEqual(argv[0], argv[1])+
"\nFuzzy :"+fuzzyEqual(argv[0], argv[1], Float.parseFloat(argv[2])));
}
/*
Parameter p is the probability (0.0 .. 1.0) that a match will yield
if 2 images are similar: 2 images only deviate from each other in p percent
*/
public static boolean fuzzyEqual(String fImg1, String fImg2, float p) throws Exception {
BufferedImage img1 = ImageIO.read(new File(fImg1));
BufferedImage img2 = ImageIO.read(new File(fImg2));
int w1 = img1.getWidth(), h1 = img1.getHeight();
//
int s = 0, t = 0;
for (int x, y = 0; y < h1; ++y) for (x = 0; x < w1; ++x) {
if (img1.getRGB(x, y) == img2.getRGB(x, y)) ++s;
++t;
}
float m = (float)s/t;
if (p == 0 && m != 1) return false;
if (p == 1 && m >= 0 || p == 0 && m == 0) return true;
return (p >= m || p >= (1-m));
}
/*
Absolute compare or discrete compare
*/
public static boolean discreteEqual(String fImg1, String fImg2) throws Exception {
BufferedImage img1 = ImageIO.read(new File(fImg1));
BufferedImage img2 = ImageIO.read(new File(fImg2));
int w1 = img1.getWidth(), h1 = img1.getHeight();
if (w1 != img2.getWidth() || h1 != img2.getHeight()) return false;
for (int x, y = 0; y < h1; ++y) for (x = 0; x < w1; ++x) {
if (img1.getRGB(x, y) != img2.getRGB(x, y)) return false;
}
return true;
}
}
and the results with 3 similar images (the right eye of cat_3.jpg has a different hue) are:
Cat_1.jpg
Cat_2.jpg (blurred Cat_1)
Cat_3.jpg (slightly different hue of right eye)
With 10% (0.1) and 20% (0.2) dissimilarity
C:\JFX\image\Test>java Fuzzy cat_1.jpg cat_1.jpg 0.1
Discrete:true
Fuzzy :true
C:\JFX\image\Test>java Fuzzy cat_1.jpg cat_2.jpg 0.1
Discrete:false
Fuzzy :false
C:\JFX\image\Test>java Fuzzy cat_1.jpg cat_2.jpg 0.2
Discrete:false
Fuzzy :true
C:\JFX\image\Test>java Fuzzy cat_3.jpg cat_2.jpg 0.2
Discrete:false
Fuzzy :true
C:\JFX\image\Test>java Fuzzy cat_3.jpg cat_2.jpg 0.1
Discrete:false
Fuzzy :false