如何从给定的差异像素值组成图像?

时间:2014-01-31 04:42:40

标签: 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.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);
    }
}

1 个答案:

答案 0 :(得分:0)

为您想要的尺寸创建一个新图像

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

然后访问各个像素以“绘制”

image.setRGB(x, y, rgb);

您的最新代码很接近,但有一些问题......

您不需要任何“图形”代码。删除此

Graphics g=res.getGraphics();

和这个

g.drawImage(res,0,0,null);
在您的JFrame中添加2个面板

。只需使用一个。

删除

JPanel result=new JPanel();

并删除

result.add(resl);
frame.add(result);

只需将新图片添加到现有面板

即可
picPanel.add(resl)