我需要比较两个相似图像中的像素,创建一个保存的第三个图像,其中两个图像之间相同的像素变为蓝色。代码还需要在开始比较之前检查图像是否具有相同的大小,如果它们的大小不同,则会向控制台退出并出错。
这是我的代码。我仍然需要覆盖两个图像并将相似的像素变成蓝色。
我需要使用Filechooser.pickAFile,但除此之外,我完全迷失了如何去做。
// Karl Thomas
// kthoma34
// Mon @ 4:00
import java.awt.Color;
public class PP2kthoma34
{
public static void main (String[] args)
{
// Original picture
Picture p1; // create the variable
String fileName = FileChooser.pickAFile();
FileChooser.setMediaPath ( fileName );
System.out.println (fileName);
p1 = new Picture( fileName );
// Width and length of original picture
int width1 = p1.getWidth();
int height1 = p1.getHeight();
Pixel[] pixelArray1 = p1.getPixels();
System.out.println(pixelArray1.length + "pixels");
System.out.println("");
// Modified picture
Picture p2; // create the variable
String filename2;
filename2 = FileChooser.pickAFile();
FileChooser.setMediaPath ( filename2 );
System.out.println (filename2);
p2 = new Picture( filename2 );
//Width and Height of manipulated picture
int width2 = p2.getWidth();
int height2 = p2.getHeight();
Pixel[] pixelArray2 = p2.getPixels();
System.out.println(pixelArray2.length + "pixels");
System.out.println("");
//checking that the images are the same size
if ((width1 != width2) || (height1 != height2))
{
System.err.println("Error: Image dimensions do not match");
System.exit(1);
}
// Save Third image with similarities highlighted in blue.
String filename3;
filename3 = FileChooser.pickAFile();
p2.write( filename3 );
}
}// end
答案 0 :(得分:0)
这可以帮助你:(Java loop through pixels in an image)
这是一个回答的线程,关于使用BufferedImage循环一个图像,因此你应该能够在Java API中找到大量的explenation(关于使用和函数)
在回答的帖子中,它还展示了如何将图像的像素设置为特定的颜色。
请记住,该线程中有关着色的所有内容都应该根据您的需求进行定制。
即你必须首先使用正确的测量值创建第三张图片(再次使用BufferedImage),然后检查两张图片中所选像素的颜色是否相互匹配,如果它们匹配,则第三张图片将相同的像素设置为蓝色,如果没有,你会做你想到的。
如果您需要更多细节,只需说出这个词,我就会看到我能为您做些什么。
编辑:(16.03.2015)
以下是一个完全按照你的魔杖工作的工人阶级。我试图尽可能清楚地评论所有内容,但根据经验我知道这对其他人来说并不总是很清楚。随意以您想要的任何方式使用此代码。
快速免责声明:您必须将软件包名称更改为您自己的软件包,并且需要在main.class中创建此类的Object。尽可能少地编码到主类中总是更漂亮,更有效。
比较班级
package stackexchange;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class PictureOverlayTest {
/*
* Four variables, three for the wanted BufferedImages, one String for the
* Path of the third Image, which does not already exist.
*/
private BufferedImage image1;
private BufferedImage image2;
private BufferedImage image3;
private String pathImage3;
public PictureOverlayTest(String filePathAndName1, String filePathAndName2,
String filePathAndName3) throws IOException {
/*
* Constructor in order to keep this method reusable and clean. Needs
* three Strings. The paths and Filenames of all three images. Image 1
* and 2 should exist already, Image 3 will be created if all
* requirements are met. Constructor creates the first two buffered
* images, sets all needed variables and starts the checkAndCompare()
* method
*/
File file = new File(filePathAndName1);
this.image1 = ImageIO.read(file);
file = new File(filePathAndName2);
this.image2 = ImageIO.read(file);
this.pathImage3 = filePathAndName3;
checkAndCompare();
}
private void checkAndCompare() throws IOException {
/*
* This function creates the Color blue, compares the sizes of both
* pictures and if they are the same, creates a third image. Then it
* loops through the two images and compares each pixel. If the pixels
* are the same, the third image gets a blue pixel at that point
*/
Color blue = Color.blue;
if (image1.getHeight() == image2.getHeight()
&& image1.getWidth() == image2.getWidth()) {
image3 = new BufferedImage(image1.getWidth(), image1.getHeight(),
image1.getType());
for (int y = 0; y < image1.getHeight(); y++) {
for (int x = 0; x < image1.getWidth(); x++) {
int colorImage1 = image1.getRGB(x, y);
int colorImage2 = image2.getRGB(x, y);
if (colorImage1 == colorImage2) {
image3.setRGB(x, y, blue.getRGB());
} else {
// Whatever Color you want. By default it is black.
}
}
}
savePicture3();
System.out.println("Message: Image comparison is done");
} else {
System.out.println("Error: Image dimensions do not match");
}
}
private void savePicture3() throws IOException {
/*
* This method saves the created Image into a file onto your computer.
* The if() statement is used to check if the file was successfully
* created, in order to avoid unwanted errors. Keep in mind, that you
* have to change the "bmp" in ImageIO.write() to whatever format you
* actually want
*/
File file = new File(pathImage3);
if (file.createNewFile()) {
ImageIO.write(image3, "bmp", file);
}
}
}
主要类
package stackexchange;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
PictureOverlayTest test = new PictureOverlayTest(
"H:\\stackexchange\\file1.bmp",
"H:\\stackexchange\\file2.bmp",
"H:\\stackexchange\\file3.bmp");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}