Morphological closing using java - java

I am trying to use this source code to do a morphologial closing on an image (with canny edge detected)
https://code.google.com/p/doccrop/source/browse/DocCrop/src/imageanalysis/morphology/Closing.java?r=3
I've created an instance of Closing and I've applied it on a BufferedImage and then draw it but I get a black image as a result!!
At first I had an error saying that the image must be type_byte_gray so I used this to change type but I guess it doesn't work
BufferedImage img1= ImageIO.read(new File(path));
BufferedImage imge = new BufferedImage(img1.getHeight(),img1.getWidth(),BufferedImage.TYPE_BYTE_GRAY);

You might use an image processing framework, like Marvin or ImageJ, for such purpose, as shown below.
input:
output:
source code:
import marvin.image.MarvinColorModelConverter;
import marvin.image.MarvinImage;
import marvin.io.MarvinImageIO;
import marvin.math.MarvinMath;
import static marvin.MarvinPluginCollection.*;
public class ClosingDemo {
public ClosingDemo(){
MarvinImage image = MarvinImageIO.loadImage("./res/closingIn.png");
image = MarvinColorModelConverter.rgbToBinary(image, 127);
morphologicalClosing(image.clone(), image, MarvinMath.getTrueMatrix(60, 60));
MarvinImageIO.saveImage(image, "./res/closingOut.png");
}
public static void main(String[] args) {
new ClosingDemo();
System.exit(0);
}
}

Related

Trying to Save a Java GUI as an Image in higher resolution

I am new to Stack Overflow but not new to programming. I am writing a java project for my own personal use, and maybe for some friends. I create a GUI, and add some images to a collage, and then I want to save it as a JPG that will scale to print nicely on an 8 1/2 x 11 paper (landscape).
I found the following code by asaren08 on Github, which works fine but the jpg is lower resolution. I need something of print quality.
I create my collage in an ImagePanel shown in the class below, it looks nice. I save it and print it and it is low resolution. I understand that is because my screen is lower resolution than my printer. If I create it pretending my resolution is 300 DPI, then it shows up on my screen scaled to fit, and that is what it saves, which is not what I want! It is not even proportional. For example, my laptop monitor is 15 inches -ish, wide and short. My image comes out wider than it should, exactly what I see on my monitor, but I want to to come out the way I set it, not the way it shows up on my monitor. On the other hand, I need to be able to see the GUI to set up my collage...
Any suggestions?
import java.awt.*;
import javax.swing.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
public class ImagePanel extends JPanel {
/**
* Save the Panel as image with the name and the type in parameters
*
* #param name name of the file
* #param type type of the file
*/
public void saveImage(String name,String type) {
System.out.println("width of image panel ="+getWidth());
BufferedImage image = new BufferedImage(getWidth(),getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
paint(g2);
try{
ImageIO.write(image, type, new File(name+"."+type));
} catch (Exception e) {
e.printStackTrace();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
}
}
Update: I added my ImagePanel to a JScrollPane as suggested in the comments. That makes it hard to see where I'm at in my collage. So for now, my silly solution is to put a little thumbnail above the ImagePanel to show what the whole thing looks like and I can see from that where I'm sitting on the ImagePanel.

File Path Changing on Export

I'm currently trying to develop a game, and the file path is changing when I export it.
Here is the code:
package random;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class Troll extends JFrame{
/**
*
*/
private static final long serialVersionUID = 4176461585360667597L;
public static BufferedImage img;
public static void main(String[] args){
File f = new File("troll.png");
try{
if(f.exists()){
System.out.println("ITS THERE! :D");
img = ImageIO.read(f);
} else {
System.out.println("DOESNT EXIST, REAL PATH IS: " + f.getAbsolutePath() );
}
}catch(Exception e){
e.printStackTrace();
}
new Troll();
}
public Troll(){
init();
}
public void init(){
setSize(1200,800);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public void paint(Graphics g){
g.drawImage(img, 500, 350, this);
}
}
When I run it through Eclipse ( The IDE I'm using ), it's running fine and it's showing the image. When I export it as a jar and convert it to an exe using Jar2Exe Software, the image does not appear and in the console it says that the absolute path for the image is on my Desktop. But when I open the exe using 7-Zip, the picture is in the exe.
How can I export it so that when the user runs it, the program can find the file path and show the image, instead of it thinking that it's on my desktop?
If you want to publish it as a jar then you need to use the Jar-specific API for reading your file. See eg. How to read a file from a jar file? (and you need to configure Eclipse to put the picture in the jar, which it sounds like you're already doing).
Also you should let us know whether it works when it's in a jar but not as an exe, that will help us narrow down where the problem may be.
I hope this is not a troll (lol)
img = ImageIO.read(this.getClass().getResource("/troll.jpg"));
You are in a jar, there is no resource file.
See that link as well: http://www.jar2exe.com/createdexe/integrate/protect
Thread.currentThread().getContextClassLoader().getResource("hello/yes.gif");

Java can't instantiate an Image?

So for my 2D game I want to use an image to represent a player but when I try to use an image, it tells me that it couldn't be "instantiated". I don't know what that means:
public class PlayerOne extends Entity{
private Image img = new Image();
[...]
#Override
public void render(Graphics g){
g.drawImage( img , x, y, Color.BLUE, new ImageObserver());
}
}
I tried it in another class with BufferedImages but that somehow doesn't work.
So it can't create Objects of neither Image nor the ImageObserver. Does anyone know a fix for this error ?
You cannot instantiate an abstract class. Please see link.
Following syntax would be operational:
private Image image = new BufferedImage(200,200,BufferedImage.TYPE_INT_RGB);
Also see link for more information on BufferedImage. Additionally, here is a tutorial which ilustrates an example implementation;
You should have an image file (*.png for example ) to start with. Then use
Image img = new ImageIcon("img.png").getImage();

Material file won't load when an Object file is loaded

So I've been trying to play around with Java 3D and, recently, I've been playing around with importing outside 3D models into a program. At this point, I can get the model into the program as an OBJ file, but for whatever reason, the program won't load the corresponding material file and I don't know if the problem is my coding or if the file just wasn't exported properly.
This is the code I wrote:
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import com.sun.j3d.loaders.objectfile.*;
import com.sun.j3d.loaders.Scene;
import java.awt.Color;
import javax.vecmath.*;
public class ModelLoadingTest {
public static void main(String[] args) {
SimpleUniverse universe = new SimpleUniverse();
BranchGroup scene = new BranchGroup();
ObjectFile loader = new ObjectFile(ObjectFile.LOAD_ALL);
loader.setFlags(ObjectFile.RESIZE);
Scene modelScene = null;
try{
modelScene = loader.load("paintedcar.obj");
}
catch(Exception e){
}
DirectionalLight lighting = new DirectionalLight(new Color3f(Color.WHITE), new Vector3f(0f, 0f, -1f));
lighting.setInfluencingBounds(new BoundingSphere(new Point3d(0.0, 0.0, 1.0), 100));
scene.addChild(modelScene.getSceneGroup());
scene.addChild(lighting);
universe.addBranchGraph(scene);
universe.getViewingPlatform().setNominalViewingTransform();
}
}
If it helps, the models I'm testing with were made in Maya and exported as Wavefront files.
... You are not loading the texture what so ever in your code. The matirl file is not coded in a obj file, you need to import it as a texture, You can do this the same as you would for a sphere except when you assign the texture to the mesh. When you assign it you need to use
"mesh name".setAppearance("your Appearance name");
for example
model.setAppearance(ap);

remote screen capturing and controlling

i m doing a project on "remote screen capturing and controlling"....in java
it is desktop application...
thre is client-server architecture....here server can capture the clients and make wath on client but, that is not known to client that someone is watching him/her....
and after captureing the client the server can also controlling the client from captured data.....and it it done at client side...automatically...as controlled by server.....
so i want your help ...please give me the suggestion....
Check out the "java.awt.Robot" class:
http://java.sun.com/javase/6/docs/api/java/awt/Robot.html
These methods should help you:
BufferedImage createScreenCapture(Rectangle screenRect);
void keyPress(int keycode)
void keyRelease(int keycode)
void mouseMove(int x, int y)
void mousePress(int buttons)
void mouseRelease(int buttons)
You have in this article the basics of screen capture using Robot (as suggested by brd6644's answer)
We can capture whole desktop, and save it to a PNG file, as follows.
public void captureScreen(String fileName) throws Exception {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
BufferedImage image = new Robot().createScreenCapture(new Rectangle(screenSize));
ImageIO.write(image, "png", new File(fileName));
}
Alternatively, we might capture our JFrame, including its window decoration, as follows.
public void captureFrame(JFrame frame, String fileName) throws Exception {
BufferedImage image = new Robot().createScreenCapture(frame.getBounds());
ImageIO.write(image, "png", new File(fileName));
}
The old (2003) jxta-remote-desktop project can also give you some pointers

Categories