Inside Collection (Course): Object-Oriented Programming (OOP) with Java
Summary: Learn how to merge pictures.
This module is one of a series of modules designed to teach you about Object-Oriented Programming (OOP) using Java.
The program described in this module requires the use of the Guzdial-Ericson multimedia class library. You will find download, installation, and usage instructions for the library at Java OOP: The Guzdial-Ericson Multimedia Class Library .
I recommend that you open another copy of this document in a separate browser window and use the following links to easily find and view the images and listings while you are reading about them.
In this module, you will learn how to do a linear merge on two pictures based on the distance of each pixel from the left side of the picture.
Program specifications
Write a program named Prob05 that uses the class definition shown in Listing 1 and Ericson's media library along with the image files named Prob05a.jpg and Prob05b.jpg (see Image 1 and Image 2 ) to produce the graphic output image shown in Image 3 .
| Image 1: Input file named Prob05a.jpg. |
|---|
![]() |
| Image 2: Input file named Prob05b.jpg. |
|---|
![]() |
| Image 3: Required graphic output image. |
|---|
![]() |
Required output text
In addition to the output image mentioned above, your program must display your name and the other line of text shown in Image 4 on the command-line screen.
| Image 4: Required output text. |
|---|
|
This program does a linear merge on two pictures based on the distance of each pixel from the left side of the picture. The program also adds a sun with a gradient and the student's name to the picture.
Will discuss in fragments
I will discuss and explain this program in fragments. A complete listing of the program is provided in Listing 6 near the end of the module.
The driver class named Prob05
The driver class containing the main method is shown in Listing 1 .
| Listing 1: The driver class named Prob05. |
|---|
|
The code in Listing 1 shouldn't require an explanation at this stage in the course.
Beginning of the class named Prob05Runner
The class named Prob05Runner begins in Listing 2 .
| Listing 2: Beginning of the class named Prob05Runner. |
|---|
|
As with Listing 1 , the code in Listing 2 shouldn't require an explanation.
The run method of the Prob05Runner class
Listing 1 calls the run method on an object of the Prob05Runner class. The run method is shown in its entirety in Listing 3 .
| Listing 3: The run method of the Prob05Runner class. |
|---|
|
The only thing in Listing 3 that I haven't explained in earlier modules is the call to the merge method, so I will limit my discussion to that method.
The merge method
The merge method is used to merge the image in Image 1 with the image in Image 2 to produce the image shown in Image 3 .
(Note, however, that the merged image was cropped to eliminate the buttons at the top of F igure 1 and Image 2 before displaying it in Image 3 .)
A linear merge
The merge method does a linear merge on two pictures based on the distance of each pixel from the left side of the picture.
The method assumes that both pictures have the same dimensions.
Beginning of the merge method
The merge method begins in Listing 4 .
| Listing 4: Beginning of the merge method. |
|---|
|
The code in Listing 4 simply declares and initializes a large number of working variables.
Do the merge
The merge is accomplished in the nested for loop in Listing 5 .
| Listing 5: Do the merge. |
|---|
|
Difficult to explain
Although the code in Listing 5 is long, tedious, and ugly, it isn't complicated However, it is somewhat difficult to explain in words.
Two scale factors
The body of the for loop begins by computing a pair of scale factors named scaleR and scaleL . The factor named scaleR has a maximum value of 1.0 and is directly proportional to the distance of the current pixel from the left edge of the picture.
The factor named scaleL also has a maximum value of 1.0 and is inversely proportional to the distance of the pixel from the left edge.
Get and save color components
The red, green, and blue values for the same pixel location in each of the pictures are obtained and saved.
Compute a new set of color values
A new set of red, green, and blue color values are computed as the sum of scaled versions of the pixel colors from the rabbit image pixel and the penguin image pixel.
The nature of the scaling
The scaling is such that the pixel colors from the rabbit image contribute most heavily to output pixels to the left of center and pixel colors from the penguin image contribute most heavily to output pixels to the right of center.
You can see the effect of this scaling algorithm in Image 3 .
The pixels in the horizontal center
The pixels along a vertical line at the center of the output image contain equal contributions of colors from both images.
End of discussion
That concludes the explanation of the code in this program.
You can view the drawSun and crop methods in Listing 6 near the end of the module.
I encourage you to copy the code from Listing 6 . Compile the code and execute it. Experiment with the code, making changes, and observing the results of your changes. Make certain that you can explain why your changes behave as they do.
Click Prob05a.jpg and Prob05b.jpg to download the required input image files.
In this module, you learned how to merge two pictures.
Select the following link to view an online video lecture on the material in this module.
This section contains a variety of miscellaneous information.
Financial : Although the Connexions site makes it possible for you to download a PDF file for this module at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should be aware that some of the HTML elements in this module may not translate well into PDF.
I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. I neither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it was made and published without my prior knowledge.
Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.
A complete listing of the program discussed in this module is provided in Listing 6 below.
| Listing 6: Complete program listing. |
|---|
|
-end-