Reputation: 924
I need to compare two images of similar kind but both have different size in JAVA, i.e one has low background width and another one has high background width. And size of images are also different. I have attached two files. In which person and t-shirt are same so I need to show result as Image is Same. But when I go with pixel wise image matching then it does not show me the true result due to different width of background. Then I have tried to remove background and then comparing it still same issue. Please fine the attached images and code. Please Help Links to images : Image one& Image two
Code:
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JOptionPane;
public class ImageProcesing extends Canvas {
private static int x1 = 0, y1 = 0;
private static int h, w;
private static final Random random = new Random();
private Color mycolor;
BufferedImage img, img1;
public BufferedImage scaleImage(int WIDTH, int HEIGHT, String filename) {
BufferedImage bi = null;
try {
ImageIcon ii = new ImageIcon(filename);//path to image
h = 512;
w = 512;
bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) bi.createGraphics();
g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
g2d.drawImage(ii.getImage(), 0, 0, w, h, null);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return bi;
}
public BufferedImage scaleImage2(String filename) {
BufferedImage bi = null;
try {
ImageIcon ii = new ImageIcon(filename);//path to image
bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) bi.createGraphics();
g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
g2d.drawImage(ii.getImage(), 0, 0, w, h, null);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return bi;
}
public ImageProcesing() {
try {
this.img = scaleImage(512, 512, "D:\\I Tech Solutions\\Rahul Ratda\\Experiments\\1.jpeg");
this.img1 = scaleImage2("D:\\I Tech Solutions\\Rahul Ratda\\Experiments\\3.jpeg");
} catch (Exception ex) {
Logger.getLogger(ImageProcesing.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void paint(Graphics g) {
int n = 0, k = 0, l = 0;
int tp = 0;
super.paint(g);
Color oldColor = new Color(img1.getRGB(0, 0));
for (int x = 0; x < h; x++) {
tp = 0;
w=512;
for (int y = 0; y < w; y++) {
oldColor = new Color(img1.getRGB(y, x));
mycolor = new Color(img.getRGB(y, x));
if (tp == 0 && oldColor.equals(Color.WHITE)) {
continue;
} else {
if (tp == 0) {
tp = 1;
w = w - y;
}
k++;
if ((mycolor.equals(oldColor))) {
g.setColor(mycolor);
g.drawLine(y, x, y, x);
n++;
}
}
}
}
System.out.println("K : "+k+"\n N : "+n);
if (n >= (k * 0.70)) {
System.out.println("Same");
}
else
System.out.println("Not Same");
/* oldColor=new Color(img1.getRGB(0,0));
for(int i = 0 ; i < WIDTH1; i++) {
for(int y = 0; y < HEIGHT1; y++) {
mycolor=new Color(img1.getRGB(i,y));
if((mycolor.equals(oldColor))){
y1++;
g.setColor(mycolor);
g.drawLine(i, y, i, y);
}
else
oldColor=mycolor;
}
}*/
/*if(x1>y1)
{
if(x1*0.6<y1)
JOptionPane.showMessageDialog(null,"Images are More than 60% Equal.");
else
JOptionPane.showMessageDialog(null,"Images are Less than 60% Equal.");
}
else{
if(y1*0.6<x1)
JOptionPane.showMessageDialog(null,"Images are More than 60% Equal.");
else
JOptionPane.showMessageDialog(null,"Images are Less than 60% Equal.");
}*/
}
/* private Color randomColor() {
return new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
}*/
public static void main(String[] args) {
JFrame frame = new JFrame();
System.out.println("width = " + w);
System.out.println("height = " + h);
frame.setSize(1000, 1000);
frame.add(new ImageProcesing());
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Upvotes: 0
Views: 395
Reputation: 868
You can develop your own algorithm for this kind of structure. As I can see that background of your images are white so first of all you perform background removal operation on your images.
Then you resize your image to lower level like 256x256 or something likethat. No you have only object in the image as color is already moved out. So you can go with your pixel by pixel comparison and keeping thrshold of 84-90% would give you expected result which you want.
Direct image comparison by pixel by pixel will not work as images are different in size as well as backgrounds space is also different and also objects size varies.
Upvotes: 1
Reputation: 43
You need to apply some image processing technique like histogram before comparing the images. Java API of opencv is a nice tool for such operations.See the link
Upvotes: 0