Java 如何从给定的一组差异像素值形成图像?
我已经找到了差异像素。我需要将这些差异像素值分组到区域中。应该随机选择一个种子像素。现在该区域必须增长为8个连接区域。我在这里附上了我迄今为止实现的代码。因此请建议我执行上述操作。Java 如何从给定的一组差异像素值形成图像?,java,Java,我已经找到了差异像素。我需要将这些差异像素值分组到区域中。应该随机选择一个种子像素。现在该区域必须增长为8个连接区域。我在这里附上了我迄今为止实现的代码。因此请建议我执行上述操作。 import java.io.IOException; import javax.imageio.ImageIO; import java.io.File; import java.awt.image.BufferedImage; import java.io.BufferedWriter; import java.
import java.io.IOException;
import javax.imageio.ImageIO;
import java.io.File;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.Scanner;
import java.awt.Image;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
public class imageMSE
{
public static String img;
public static int no;
public static BufferedImage[] image;
public static BufferedImage[] res;
public static void getImage()
{
Scanner in=new Scanner(System.in);
System.out.println("\nPLEASE ENTER THE TOTAL NUMBER OF IMAGES:");
no=in.nextInt();
System.out.println("\nTHE NUMBER OF IMAGES ARE: "+no);
image=new BufferedImage[no];
for(int i=0;i<no;i++)
{
image[i]=null;
}
try
{
Scanner in1=new Scanner(System.in);
System.out.println("\nPLEASE ENTER THE PATH FOR IMAGES:");
for(int i=0;i<no;i++)
{
System.out.println("\nPLEASE ENTER THE PATH FOR IMAGE["+(i+1)+"] :");
img=in1.nextLine();
image[i] = ImageIO.read(new File(img));
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void getPixelValue()
{
int width=image[0].getWidth(null);
int height=image[0].getHeight(null);
int[][][] color=new int[no][width][height];
int scount=0;
int dcount=0;
int err=0;
int sqr=0;
int sum=0;
double simper;
res=new BufferedImage[no];
for(int i=0;i<no;i++)
{
res[i]=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
}
int [][]com1=new int[width][height];
int [][]com2=new int[width][height];
for(int ino=1;ino<no;ino++)
{
for(int i=0;i<width;i++)
{
for(int j=0;j<height;j++)
{
com1[i][j]=image[0].getRGB(i,j);
com2[i][j]=image[ino].getRGB(i,j);
if(com1[i][j]==com2[i][j])
{
scount++;
}
else
{
sqr=0;
dcount++;
err=com1[i][j]-com2[i][j];
sqr=err*err;
res[ino].setRGB(i,j,com2[i][j]);
sum+=sqr;
// if(err>500000)
// System.out.println("the error value is : "+err);
}
}
}
float mul=width*height;
System.out.println("the no of pixel similar for "+ino+ "th image is "+scount);
System.out.println("the no of pixel gets varied for "+ino+ "th image is "+dcount);
System.out.println("Mean Square error is : "+sum);
simper=(100*scount)/mul;
System.out.println("the percentage of similarity for the 1th image and "+(ino+1)+ "th image is "+simper);
sum=0;
dcount=0;
scount=0;
}
}
public static void main(String[] args)
{
getImage();
getPixelValue();
JFrame frame = new JFrame();
JLabel[] label=new JLabel[no];
JLabel resl[]=new JLabel[no];
JPanel picPanel=new JPanel();
picPanel.setLayout(new GridLayout(5,5,2,2));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000, 1000);
for(int i=0;i<no;i++)
{
label[i] =new JLabel( new ImageIcon(image[i]));
label[i].setSize(100,100);
picPanel.add(label[i]);
}
for(int i=1;i<no;i++)
{
resl[i]=new JLabel(new ImageIcon(res[i]));
picPanel.add(resl[i]);
}
frame.add(picPanel);
frame.setVisible(true);
}
}
导入java.io.IOException;
导入javax.imageio.imageio;
导入java.io.File;
导入java.awt.image.buffereImage;
导入java.io.BufferedWriter;
导入java.io.FileWriter;
导入java.util.Scanner;
导入java.awt.Image;
导入javax.swing.*;
导入java.awt.*;
导入java.awt.image.buffereImage;
公共类imageMSE
{
公共静态字符串img;
公共静态int no;
公共静态缓冲区映像[]映像;
公共静态缓冲区映像[]res;
公共静态void getImage()
{
扫描仪输入=新扫描仪(系统输入);
System.out.println(“\n请输入图像总数:”);
否=in.nextInt();
System.out.println(“\n图像数为:“+no”);
图像=新的缓冲区图像[否];
对于(int i=0;i为所需大小创建一个新图像
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
然后访问各个像素以“绘制”
您的最新代码很接近,但有一些问题
您不需要任何“图形”代码。请删除此代码
Graphics g=res.getGraphics();
还有这个
g.drawImage(res,0,0,null);
在JFrame中添加了两个面板。只需使用一个面板即可
删除
JPanel result=new JPanel();
删除
result.add(resl);
frame.add(result);
只需将新图像添加到现有面板
picPanel.add(resl)
你所说的draw是什么意思?在计算错误值后,我将res.setRGB(I,j,err)添加到了else部分的getPixelValue函数中,我已将代码修改为下面的一个。但图像未显示。您仍然需要将新图像添加到JPanel,就像添加其他图像一样。请编辑您的问题,使其包含最新的代码。我已包含JPanel,但未获得输出。现在我已编辑了代码。
picPanel.add(resl)