Results 1 to 3 of 3
  1. #1
    Wahoowa is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default Comparison of pixels across images?

    Please see post R3 for a (close as I can get) SSCCE code and a very basic description of the problem.



    So I'm attempting to create a program to compare the color intensity of pixels across images. Specifically for my task, I'm attempting to compare the average intensity of red in one image with the intensity of green in matching pixels in another image. This task doesn't seem too difficult, but I also have to allow certain regions of the images to be selected (same region in each image) and only those regions. I have a version of the region selection, but I'm trying to figure out how to make the selection actually transfer to the image I'm analyzing. The code is to test for calcium concentrations in regions of mitochondria vs regions without mitochondria in a cell. Calcium is green, mitochondria is red. I chose yellow as the selection color because blue may be coming into a third image later, but if I can get help actually moving the region into the BufferedImage itself. I have the pseudocode for the scanning of the pixels and regions written elsewhere. I really just need help selecting the region WITHIN the image instead of just in the JPanel.

    Main window:

    Java Code:
    import java.awt.Color;
    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JFileChooser;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.util.ArrayList;
    
    import javax.swing.JMenu;
    
    
    @SuppressWarnings("serial")
    public class Menu extends JFrame {
    
    	private JPanel contentPane;
    	public File mitoFile, calciumFile;
    	private Calcium calcWindow;
    	private Mitochondrial mitoWindow;
    	public BufferedImage mitoPicture, calcPicture;
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					Menu frame = new Menu();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public Menu() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 443, 59);
    		
    		JMenuBar menuBar = new JMenuBar();
    		setJMenuBar(menuBar);
    		
    		JMenu mnFile = new JMenu("File");
    		menuBar.add(mnFile);
    		
    		JMenuItem mntmOpenCalciumFile = new JMenuItem("Open Calcium File...");
    		mnFile.add(mntmOpenCalciumFile);
    		mntmOpenCalciumFile.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				JFileChooser fc = new JFileChooser();
    				fc.showOpenDialog(contentPane);
    				calciumFile = fc.getSelectedFile();
    				calcWindow = new Calcium(calciumFile);
    				calcWindow.setVisible(true);
    				calcPicture = calcWindow.getImage();
    			}
    		});
    		
    		JMenuItem mntmOpenFile = new JMenuItem("Open Mitochondria File...");
    		mnFile.add(mntmOpenFile);
    		mntmOpenFile.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				JFileChooser fc = new JFileChooser();
    				fc.showOpenDialog(contentPane);
    				mitoFile = fc.getSelectedFile();
    				mitoWindow = new Mitochondrial(mitoFile);
    				mitoWindow.setVisible(true);
    				mitoPicture = mitoWindow.getImage();
    			}
    		});
    		
    		JMenu mnGraphicsOperation = new JMenu("Graphics Operations");
    		menuBar.add(mnGraphicsOperation);
    		
    		JMenuItem mntmCompareImages = new JMenuItem("Compare Images");
    //		mntmCompareImages.addActionListener(new ActionListener() {
    //			public void actionPerformed(ActionEvent e) {
    //				if (mitoWindow.isVisible() && calcWindow.isVisible()) {
    //					mitoWindow.scanImage();
    //					calcWindow.scanImage();
    //					boolean inCell = false;
    //					for (int i = 0; i < 30; i++) {
    //						if (mitoWindow.rgbs[i] == Color.YELLOW.getRGB() && mitoWindow.rgbs[i] != Color.yellow.getRGB()) {
    //							inCell = !inCell;
    //						}
    //						if (mitoWindow.rgbs[i] == Color.yellow.getRGB() && mitoWindow.rgbs[i+1] == Color.yellow.getRGB()) {
    //							inCell = false;
    //						}
    //						if (inCell) {
    //							System.out.println("" + mitoWindow.rgbs[i]);
    //						}
    //						Color c = new Color(mitoWindow.rgbs[i], true);
    //						System.out.println(c.getRed() + ", " + c.getGreen() + ", " + c.getBlue());
    //					}
    //				}
    //			}
    //		});
    		mnGraphicsOperation.add(mntmCompareImages);
    		
    		JMenuItem TransferCircles = new JMenuItem("Transfer Circles");
    		mnGraphicsOperation.add(TransferCircles);
    		TransferCircles.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				if (calcWindow.isVisible() && mitoWindow.isVisible()) {
    					if (!calcWindow.yellowpoints.isEmpty()) {
    						mitoWindow.yellowpoints = calcWindow.yellowpoints;
    						mitoWindow.repaint();
    					}
    				}
    			}
    		});
    		
    		JMenuItem ClearCircles = new JMenuItem("Clear Circles");
    		mnGraphicsOperation.add(ClearCircles);
    		ClearCircles.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				if (calcWindow.isVisible()) {
    					calcWindow.yellowpoints.clear();
    					calcWindow.repaint();
    				}
    				if (mitoWindow.isVisible()) {
    					mitoWindow.yellowpoints.clear();
    					mitoWindow.repaint();
    				}
    			}
    		});
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    	}
    
    }
    Mitochondria window: (First image window)

    Java Code:
    import java.awt.Color;
    
    
    @SuppressWarnings("serial")
    public class Mitochondrial extends JFrame {
    
    	private JPanel drawingPane;
    	private BufferedImage mitoPicture;
    	public ArrayList<ArrayList<Point>> rows;
    	public ArrayList<ArrayList<Integer>> rgbrows;
    	private boolean drawingLine = false;
    	public ArrayList<Point> yellowpoints = new ArrayList<Point>();
    	private Point breakPoint = new Point(-1, -1);
    	private Point firstPoint;
    
    	public Mitochondrial(File imageFile) {
    		setBounds(100, 100, 450, 300);
    		drawingPane = new JPanel() {
    			@Override
    			public void paint(Graphics g) {
    				super.paint(g);
    				if (yellowpoints.size() > 1) {
    					for (int i = 1; i < yellowpoints.size(); i++) {
    						Point startPoint = yellowpoints.get(i-1);
    						Point endPoint = yellowpoints.get(i);
    						if (!endPoint.equals(breakPoint)) {
    							g.setColor(Color.YELLOW);
    							g.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
    						} else {
    							i = i+2;
    						}
    					}
    				}
    			}
    		};
    		setContentPane(drawingPane);
    		
    		drawingPane.addMouseListener(new MouseListener() {
    
    			@Override
    			public void mouseClicked(MouseEvent e) {
    			}
    
    			@Override
    			public void mouseEntered(MouseEvent arg0) {
    			}
    
    			@Override
    			public void mouseExited(MouseEvent arg0) {
    			}
    
    			@Override
    			public void mousePressed(MouseEvent e) {
    			}
    
    			@Override
    			public void mouseReleased(MouseEvent e) {
    				yellowpoints.add(e.getPoint());
    				yellowpoints.add(firstPoint);
    				yellowpoints.add(breakPoint);
    				drawingLine = false;
    				drawingPane.repaint();
    			}
    		});
    		drawingPane.addMouseMotionListener(new MouseMotionListener() {
    
    			@Override
    			public void mouseDragged(MouseEvent e) {
    				if (!drawingLine) {
    					firstPoint = e.getPoint();
    					yellowpoints.add(e.getPoint());
    					drawingLine = true;
    				} else {
    					yellowpoints.add(e.getPoint());
    					setContentPane(drawingPane);
    					drawingPane.repaint();
    				}
    			}
    
    			@Override
    			public void mouseMoved(MouseEvent e) {
    			}
    			
    		});
    		
    		try {
    			mitoPicture = ImageIO.read(imageFile);
    			int height = mitoPicture.getHeight();
    			int width = mitoPicture.getWidth();
    			this.setBounds(100, 100, 16+width, 40+height);
    			JLabel picLabel = new JLabel(new ImageIcon(mitoPicture));
    			drawingPane.add(picLabel);
    		} catch (IOException e) {
    		}
    	}
    	
    	public BufferedImage getImage() {
    		return mitoPicture;
    	}
    	
    	public void scanImage() {
    		int width = mitoPicture.getWidth();
    		int height = mitoPicture.getHeight();
    		for (int i = 0; i < height; i++) {
    			ArrayList<Integer> rgbs = new ArrayList<Integer>();
    			ArrayList<Point> points = new ArrayList<Point>();
    			for (int j = 0; j < width; j++) {
    				rgbs.add(mitoPicture.getRGB(j, i));
    				Point point = new Point(j, i);
    				points.add(point);
    			}
    			rgbrows.add(rgbs);
    			rows.add(points);
    		}
    	}
    }
    Calcium Window: (Second image window)

    Java Code:
    import java.awt.Color;
    
    
    @SuppressWarnings("serial")
    public class Calcium extends JFrame {
    
    	private JPanel drawingPane;
    	private BufferedImage calcPicture;
    	public ArrayList<ArrayList<Point>> rows;
    	public ArrayList<ArrayList<Integer>> rgbrows;
    	private boolean drawingLine = false;
    	public ArrayList<Point> yellowpoints = new ArrayList<Point>();
    	private Point breakPoint = new Point(-1, -1);
    	private Point firstPoint;
    	public Calcium(File imageFile) {
    		setBounds(100, 100, 450, 300);
    		drawingPane = new JPanel() {
    			@Override
    			public void paint(Graphics g) {
    				super.paint(g);
    				if (yellowpoints.size() > 1) {
    					for (int i = 1; i < yellowpoints.size(); i++) {
    						Point startPoint = yellowpoints.get(i-1);
    						Point endPoint = yellowpoints.get(i);
    						if (!endPoint.equals(breakPoint)) {
    							g.setColor(Color.YELLOW);
    							g.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
    						} else {
    							i = i+2;
    						}
    					}
    				}
    			}
    		};
    		setContentPane(drawingPane);
    		
    		drawingPane.addMouseListener(new MouseListener() {
    
    			@Override
    			public void mouseClicked(MouseEvent e) {
    			}
    
    			@Override
    			public void mouseEntered(MouseEvent arg0) {
    			}
    
    			@Override
    			public void mouseExited(MouseEvent arg0) {
    			}
    
    			@Override
    			public void mousePressed(MouseEvent e) {
    			}
    
    			@Override
    			public void mouseReleased(MouseEvent e) {
    				yellowpoints.add(e.getPoint());
    				yellowpoints.add(firstPoint);
    				yellowpoints.add(breakPoint);
    				drawingLine = false;
    				drawingPane.repaint();
    			}
    		});
    		drawingPane.addMouseMotionListener(new MouseMotionListener() {
    
    			@Override
    			public void mouseDragged(MouseEvent e) {
    				if (!drawingLine) {
    					firstPoint = e.getPoint();
    					yellowpoints.add(e.getPoint());
    					drawingLine = true;
    				} else {
    					yellowpoints.add(e.getPoint());
    					setContentPane(drawingPane);
    					drawingPane.repaint();
    				}
    			}
    
    			@Override
    			public void mouseMoved(MouseEvent e) {
    			}
    			
    		});
    		
    		try {
    			calcPicture = ImageIO.read(imageFile);
    			int height = calcPicture.getHeight();
    			int width = calcPicture.getWidth();
    			this.setBounds(100, 100, 16+width, 40+height);
    			JLabel picLabel = new JLabel(new ImageIcon(calcPicture));
    			drawingPane.add(picLabel);
    		} catch (IOException e) {
    		}
    	}
    	
    	public BufferedImage getImage() {
    		return calcPicture;
    	}
    	
    	public void scanImage() {
    		int width = calcPicture.getWidth();
    		int height = calcPicture.getHeight();
    		for (int i = 0; i < height; i++) {
    			ArrayList<Integer> rgbs = new ArrayList<Integer>();
    			ArrayList<Point> points = new ArrayList<Point>();
    			for (int j = 0; j < width; j++) {
    				rgbs.add(calcPicture.getRGB(j, i));
    				Point point = new Point(j, i);
    				points.add(point);
    			}
    			rgbrows.add(rgbs);
    			rows.add(points);
    		}
    	}
    }
    Thanks ahead of time for any help and I'll answer any questions anyone has.
    Last edited by Wahoowa; 07-24-2012 at 08:35 AM. Reason: SSCCE posted

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Comparison of pixels across images?

    Consider simplifying your posted code considerably as I see a lot of code presented that is completely unrelated to the problem at hand. Consider creating and posting an SSCCE as this will help both you and us.

  3. #3
    Wahoowa is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default Re: Comparison of pixels across images?

    Thanks for the advice. This is as close to an SSCCE that I believe I can get while maintaining all relevant functionality. To repeat the question, I was trying to figure out how to transfer the yellow lines into the image, basically. Or find a way to blend the two while reading RGB values.

    Image Window:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    
    public class ImageWindow extends JFrame {
    
    	private JPanel drawingPane;
    	private BufferedImage mitoPicture;
    	private boolean drawingLine = false;
    	public ArrayList<Point> yellowpoints = new ArrayList<Point>();
    	private Point breakPoint = new Point(-1, -1);
    	private Point firstPoint;
    
    	/**
    	 * Create the frame.
    	 */
    	public ImageWindow(File imageFile) {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		drawingPane = new JPanel() {
    			
    			//Use the paint method to draw the points.
    			@Override
    			public void paint(Graphics g) {
    				super.paint(g);
    				if (yellowpoints.size() > 1) {
    					for (int i = 1; i < yellowpoints.size(); i++) {
    						Point startPoint = yellowpoints.get(i-1);
    						Point endPoint = yellowpoints.get(i);
    						if (!endPoint.equals(breakPoint)) {
    							g.setColor(Color.YELLOW);
    							g.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
    						} else {
    							i = i+2;
    						}
    					}
    				}
    			}
    		};
    		setContentPane(drawingPane);
    		
    		drawingPane.addMouseListener(new MouseListener() {
    
    			@Override
    			public void mouseClicked(MouseEvent e) {
    			}
    
    			@Override
    			public void mouseEntered(MouseEvent arg0) {
    			}
    
    			@Override
    			public void mouseExited(MouseEvent arg0) {
    			}
    
    			@Override
    			public void mousePressed(MouseEvent e) {
    			}
    
    			@Override
    			public void mouseReleased(MouseEvent e) {
    				yellowpoints.add(e.getPoint());
    				yellowpoints.add(firstPoint);
    				yellowpoints.add(breakPoint);
    				drawingLine = false;
    				drawingPane.repaint();
    			}
    		});
    		drawingPane.addMouseMotionListener(new MouseMotionListener() {
    
    			//Save points in an ArrayList to draw.
    			@Override
    			public void mouseDragged(MouseEvent e) {
    				if (!drawingLine) {
    					firstPoint = e.getPoint();
    					yellowpoints.add(e.getPoint());
    					drawingLine = true;
    				} else {
    					yellowpoints.add(e.getPoint());
    					setContentPane(drawingPane);
    					drawingPane.repaint();
    				}
    			}
    
    			@Override
    			public void mouseMoved(MouseEvent e) {
    			}
    
    		});
    
    		//Create the image by reading the Image File
    		try {
    			mitoPicture = ImageIO.read(imageFile);
    			int height = mitoPicture.getHeight();
    			int width = mitoPicture.getWidth();
    			this.setBounds(100, 100, 16+width, 40+height);
    			JLabel picLabel = new JLabel(new ImageIcon(mitoPicture));
    			drawingPane.add(picLabel);
    		} catch (IOException e) {
    		}
    	}
    }
    File Chooser (I left this so people are able to more easily select an image file):

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    
    
    public class TestMenu extends JFrame {
    
    	private JPanel contentPane;
    
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					TestMenu frame = new TestMenu();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	public TestMenu() {
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		contentPane.setLayout(new BorderLayout(0, 0));
    		setContentPane(contentPane);
    		
    		JButton button = new JButton("Open File");
    		contentPane.add(button);
    		button.addActionListener(new ActionListener() {
    
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				JFileChooser fc = new JFileChooser();
    				fc.showOpenDialog(contentPane);
    				File imageFile = fc.getSelectedFile();
    				ImageWindow imageWin = new ImageWindow(imageFile);
    				imageWin.setVisible(true);
    			}
    			
    		});
    	}
    
    }
    Again, thanks for the advice. I can see how that was a lot of code to sift through.

Similar Threads

  1. help with some pixels
    By HelloWorld1234 in forum New To Java
    Replies: 13
    Last Post: 07-09-2012, 12:58 AM
  2. Pixels
    By shakeel in forum Java 2D
    Replies: 0
    Last Post: 03-01-2011, 11:32 AM
  3. Decimal Pixels?
    By Jcbconway in forum AWT / Swing
    Replies: 0
    Last Post: 11-29-2010, 09:25 PM
  4. Image to pixels
    By Deva in forum New To Java
    Replies: 6
    Last Post: 04-01-2010, 08:35 PM
  5. Counting Pixels
    By shaungoater in forum Java 2D
    Replies: 5
    Last Post: 11-29-2007, 05:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •