How should I do image processing in Java? - java

I'm making an applet that lets users crop out a piece of an image and save it. For cropping, I'm going to implement a "magic wand"-esque tool. I can do all this in Matlab but i'm having some trouble figuring out the Java libraries. Here are a few tasks I need to perform:
Randomly access pixels in an image by (x,y) and return a single object (java.awt.Color, ARGB int, short[], whatever -- as long as I'm not dealing with channels individually)
Create an alpha channel from a boolean[ ][ ]
Create a N by M image that's initialized to green
Any pros out there who can help me? Just some code snippets off the top of your head would be fine.
Many thanks,
Neal

You want to use the Java2D libraries. Specifically, you want to use the BufferedImage class from the library to deal with your images. You can access individual pixels and do all of the things you have specified above. Sun/Oracle has a good tutorial to get you started in the right direction. The second part in that tutorial goes over creating an alpha channel. Oh, and to access individual pixels, you want to use the WritableRaster class. So you can do something like this. Hope this gets you started.
WritableRaster imageRaster = Bufferedimg.getRaster();
//use java random generation to get a random x and y coordinate, then call this to access the pixel
imageRaster.getPixel(x, y,(int[])null);

ImageJ is a mature, open-source image processing framework that supports macros, plugins and a host of other features.

Marvin is a Java image processing framework that can help you. It provides algorithms for filtering, feature extraction, morphological analysis, tranformations, segmentation and so forth. Moreover, its architecture supports real-time video processing with the same algorithms.

Related

Is there any easy way to rotate an image about z axis using java without jumping into java 3d?

I want to rotate an image about the z axis using java.
I tried to search for ways to do that but it involves complex matrix manipulations and it works on 3d models and stuff. I will resort to that if there is no other way but would like to know if there are any other alternatives for this.
I simply need to rotate an image about z axis and nothing else.
EDIT
This is what I need exactly:
I have an image like this:
And I want to convert it to something of this sort:
I did this using processing. I need some way of doing this using java.
If you are referring to rotating an image during a Swing paint operation, then the correct way to do this is with an AffineTransform.
Graphics2D graphic;
graphic.drawRenderedImage(image, AffineTransform.getRotateInstance(Math.PI));
Unfortunately AffineTransform does not support perspective transforms (by definition a transform is only Affine if parallel lines remain parallel). For perspective transforms you need to use the Java Advanced Imaging API which can be downloaded from Oracle's site. It has a PerspectiveTransform that does just what you want. Unfortunately JAI is not quite as straightforward to use as it is much more flexible.

Java and Javascript methods of controlling display, pixel per pixel

I'm working on a new project that will require some 3d rendering. I am planning on writing all of the calculations for each individual pixel (bypassing a graphics engine), and I'd like to do it with Javascript, but I don't know if there's a way. Posting HTML to display thousands of pixels is way too slow. I need something quick and responsive, so I'm considering using JAVA.
My question is, do either of these have the ability to perform calculations on individual pixels, quickly? And how would I access the display in such a way?
Thanks
If you have the time to get familiar with OpenGL, you could use OpenGL in java:
http://en.wikipedia.org/wiki/Java_OpenGL
This would allow you to directly manipulate a specified OpenGL-frame. Kindof like what runescape does.

How to Fourier transform an Android camera image?

This is a question I've been mulling over for a couple of evenings now, and I'm not really any closer to an answer. I'd like to take the Fourier transform of an image that's just been taken on a camera on an Android device. Now the way I see it, there are 3 options for doing this:
Transform the byte[] array which is returned from the camera, then change the result to an image which can be displayed. This seems the most direct method but I can't find any libraries which can transform byte arrays.
Convert the byte array to an int[] or other number array and run a transform from one of the mathematical libraries like JTransform. Then convert to an image from the number array.
Convert the camera image to a RenderedImage object and use the DFTDescriptor class in the Java Advanced Imaging library to perform the transform. Not sure how well this would run on phone processors AND the documentation for JAI isn't exactly the clearest...
Or have I missed some other glaringly obvious way that merits a *hit head with hand*?
I'd be really grateful if someone could shine some light on this. I seem to be going in circles with it!
Thanks.
My suggestion is that you use Piotr Wendykier's JTransform library (as per your option 2). Amongst other possibilities it specifically supplies a 2D DFT transform, which should fit your requirement nicely.
Cheers,

How to improve the image quality before the image processing start in javacv or opencv?

I have an image with 400x400 image to identify different components from it. But when I try to identify components using that most of time it doesn't provide correct answers. So I need to know whether there are some kind of methods in javacv or opencv to improve the quality of the image or increase the size of the image without effecting to its quality ?
This is the sample image that I use. (This is the maximum size that I can get and I can't use any photo editing softwares in the project, because it's dynamically generated image.)
In my image processing I need to identify squares and rectangles that connects those squares. And specially I need to get the width and height of those using pixel values.
You can scale it to any size, if you can vectorize it... and in your case vestorization is quite simple as you have some simple geometrical objects in image.
So, in my view your approach should be like this:
detect edges in the image with a high threshold (as you have very distinct objects)
vectorize them
scale them to any size
You should also look at the following link: Increasing camera capture resolution in OpenCV.
If you stick to image processing the easiest way to do it is to apply an equalizeHist(). This will increase contrast and will improve subsequent steps.
But, and this is a biiiig 'but', why are you doing it? Just reading this post, I saw another solution, and a quick google proved me I am right:
Kabeja is a Java library for parsing, processing and converting
Autodesk's DXF format. You can use Kabeja from the CommandLine or
embed into your application. All parsed data are accessible with the
DOM-like API.
That means you can extract directly all the data you want from that image in a text format. Probably something like "at position x, y there is a transistor, or whatever." So why would you render that file into an image, then analyse that image to extract the components?
If you do it for school (I know that many school projects are like this) I would recommend you to find a real problem to solve, and propose it to your teacher. You will be happier to do something that is not complete nonsense.
vectorizing the image is best option I guess as suggested by mocap.
you can also use enhancement tools like sharpening, saturating etc.

Hardware accelerate bitmap drawing in java

I want to be able to draw consecutive bitmaps (of type BufferedImage.TYPE_INT_RGB) of a video as quickly as possible in java. I want to know the best method in doing so. Does anyone have any advice where I should start? From what I've read, 2 options are:
1) Use GDI/GDI+ routines in a JNI dll working with JAWT (Im on Windows)
2) Use Java3D and apply Textures to a Box's face and rotate it to the camera
Im interesting in any advice on these topics as well as any others.
I have done a decent amount of GDI/GDI+ programming in VB when i created an ActiveX control, so using GDI should be painless, but im guessing Java3D will utilize the GPU more (I could be wrong) and give better performance. What do you think? GDI and JAWT with my previous experience, or start and new API journey with Java3D.
Thanks in advance. :)
To obtain a fluid animation (if it what you want to get), you need to use double buffering. For doing this, you will need to create a new java.awt.Image (or a subclass like BufferedImage, or if you want OpenGL accelerated processing, VolatileImage) for each frame you want to display. If you haven't already done so, call Image.getGraphics() to get a java.awt.Graphics object (can also be useful to add your content to the Image). At the end, when you hidden Image is complete, call Graphics.draw() to replace the current display smoothly.
VolatileImage is OpenGL accelerated and much faster. When VolatileImage.getGraphics() is called, it actually returns a Graphics2D, which is also part of the accelerated graphic pipeline.
It works on Windows, Linux and Solaris, but you need to have OpenGL drivers installed for your graphic card.
Some additional refs:
Accelerated graphic pipeline:
http://download.oracle.com/javase/1.5.0/docs/guide/2d/new_features.html
http://www.javalobby.org/forums/thread.jspa?threadID=16840&tstart=0
Double buffering:
http://www.java2s.com/Code/Java/2D-Graphics-GUI/Smoothmoveusingdoublebuffer.htm
http://www.heatonresearch.com/articles/23/page2.html
http://www.javacooperation.gmxhome.de/BildschirmflackernEng.html

Categories