Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Reduce the image comparison time

  1. #1
    Junior Member
    Join Date
    Apr 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Reduce the image comparison time

    Here's a similar percentage of the two images. I would like to ask if there is a way to reduce nanotime to below 80000000 nano sec.
     public static void main(String[] args) 
    	    { 
     
    		    long startTime = System.nanoTime();
    	        BufferedImage imgA = null; 
    	        BufferedImage imgB = null; 
     
     
    	        try
    	        { 
     
    	            File fileA = new File("C:\\Users\\rx-78-1.jpg"); 
    	            File fileB = new File("C:\\Users\\rx-78-2.jpg"); 
     
     
    	            imgA = ImageIO.read(fileA); 
    	            imgB = ImageIO.read(fileB); 
     
    	        } 
    	        catch (IOException e) 
    	        { 
    	            System.out.println(e); 
    	        } 
     
     
                imgA = resize(imgA, 8, 8);
                imgB = resize(imgB, 8, 8);
     
     
    	        int width1 = imgA.getWidth(); 
    	        int width2 = imgB.getWidth(); 
    	        int height1 = imgA.getHeight(); 
    	        int height2 = imgB.getHeight(); 
     
     
    	        	//Take the smaller image width and height
    	        	if(width1>width2) width1 = width2;
    	        	if(height1>height2) height1 = height2;
     
     
    	        long difference = 0; 
    	        for (int y = 0; y < height1; y++){ 
    	        	for (int x = 0; x < width1; x++){ 
    	                    int rgbA = imgA.getRGB(x, y);
    	                    int rgbB = imgB.getRGB(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 rgb data = width * height * 3 
    	       double total_rgb_data = width1 * height1 * 3; 
     
    	       // average pixels per rgb
    	       double avg_different_pixels = difference / total_rgb_data; 
     
    	       // There are 255 values of pixels in total 
    	       double percentage = (avg_different_pixels / 255) * 100; 
     
    	       System.out.println("Similarity Percentage::" + (100-percentage) +" %"); 
     
    	       long endTime = System.nanoTime();
    	       System.out.println("Time taken::"+(endTime - startTime)+" nano sec");
     
    	    }//main() ends here 
     
    	    private static BufferedImage resize(BufferedImage image, int width, int height) {
    	        BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    	        Graphics2D g = resizedImage.createGraphics();
    	        g.drawImage(image, 0, 0, width, height, null);
    	        g.dispose();
    	        return resizedImage;
    	    }
     
    	}//class ends here
    rx-78-1.jpg
    rx-78-2.jpg

  2. #2
    Member Helium c2's Avatar
    Join Date
    Nov 2023
    Location
    Kekaha, Kaua'i
    Posts
    110
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Reduce the image comparison time

    Would this make a difference? What is the average waiting time now? About 2-3 seconds. For an average file size. Smaller or larger.

Similar Threads

  1. Image comparison
    By klenam in forum The Cafe
    Replies: 1
    Last Post: March 18th, 2014, 07:40 AM
  2. About deleting image on a certain time without the pausing
    By poldz123 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 18th, 2013, 07:26 AM
  3. Problem with image crop and pixel comparison code
    By tuathan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 29th, 2012, 09:08 AM
  4. Problem in rotating and moving image at the same time
    By SlimShady in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 17th, 2010, 02:33 PM
  5. Changing colors of large image in real time
    By chals in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: May 7th, 2009, 05:06 AM

Tags for this Thread