Commit 256c8146 authored by lafabregue's avatar lafabregue

add the possiblity to compare an image with another in the imagePnale popupMenu

parent b6a7bf3f
......@@ -127,7 +127,7 @@ public class ROIToolsFrame extends JInternalFrame implements ActionListener {
private Vector<RegionOfInterest> rois;
/** Needed for the modification of the transparency */
private int sliderValue = (int) ImagePanel.transparency;
private int sliderValue = (int) ImagePanel.drawingTransparency;
/**
* Create the interface of the module.
......@@ -618,7 +618,7 @@ public class ROIToolsFrame extends JInternalFrame implements ActionListener {
JPanel jp_main = new JPanel(new BorderLayout());
int answer;
float currentTransparency = ImagePanel.transparency;
float currentTransparency = ImagePanel.drawingTransparency;
JSlider js_transparency = new JSlider(SwingConstants.HORIZONTAL, 0,
100, 50);
......
......@@ -811,80 +811,80 @@ public class ConstraintsSelectionDialog extends JInternalFrame implements Zoomab
return;
}
this.viewer = new PyramidImageViewer(img.getFilename(), true);
// if (this.image != null && !this.image.getPath().equals(img.getPath()))
// this.clear();
// this.width = img.getWidth();
// this.imageSource = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
// this.imagePoints = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
//
// double data[][][] = img.load(0, 0, img.getWidth(), img.getHeight());
// double[] max = new double[data.length];
// double[] min = new double[data.length];
//
// // for each band
// for (int i = 0; i < data.length; i++) {
// max[i] = min[i] = data[i][0][0];
// // for each column
// for (int x = 0; x < data[i].length; x++) {
// // for each line
// for (int y = 0; y < data[i][x].length; y++) {
// // searching the max and min value of the band
// max[i] = Math.max(max[i], data[i][x][y]);
// min[i] = Math.min(min[i], data[i][x][y]);
// }
// }
// }
// short[][][] dataView = new short[data.length][data[0]
// .length][data[0][0].length]; // the values to display
// double values[][] = data[0];
// /*
// * max contains the max value for r g b, we can create a valid
// * representation of data. the data values must be between 0 and 255 to
// * be valid, so let's convert them if needed
// */
//
// int[] bands = new int[3];
// bands[0] = img.r;
// bands[1] = img.g;
// bands[2] = img.b;
//
// // we use at most 3 bands
// int nbUsedBand = img.getNbBands();
// if (nbUsedBand > 3) {
// nbUsedBand = 3;
// }
// for (int b = 0; b < nbUsedBand; b++) {
// int i = bands[b];
// for (int x = 0; x < data[0].length; x++) {
// for (int y = 0; y < data[0][0].length; y++) {
// if (data[i][x][y] > 0) {
// // convert only if needed to avoid color deformation on
// // 8 bits images
// if (max[i] <= 255 && min[b] >= 0) {
// dataView[i][x][y] = (short) data[b][x][y];
// } else {
// dataView[i][x][y] = (short) (255 *
// (data[i][x][y] - min[b]) / (max[b] - min[b]));
// }
// } else {
// dataView[i][x][y] = 0;
// }
// }
// }
// }
// // pixels of the image to display
// for (int x = 0; x < values.length; x++) {
// for (int y = 0; y < values[0].length; y++) {
//
// int pixel = (int) values[x][y] << 24 | dataView[bands[0]][x][y] << 16
// | dataView[bands[1]][x][y] << 8 | dataView[bands[2]][x][y];
//
// imageSource.setRGB(x, y, pixel);
// imagePoints.setRGB(x, y, pixel);
// }
// }
// this.viewer = new PyramidImageViewer(img.getFilename(), true);
if (this.image != null && !this.image.getPath().equals(img.getPath()))
this.clear();
this.width = img.getWidth();
this.imageSource = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
this.imagePoints = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
double data[][][] = img.load(0, 0, img.getWidth(), img.getHeight());
double[] max = new double[data.length];
double[] min = new double[data.length];
// for each band
for (int i = 0; i < data.length; i++) {
max[i] = min[i] = data[i][0][0];
// for each column
for (int x = 0; x < data[i].length; x++) {
// for each line
for (int y = 0; y < data[i][x].length; y++) {
// searching the max and min value of the band
max[i] = Math.max(max[i], data[i][x][y]);
min[i] = Math.min(min[i], data[i][x][y]);
}
}
}
short[][][] dataView = new short[data.length][data[0]
.length][data[0][0].length]; // the values to display
double values[][] = data[0];
/*
* max contains the max value for r g b, we can create a valid
* representation of data. the data values must be between 0 and 255 to
* be valid, so let's convert them if needed
*/
int[] bands = new int[3];
bands[0] = img.r;
bands[1] = img.g;
bands[2] = img.b;
// we use at most 3 bands
int nbUsedBand = img.getNbBands();
if (nbUsedBand > 3) {
nbUsedBand = 3;
}
for (int b = 0; b < nbUsedBand; b++) {
int i = bands[b];
for (int x = 0; x < data[0].length; x++) {
for (int y = 0; y < data[0][0].length; y++) {
if (data[i][x][y] > 0) {
// convert only if needed to avoid color deformation on
// 8 bits images
if (max[i] <= 255 && min[b] >= 0) {
dataView[i][x][y] = (short) data[b][x][y];
} else {
dataView[i][x][y] = (short) (255 *
(data[i][x][y] - min[b]) / (max[b] - min[b]));
}
} else {
dataView[i][x][y] = 0;
}
}
}
}
// pixels of the image to display
for (int x = 0; x < values.length; x++) {
for (int y = 0; y < values[0].length; y++) {
int pixel = (int) values[x][y] << 24 | dataView[bands[0]][x][y] << 16
| dataView[bands[1]][x][y] << 8 | dataView[bands[2]][x][y];
imageSource.setRGB(x, y, pixel);
imagePoints.setRGB(x, y, pixel);
}
}
this.image = img;
doZoomedOp();
......
......@@ -10,6 +10,7 @@ import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
......@@ -171,6 +172,7 @@ public class ConstraintsSelectionInternalFrame extends JDialog {
public ConstraintsSelectionInternalFrame(ImagePanel imagePanel, Data data) {
super();
mainPanel = new JPanel(new BorderLayout());
this.imagePanel = imagePanel;
if (data != null) {
// case of Data provided
......@@ -179,11 +181,12 @@ public class ConstraintsSelectionInternalFrame extends JDialog {
this.data = data;
} else {
// otherwise we base our estimation on the image
StreamedImageReaderWrapper reader = new StreamedImageReaderWrapper(imagePanel.getRawImage().getPath());
minDistance = 0;
maxDistance = 0;
for (int i = 0 ; i < reader.getMaxValues().length ; i++) {
double d = (reader.getMaxValues()[i] - reader.getMinValues()[i]);
for (int i = 0 ; i < imagePanel.getRawImage().getMaxValues().length ; i++) {
double d = (imagePanel.getRawImage().getMaxValues()[i]
- imagePanel.getRawImage().getMinValues()[i]);
maxDistance += d*d;
}
maxDistance = Math.sqrt(maxDistance);
......@@ -306,22 +309,23 @@ public class ConstraintsSelectionInternalFrame extends JDialog {
public void mouseClicked(MouseEvent e) {
if (imagePanel.getDisplayedImage() != null) {
// point coordinates relative to img
int x = (int) ((e.getX()
- (int) ((displayPanel.getWidth() - (mNew.getWidth())) / 2.0)) / mZoomRate);
int y = (int) ((e.getY()
- (int) ((displayPanel.getHeight() - (mNew.getHeight())) / 2.0)) / mZoomRate);
if (x >= 0 && y >= 0 && x < imageSource.getWidth() * mZoomRate
&& y < imageSource.getHeight() /* mZoomRate*/) {
// int x = (int) ((e.getX()
// - (int) ((displayPanel.getWidth() - (mNew.getWidth())) / 2.0)) / mZoomRate);
// int y = (int) ((e.getY()
// - (int) ((displayPanel.getHeight() - (mNew.getHeight())) / 2.0)) / mZoomRate);
Point p = imagePanel.converteCoordinatesToImage(e.getX(), e.getY());
if (p.x >= 0 && p.y >= 0 && p.x < imagePanel.getRawImage().getWidth()
&& p.y < imagePanel.getRawImage().getHeight() /* mZoomRate*/) {
switch (currentStep) {
case MustLink_step1:
case CannotLink_step1:
case Label_step1:
addFirstPixel(x, y);
addFirstPixel(p.x, p.y);
break;
case MustLink_step2:
case CannotLink_step2:
addSecondPixel(x, y);
addSecondPixel(p.x, p.y);
break;
default:
// we don't refresh if not a valid step
......
......@@ -11,6 +11,7 @@ import jcl.data.mask.Mask;
import mustic.gui.*;
import mustic.io.RawImage;
import mustic.io.roi.RegionOfInterest;
import mustic.utils.filters.ImageFileFilter;
import mustic.utils.filters.ZipFileFilter;
import mustic.utils.image.BufferedImageHelper;
import mustic.utils.image.BufferedImageViewer;
......@@ -28,7 +29,7 @@ public class ImagePanel extends JPanel implements Zoomable {
private static final long serialVersionUID = 1L;
/** The transparency must be between 0.0 and 1.0 */
public static float transparency = new Float(0.50);
public static float drawingTransparency = new Float(0.50);
protected JMenuItem classifyAll = null;
......@@ -63,6 +64,21 @@ public class ImagePanel extends JPanel implements Zoomable {
/** zoom step */
private float mZoomStep = 0.7f;
/** transparency level */
private float transparencyStep = 0.03f;
/** transparency of the image */
private float transparency = 1;
/** test if T key is pressed for transparency */
private boolean keyTPressed = false;
/** test if Y key is pressed for compared image transparency */
private boolean keyYPressed = false;
/** transparency of the compared image */
private float comparedImageTransparency = 0.5f;
private ClassList pl_label = null;
......@@ -78,6 +94,7 @@ public class ImagePanel extends JPanel implements Zoomable {
/** Attributes to draw elements on the screen */
private Point clickedPoint = null;
/**
* Create a new ImagePanel from a BufferedImage.
......@@ -178,6 +195,24 @@ public class ImagePanel extends JPanel implements Zoomable {
displayPanel.decreaseZoomBoxArea();
}
}
// transparency of the image
else if(keyTPressed) {
if (e.getUnitsToScroll() > 0) {
transparency += transparencyStep;
} else {
transparency -= transparencyStep;
}
transparencyChanged();
}
// transparency of the compared image
else if(keyYPressed) {
if (e.getUnitsToScroll() > 0) {
comparedImageTransparency += transparencyStep;
} else {
comparedImageTransparency -= transparencyStep;
}
transparencyChanged();
}
// zoom in image
else {
if (e.getUnitsToScroll() > 0) {
......@@ -194,12 +229,14 @@ public class ImagePanel extends JPanel implements Zoomable {
JMenuItem menuROI = new JMenuItem("ROI tools...");
JMenuItem menuResult = new JMenuItem("Open result...");
JMenuItem menuInfo = new JMenuItem("Image properties...");
JMenuItem menuCompare = new JMenuItem("Compare with image...");
JMenuItem menuClose = new JMenuItem("Close");
popupMenu.add(menuSeg);
popupMenu.add(menuROI);
popupMenu.add(menuResult);
popupMenu.add(menuInfo);
popupMenu.add(menuCompare);
popupMenu.add(menuClose);
this.displayPanel.addMouseListener(new MouseAdapter() {
......@@ -270,6 +307,13 @@ public class ImagePanel extends JPanel implements Zoomable {
}
});
menuCompare.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
openComparedImage();
}
});
displayPanel.addComponentListener(new ComponentListener() {
@Override
public void componentResized(ComponentEvent e) {
......@@ -337,20 +381,88 @@ public class ImagePanel extends JPanel implements Zoomable {
if(e.getKeyCode() == KeyEvent.VK_H) {
//TODO add
}
// for transparency
if(e.getKeyCode() == KeyEvent.VK_T) {
keyTPressed = true;
}
// for compared image transparency
if(e.getKeyCode() == KeyEvent.VK_Y) {
keyYPressed = true;
}
}
@Override
public void keyReleased(KeyEvent e) {
// for transparency
if(e.getKeyCode() == KeyEvent.VK_T) {
keyTPressed = false;
}
// for compared image transparency
if(e.getKeyCode() == KeyEvent.VK_Y) {
keyYPressed = false;
}
}
});
this.displayPanel.setFocusable(true);
}
/**
* Notify the viewer that the transparency has changed
*/
private void transparencyChanged() {
if (transparency > 1) {
transparency = 1;
}
if (transparency < 0) {
transparency = 0;
}
if (comparedImageTransparency > 1) {
comparedImageTransparency = 1;
}
if (comparedImageTransparency < 0) {
comparedImageTransparency = 0;
}
viewer.adjustTransparency(transparency, comparedImageTransparency);
displayPanel.repaint();
}
private void openComparedImage() {
JFileChooser fileChooser;
if (MainFrame.getInstance().getCurrentDirectory() == null) {
fileChooser = new JFileChooser();
MainFrame.getInstance().setCurrentDirectory(fileChooser.getCurrentDirectory());
}
else {
fileChooser = new JFileChooser(MainFrame.getInstance().getCurrentDirectory());
}
ImageFileFilter filter = new ImageFileFilter();
fileChooser.addChoosableFileFilter(filter);
fileChooser.setFileFilter(filter);
fileChooser.setAcceptAllFileFilterUsed(true);
fileChooser.setMultiSelectionEnabled(false);
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
MainFrame.getProgressStatus().setMessage("loading file...");
this.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
viewer.addComparedImage(new RawImage(fileChooser.getSelectedFile()));
this.session.getBirdViewPanel().syncImage();
}
this.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
MainFrame.getProgressStatus().reset();
}
public static void setNewTransparency(float nT) {
ImagePanel.transparency = nT;
ImagePanel.drawingTransparency = nT;
}
private void applyMask() {
......@@ -377,7 +489,7 @@ public class ImagePanel extends JPanel implements Zoomable {
/** Initialise the transparency */
float[] compo = new float[4];
c.getColorComponents(compo);
Color clr = new Color(c.getColorSpace(), compo, ImagePanel.transparency);
Color clr = new Color(c.getColorSpace(), compo, ImagePanel.drawingTransparency);
Graphics g = img.getGraphics();
g.setColor(clr);
......@@ -401,7 +513,7 @@ public class ImagePanel extends JPanel implements Zoomable {
/** Initialise the transparency */
float[] compo = new float[4];
c.getColorComponents(compo);
Color clr = new Color(c.getColorSpace(), compo, ImagePanel.transparency);
Color clr = new Color(c.getColorSpace(), compo, ImagePanel.drawingTransparency);
g.setColor(clr);
g.drawLine(x, y, x1, y1);
......@@ -454,7 +566,7 @@ public class ImagePanel extends JPanel implements Zoomable {
/** Initialise the transparency */
float[] compo = new float[4];
c.getColorComponents(compo);
Color clr = new Color(c.getColorSpace(), compo, ImagePanel.transparency);
Color clr = new Color(c.getColorSpace(), compo, ImagePanel.drawingTransparency);
Graphics g = img.getGraphics();
g.setColor(clr);
......@@ -593,7 +705,7 @@ public class ImagePanel extends JPanel implements Zoomable {
/** Initialise the transparency */
compo = c.getColorComponents(compo);
c = new Color(c.getColorSpace(), compo, ImagePanel.transparency);
c = new Color(c.getColorSpace(), compo, ImagePanel.drawingTransparency);
g.setColor(c);
......@@ -1048,7 +1160,7 @@ public class ImagePanel extends JPanel implements Zoomable {
* x coordinate of the pixel in the image
* @return coordinate in the screen
*/
private Point converteCoordinatesToScreen(int x, int y) {
public Point converteCoordinatesToScreen(int x, int y) {
int sx = (int) ((x - currentTopLeft.x) * getZoomRate());
int sy = (int) ((y - currentTopLeft.y) * getZoomRate());
return new Point(sx, sy);
......@@ -1062,7 +1174,7 @@ public class ImagePanel extends JPanel implements Zoomable {
* x coordinate of the pixel in the screen
* @return coordinate in the image
*/
private Point converteCoordinatesToImage(int x, int y) {
public Point converteCoordinatesToImage(int x, int y) {
int sx = (int) (x / getZoomRate()) + currentTopLeft.x;
int sy = (int) (y / getZoomRate()) + currentTopLeft.y;
return new Point(sx, sy);
......
......@@ -244,22 +244,53 @@ public class BufferedImageHelper {
* the first image
* @param image2
* the second image
* @param transparency1
* transparency to apply to image 1
* @param transparency2
* transparency to apply to image 2
* @return the average of image1 and image2
*/
public static BufferedImage averagedBufferedImage(BufferedImage image1, BufferedImage image2) {
y */
public static BufferedImage averagedBufferedImage(BufferedImage image1, BufferedImage image2,
float transparency1, float transparency2) {
if (image1.getHeight() != image2.getHeight() ||
image1.getWidth() != image2.getWidth()) {
throw new IndexOutOfBoundsException("the BufferedImage shouyld have the same dimensions");
throw new IndexOutOfBoundsException("the BufferedImage should have the same dimensions");
}
BufferedImage result = new BufferedImage(image1.getWidth(), image1.getHeight(),
BufferedImage.TYPE_INT_RGB);
float averageFactor = transparency1 + transparency2;
for(int x = 0 ; x < image1.getWidth() ; x++) {
for(int y = 0 ; y < image1.getHeight() ; y++) {
Color c1 = new Color(image1.getRGB(x, y));
Color c2 = new Color(image2.getRGB(x, y));
Color c = new Color((c1.getRed()+c2.getRed())/2,
(c1.getGreen()+c2.getGreen())/2,
(c1.getBlue()+c2.getBlue())/2);
Color c = new Color((int) ((c1.getRed() * transparency1 + c2.getRed() * transparency2) / averageFactor),
(int) ((c1.getGreen() * transparency1 + c2.getGreen() * transparency2) / averageFactor),
(int) ((c1.getBlue() * transparency1 + c2.getBlue() * transparency2) / averageFactor));
result.setRGB(x, y, c.getRGB());
}
}
return result;
}
/**
* Create a new BufferedImage corresponding to an image with a transparency factor
*
* @param image
* the origin image
* @param transparency
* the transparency factor
* @return the image with transparency applied
*/
public static BufferedImage applyTransparency(BufferedImage image, float transparency) {
BufferedImage result = new BufferedImage(image.getWidth(), image.getHeight(),
BufferedImage.TYPE_INT_RGB);
for(int x = 0 ; x < image.getWidth() ; x++) {
for(int y = 0 ; y < image.getHeight() ; y++) {
Color c2 = new Color(image.getRGB(x, y));
Color c = new Color((int) (c2.getRed() * transparency),
(int) (c2.getGreen() * transparency),
(int) (c2.getBlue() * transparency));
result.setRGB(x, y, c.getRGB());
}
}
......
......@@ -253,16 +253,21 @@ public class BufferedImageViewer extends ImageViewer {
screen = op.filter(screen, null);
//mScreenImage = op.filter(this.mScreenImage, null).getSubimage(0, 0, width, height);
if (comparedImage == null) {
screen = BufferedImageHelper.applyTransparency(screen, transparency);
}
if (saved) {
realX = x;
realY = y;
mScreenImage = screen;
}
if (comparedImage != null) {
BufferedImage scr2 = comparedImage.getScreenImage(x, y, width, height, zoomRate, saved);
return BufferedImageHelper.averagedBufferedImage(screen, scr2);
}
return BufferedImageHelper.averagedBufferedImage(screen, scr2,
transparency, comparedImageTransparency);
}
return screen;
}
......@@ -271,7 +276,8 @@ public class BufferedImageViewer extends ImageViewer {
public BufferedImage getScreenImage() {
if (comparedImage != null) {
BufferedImage scr2 = comparedImage.getScreenImage();
return BufferedImageHelper.averagedBufferedImage(mScreenImage, scr2);
return BufferedImageHelper.averagedBufferedImage(mScreenImage, scr2,
transparency, comparedImageTransparency);
}
return mScreenImage;
......
......@@ -178,8 +178,33 @@ public abstract class ImageViewer {
this.maxValues = maxValues;
}
public void adjustTransparency(float firstImage, float secondImage) {
/**
* Change the transparency level for each image.
* If a transparency factor is bellow 0 ore above 1 it is truncated
*
* @param t1
* the first image transparency factor
* @param t2
* the second image transparency factor
*/
public void adjustTransparency(float t1, float t2) {
transparency = t1;
comparedImageTransparency = t2;
if (transparency > 1) {
transparency = 1;
}
if (transparency < 0) {
transparency = 0;
}
if (comparedImageTransparency > 1) {
comparedImageTransparency = 1;
}
if (comparedImageTransparency < 0) {
comparedImageTransparency = 0;
}
System.out.println("tr : "+transparency+" cpmp tr : "+comparedImageTransparency);
}
/**
* Rescale the image to take in account the modification of the parameters
......
......@@ -275,6 +275,10 @@ public class PyramidImageViewer extends ImageViewer {
AffineTransformOp op = new AffineTransformOp(trans, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
screen = op.filter(screen, null);
if (comparedImage == null) {
screen = BufferedImageHelper.applyTransparency(screen, transparency);
}
if (saved) {
realX = x;
realY = y;
......@@ -283,7 +287,8 @@ public class PyramidImageViewer extends ImageViewer {
if (comparedImage != null) {
BufferedImage scr2 = comparedImage.getScreenImage(x, y, width, height, zoomRate, saved);
return BufferedImageHelper.averagedBufferedImage(screen, scr2);
return BufferedImageHelper.averagedBufferedImage(screen, scr2,
transparency, comparedImageTransparency);
}
return screen;
......@@ -293,7 +298,8 @@ public class PyramidImageViewer extends ImageViewer {
public BufferedImage getScreenImage() {
if (comparedImage != null) {
BufferedImage scr2 = comparedImage.getScreenImage();
return BufferedImageHelper.averagedBufferedImage(mScreenImage, scr2);
return BufferedImageHelper.averagedBufferedImage(mScreenImage, scr2,
transparency, comparedImageTransparency);
}
return mScreenImage;
......@@ -328,6 +334,8 @@ public class PyramidImageViewer extends ImageViewer {
comparedImage = null;
} else {
comparedImage = new PyramidImageViewer(image, true);
comparedImage.getScreenImage(realX, realY, mScreenImage.getWidth(),
mScreenImage.getHeight(), currentLvlZoomRate*relativeZoomRate, true);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment