473,386 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to Read/Write image files? compute differences in 2 pictures...

I need to compute the differences in color between 2 pictures. Also, I need to write a method that computes a useful difference color of two colors (where the colors and their difference will be represented as single int's). The formula you should use for colordiff, the difference color between color1 and color2, with color1 = (R1, G1, B1) and color2 = (R2, G2, B2) is as follows:
colordiff = (Rdiff, Gdiff, Bdiff) where
Rdiff = |R1-R2| Gdiff = |G1-G2| Bdiff = |B1-B2|
here's what I have:
Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.awt.image.*;
  3. import javax.imageio.*;
  4. import java.io.*;
  5.  
  6. public class ImageDiff 
  7. {
  8.  
  9.   public static void main (String args[]) throws IOException 
  10.   {
  11.     int startX, startY, endX, endY, color1, color2;
  12.  
  13.     File imageFile1 = new File("pisa1.jpg");
  14.     BufferedImage image1 = ImageIO.read(imageFile1);
  15.     File imageFile2 = new File("pisa2.jpg");
  16.     BufferedImage image2 = ImageIO.read(imageFile2);
  17.  
  18.     startX = image1.getMinX();
  19.     startY = image1.getMinY();
  20.     endX = startX + image1.getWidth();
  21.     endY = startY + image1.getHeight();
  22.  
  23.     for (int i=startX; i<endX; i++) {
  24.       for (int j=startY; j<endY; j++) {
  25.         color1 = image1.getRGB(i,j);
  26.         color2 = image2.getRGB(i,j);
  27.         image1.setRGB(i,j,color1-color2);
  28.       }
  29.     }
  30.  
  31.     File outImage = new File("pisadiff.jpg");
  32.     ImageIO.write(image1, "jpg", outImage);
  33.   }
  34.  
  35.   // Converting R-G-B values into a single integer
  36.   public static int RGBint (int R, int G, int B) 
  37.   {
  38.     return (R*256*256+G*256+B);
  39.   }
  40.  
  41.   // grab the R value from a single integer
  42.   public static int getRFromRGB (int color) 
  43.   {
  44.     return ((color & 0x00FF0000) >> 16);
  45.   }
  46.  
  47.   // grab the G value from a single integer
  48.   public static int getGFromRGB (int color) 
  49.   {
  50.     return ((color & 0x0000FF00) >> 8);
  51.   }
  52.  
  53.   // grab the B value from a single integer
  54.   public static int getBFromRGB (int color) 
  55.   {
  56.     return (color & 0x000000FF);
  57.   }
  58. }
  59.  
Mar 20 '08 #1
0 1717

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: bborden | last post by:
Novice Access programmer here. I would like to display an image using the Toolbox Image object by calling the images file name using: =fPictureFiles(!!,1) in the Picture control. Below...
1
by: news.microsoft.com | last post by:
Hello and first of all thank you for your time. I am currently developing an ASP.net web page, which has data stored in SQL Server. It is a web page for a State Agent and my problem is: I need...
9
by: EricJ | last post by:
hi all the situation there is a picbox w a drawing, if the user clicks somewhere on the drawing a cross is added where he clicked (the cross is a pic in a new picture box), he can add as many...
1
by: goldtech | last post by:
Is there a way to create thumnails with a script in PHP with safe mode ON? We want to keep safe mode ON for our server but beable to automatically create thumbs from full size image files. Is...
5
by: Buchwald | last post by:
hello group, I have a long (large) script that shows a random picture when a webpage is refreshed. It's long because i have a lot of pictures: 246 Here is some code:...
5
by: news.microsoft.com | last post by:
Hello, what is the most performant size for the byte array for reading/writing using 2 filestreams? example code: Dim bytearrayinput(4095) As Byte Dim rdlen As Long = 0 Dim totlen As Long...
3
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
3
by: =?Utf-8?B?ZGF2aWQ=?= | last post by:
I try to follow Steve's paper to build a database, and store a small text file into SQL Server database and retrieve it later. Only difference between my table and Steve's table is that I use NTEXT...
14
by: Zoro | last post by:
My task is to read html files from disk and save them onto SQL Server database field. I have created an nvarchar(max) field to hold them. The problem is that some characters, particularly html...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.