The dicom bufferedImage from pixelmed is having low contrast - java

I would like to display a DICOM image in my java program. I am using pixelmed. However, I found that i cant correctly display the correct contrast. The contrast is too low.
Here is my code:
(SourceImage is a class provided by PixelMed, chosenImageFile.getPath() is just the path of the DICOM File.)
SourceImage dimg = new SourceImage(chosenImageFile.getPath());
BufferedImage image = dimg.getBufferedImage();
BufferedImage source = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = source.createGraphics();
g2d.drawImage(image, 0, 0, null);
dicomImgDisplayer1.setImage(source);
dicomImgDisplayer1 is an class extend JPanel. setImage() of this JPanel class will call the setImage() of an JFrame class.
The JFrame class's setImage() code:
public void setImage(BufferedImage image) {
this.image = image;
setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
repaint();
revalidate();
}
public void paint(Graphics graphics) {
Graphics2D g2d = (Graphics2D) graphics;
g2d.drawImage(image, null, 0, 0);
}
Is that something wrong with the color model? Please help. Thanks.

Does your image have a prescribed window width / window center? Be sure you set that (or allow the user to adjust it). See SingleImagePanel - there are some static methods to apply windowing to your buffered image.

Related

Difference between drawing on JPanel via paintComponent and Buffered Image

I have experimented with two different methods of drawing the same shape, the first image is drawn by overriding JPanel's paintComponent(Graphics g) method and using g.drawOval(..) etc,
The second image is drawn by creating a buffered image and drawing on it by using buffered image's graphics. How can I achieve the same rendering quality on both approaches? I have tried using many different rendering hints but none of them gave the same quality. I also tried sharpening by using Kernel and filtering, still couldn't.
private void createImage() {
image = new BufferedImage(IMG_SIZE, IMG_SIZE, BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = image.createGraphics();
gr.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
gr.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
gr.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
//something along the way
gr.drawOval(.....);
gr.drawLine(.....);
gr.drawOval(.....);
panel.repaint();
gr.dispose();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(backgroundColor);
if (USE_BUFFERED_IMAGE) {
g.drawImage(image, startX, startY, null);
} else {
//something along the way
g.drawOval(.....);
g.drawLine(.....);
g.drawOval(.....);
}
}
Drawing using JPanel paintComponent graphics
Drawing using Buffered Image graphics then it is drawn on Jpanel via drawimage
EDIT
I found my solution by getting almost every setting of panel graphics and applying them to buffered image graphics. Not by using only using the same rendering hints or "minimal reproducible examples" approaches. Here, the importing thing is that the panel's graphic scales everything by 1.25 and then scales down to the original before showing it on the panel.
Here is an example, -this is not exactly how my code is, this is just an example to give you an idea-
private void createImages(Paint paint, RenderingHints hints,
AffineTransform transform, Stroke stroke,
Composite composite, GraphicsConfiguration config ){
image = config.createCompatibleImage(IMG_SIZE, IMG_SIZE,
BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = image.createGraphics();
// same options
gr.setPaint(paint);
gr.setRenderingHints(hints);
gr.setTransform(transform);
gr.setStroke(stroke);
gr.setComposite(composite);
//something along the way
gr.drawOval(.....);
gr.drawLine(.....);
gr.drawOval(.....);
panel.repaint();
gr.dispose();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(backgroundColor);
if (USE_BUFFERED_IMAGE) {
Graphics2D g2 = (Graphics2D)g;
createImages(g2.getPaint(), g2.getRenderingHints(),g2.getTransform(),
g2.getStroke(),g2.getComposite(), g2.getDeviceConfiguration());
//scaling down is important because your drawings get scaled to 1.25
// by panels graphics' transformation
g.drawImage(image, startX, startY,(int)(IMG_SIZE*0.8),(int)(IMG_SIZE*0.8), null);
} else {
//something along the way
g.drawOval(.....);
g.drawLine(.....);
g.drawOval(.....);
}
}
I found my solution by getting almost every setting of panel graphics and applying them to buffered image graphics. Here, the importing thing is that the panel's graphic scales everything by 1.25 and then scales down to the original before showing it on the panel.
Here is an example, -this is not exactly how my code is, this is just an example to give you an idea-
private void createImages(Paint paint, RenderingHints hints,
AffineTransform transform, Stroke stroke,
Composite composite, GraphicsConfiguration config ){
image = config.createCompatibleImage(IMG_SIZE, IMG_SIZE,
BufferedImage.TYPE_INT_ARGB);
Graphics2D gr = image.createGraphics();
// same options
gr.setPaint(paint);
gr.setRenderingHints(hints);
gr.setTransform(transform);
gr.setStroke(stroke);
gr.setComposite(composite);
//something along the way
gr.drawOval(.....);
gr.drawLine(.....);
gr.drawOval(.....);
panel.repaint();
gr.dispose();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(backgroundColor);
if (USE_BUFFERED_IMAGE) {
Graphics2D g2 = (Graphics2D)g;
createImages(g2.getPaint(), g2.getRenderingHints(),g2.getTransform(),
g2.getStroke(),g2.getComposite(), g2.getDeviceConfiguration());
//scaling down is important because your drawings get scaled to 1.25
// by panels graphics' transformation
g.drawImage(image, startX, startY,(int)(IMG_SIZE*0.8),(int)(IMG_SIZE*0.8), null);
} else {
//something along the way
g.drawOval(.....);
g.drawLine(.....);
g.drawOval(.....);
}
}

How to draw transparent image in java with BufferedImage?

I'm willing to show an transparent image on a canvas in java with the help of graphics2D and BufferedImage.
Here is the code which loads image.
private static BufferedImage sprites,board;
public static void load(){
try {
board = new BufferedImage(100,100,BufferedImage.TYPE_INT_ARGB);
board = ImageIO.read(new File("res/chesssprite.png"));
} catch (IOException ex) {
Logger.getLogger(SpriteManager.class.getName()).log(Level.SEVERE, null, ex);
}
}
and here is the code which renders the image
public void render(){
BufferStrategy bs = this.getBufferStrategy();
if(bs == null){
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
Graphics2D g2d = (Graphics2D) g;
{
g2d.setColor(new Color(150,150,150));
g2d.fillRect(0,0,getWidth(), getHeight());
g2d.setComposite(AlphaComposite.Src);
g2d.drawImage(board,0,0,null);
}
g = g2d;
g.dispose();
bs.show();
}
I have search on net a lot but didn't come with an solution. If anyone knowns how to fix this.
Here is the image..
And here is how output looks like
Okay whoever facing these kind of problem:
Make sure the image is transparent. Test it in image viewer.
Remove the line g2d.setComposite(AlphaComposite.Src); This line adds alpha composites which make every transparent pixel black.

Repaint an argb BufferedImage

In my applet I make use of different BufferedImages and use them as screen parts. Each screen part will only be repainted when the content needs to change.
This is the abstract ScreenPart class:
public abstract class ScreenPart extends BufferedImage{
Graphics2D g;
private BufferedImage buffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
public ScreenPart(int width, int height) {
super(width, height, BufferedImage.TYPE_INT_ARGB);
g = createGraphics();
repaint();
}
public abstract void paint(Graphics2D g);
public void repaint(){
g.drawImage(buffer, 0, 0, null);
paint(g);
}
}
But the buffer doesn't work because the buffer is also transparent. It will work when I change the BufferedImage type of the buffer from ARGB to RGB but this displays also a black background. So my question is: how can I correctly repaint this BufferedImage with a buffer?
Already found a solution:
public void repaint() {
g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR));
g.fillRect(0,0, getWidth(), getHeight());
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
paint(g);
}
This doesn't make use of another BufferedImage.

Java: Load image from file, edit and add to JPanel

I want to load an image from my computer into 2D Graphics so that I can edit it afterwards and then I want to add it to JPanel. If you need to see my project I can send it to you.
void loadImage()
{
FileDialog fd = new FileDialog(new Frame(), "Please choose a file:", FileDialog.LOAD);
fd.show();
if (fd.getFile() != null)
{
File fil = new File(fd.getDirectory(), fd.getFile());
strDirectory = fd.getDirectory();
strFileType = fd.getFile();
mainImage.setIcon(new ImageIcon(fil.toString()));
getFileList(strDirectory);
checkFileType(strFileType);
}
}
Thanks in advance
To load your image into the memory, you can use ImageIO.read(File). To edit it afterwards, obtain a Graphics2D instance from it by calling createGraphics():
BufferedImage img = ImageIO.read(yourFile);
Graphics2D g = img.createGraphics();
// Draw here on the graphics
g.dispose();
You can even turn on anti-alias by setting a RenderingHint before drawing:
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIASING_ON);
Then, to add it to a JPanel, create your custom JComponent and add an instance of that component to your JPanel:
public class JImageComponent extends JComponent
{
private BufferedImage img;
public JImageComponent(BufferedImage bi)
{
img = bi;
}
#Override
public void paintComponent(Graphics g)
{
g.drawImg(img, 0, 0, this);
}
}
please read this tutorials about Icon in Swing and your Image/ImageIcon would by placed to the JLabel, this way eliminated all troubles came from paint/paintComponents ...
For image loading you should use ImageIO object with method read(File file) see docs. Then you will get BufferedImage instance of which you can make your changes through Graphics2D instance which you'll obtain by calling createGraphics() on the image instance see docs. Last thing, override method paintComponent() from JPanel or better JComponent see docs and there you can draw your image on Graphics instance which you'll get as parameter in paintComponent(Graphics g) method by calling drawImage(Image img, int x, int y, ImageObserver observer) see docs where ImageObserver set to null.

Transparent BufferedImage shows with a black background while painted on a JLabel

I've got a BufferedImage which is created from a png file. When creating it I set the type to be TYPE_INT_ARGB which should give me a transparent image. When I use paintComponent inside a JPanel to paint the image, I get the image with a black background. I really need to get it transparent so any help will be useful. Here is the code for clarity:
public class ImagePanel extends JPanel {
private static final long serialVersionUID = 1L;
private BufferedImage image;
public ImagePanel() {
this.image = null;
}
public void createImage(String fileName) {
this.image = ImageUtilities.getBufferedImage(fileName, this);
this.repaint();
}
public void paint(Graphics g) {
g.drawImage(this.image, 0, 0, this);
}
}
Here is how I load the image:
public class ImageUtilities {
/** Create Image from a file, then turn that into a BufferedImage.
*/
public static BufferedImage getBufferedImage(String imageFile, Component c) {
Image image = c.getToolkit().getImage(imageFile);
waitForImage(image, c);
BufferedImage bufferedImage = new BufferedImage(image.getWidth(c), image.getHeight(c),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bufferedImage.createGraphics();
g2d.drawImage(image, 0, 0, c);
return(bufferedImage);
}
And one last thing to add is that this ImagePanel is inside another Panel, if that has any significance.
Not sure if this will solve your problem, but:
override paintComponent instead of paint (http://download.oracle.com/javase/tutorial/uiswing/painting/closer.html)
consider to use the newer javax.imageio API
make sure the panel is not opaque (opaque = false)
Are you restricted to using an older version of Java? Try using ImageIO.read(fileName) to load the image file.
Try this (i.e. setComposite()):
g2d.setComposite(AlphaComposite.SrcOver);
g2d.setPaint(backgroundColor);
g2d.fillRect(0, 0, w, h);

Categories

Resources