how to perform the image segmentation in java? - java

hi guys i am an infant for image processing technique in java , i have decided to develop one project in image processing so i need what are the algorithms are followed and also which one is easier to develop please some one guide me it may be great for me.....and also which technology is best for image processing java or Matlab? guide me...

I think the best image processing tool for you depends on the kind of project you're working on.
If you're working on a research project that needs productivity, quick validation and writting reports, Matlab and similar tools are the best option. On the other hand, if you're developing a software product, Java, C++, C, Objective-C, etc is more indicated. Matlab solutions are not easy to deliver and maintain in production.
Since you asked how to do image segmentation in Java, I'll provide an example using Java and Marvin Image Processing Framework. As suggested by #Asif Sharif, FloodFill segmentation is a good strategy and I used it!
INPUT IMAGE:
OUTPUT IMAGE:
HOW IT WORKS:
Load input image.
Change green pixels to white pixels.
Apply intensity thresholding for separating foreground from background.
Apply morphological closing to group separated parts of the same object
Use FloodFill segmentation to get the segments.
Draw the segments coordinates in the original image.
SOURCE:
import static marvin.MarvinPluginCollection.*;
public class SimpleSegmentation {
public SimpleSegmentation(){
// 1. Load image
MarvinImage original = MarvinImageIO.loadImage("./res/robocup.jpg");
MarvinImage image = original.clone();
// 2. Change green pixels to white
filterGreen(image);
// 3. Use threshold to separate foreground and background.
MarvinImage bin = MarvinColorModelConverter.rgbToBinary(image, 127);
// 4. Morphological closing to group separated parts of the same object
morphologicalClosing(bin.clone(), bin, MarvinMath.getTrueMatrix(30, 30));
// 5. Use Floodfill segmention to get image segments
image = MarvinColorModelConverter.binaryToRgb(bin);
MarvinSegment[] segments = floodfillSegmentation(image);
// 6. Show the segments in the original image
for(int i=1; i<segments.length; i++){
MarvinSegment seg = segments[i];
original.drawRect(seg.x1, seg.y1, seg.width, seg.height, Color.yellow);
original.drawRect(seg.x1+1, seg.y1+1, seg.width, seg.height, Color.yellow);
}
MarvinImageIO.saveImage(original, "./res/robocup_segmented.png");
}
private void filterGreen(MarvinImage image){
int r,g,b;
for(int y=0; y<image.getHeight(); y++){
for(int x=0; x<image.getWidth(); x++){
r = image.getIntComponent0(x, y);
g = image.getIntComponent1(x, y);
b = image.getIntComponent2(x, y);
if(g > r*1.5 && g > b*1.5){
image.setIntColor(x, y, 255,255,255);
}}}
}
public static void main(String[] args) { new SimpleSegmentation(); }
}

For image segmentation in JAVA you can also consider to use open-source IMMI tool (http://spl.utko.feec.vutbr.cz/en/). In comparison to Matlab, it is (in my opinion) more simple to use and simply enables also image mining.

You can use the Java Advanced Imaging (JAI) Library to do image processing in java. You have to decide for yourself whether Java or MATLAB is better for you.

Algorithm for Image segmentation depends upon what type of output you want after segmentation. Each algorithm performs a different segmentation. I think the region growing or Flood Fill is good for this purpose.
You can use Java/JAI and JavaCV for this Image processing tasks.

MATLAB is better for image processing. And the best way is to find special image processing tools (or libraries).

Related

How to rotate an image on command using Codename One's Transform class?

I am currently doing a school project where we are creating an Asteroids game using Codename One. My current functionality works well, except for when it comes to rotating the image of the ship. Using the Transform class has been ineffective; the image does not rotate, no matter how the Transform is applied or the image is drawn.
Here is a sample portion of the code used to turn:
public void turnRight() //Rotates the ship 5 degrees clockwise
{
if (direction==355)
direction = 0;
else
direction+=5;
Transform tmpTransform = Transform.makeIdentity();
theImage.getGraphics().getTransform(tmpTransform);
tmpTransform.rotate((float)Math.toRadians(5), x, y);
theImage.getGraphics().setTransform(tmpTransform);
theImage.getGraphics().drawImage(shipPic, 0, 0);
}
Where:
theImage is a mutable Image (100x100)
shipPic is an Image created via Image.createImage(String path)
In addition, I have tried using the draw(Graphics g, Point p) method and passing theImage.getGraphics(), as well as passing shipPic.getGraphics()
I am at a loss, and Codename One's documentation on the subject is unhelpful.
Could I get some assistance, please?
You need to use one graphics object so something like this:
Graphics g = theImage.getGraphics();
Would be more correct. You also must test for transform support when rendering onto an image as low level graphics isn't always portable to all OS's in all surfaces. A good example is iOS where rendering onto an image uses a completely different low level implementation than the display rendering.
Normally I would render directly to the display as that is hardware accelerated on modern devices and images are often implemented in software.
About the documentation, did you read the graphics section in the developer guide?
It should contain explanations of everything and if something is missing there is search. If you still can't find something and figure it out by yourself notice you can also edit the docs and help us improve them.

Java image normalization

I am doing the image processing and I am interested in normalization of the image. Somewhere I come to know about the ImageJ library.
So could anyone help me in finding example code for Image normalization using ImageJ library.
I know It isn't with imageJ. But I would help you with java.
Try to use Catalano Framework.
The next version (1.2) will have more algorithms for image normalization as:
Gray World
White Patch
Modified White Patch
Example:
FastBitmap fb = new FastBitmap(bufferedImage);
HistogramEqualization hist = new HistogramEqualization();
hist.applyInPlace(fb);
bufferedImage = fb.toBufferedImage();

How to create large SWT image?

In my eclipse-rcp application I need to create an image with dimensions 30000x10000 px or more. This image is NatTable representation. Using standard image creation approach, it fails with different problems: OutOfMemory, SWTError - IllegalArgument or my PC stops responding (btw, its windows 7, 64bit, 4 RAM - client have much slower laptops, but the picture is still needs to be created). Here is a code snippet:
private Image getNattableImageRepresentation(final Display display) {
final Rectangle totalGridArea = getTotalGridArea(); //this returns Rectangle(0,0,30000,10000)
setGridLayerSize(totalGridArea);
final Image nattableImage = new Image(display, totalGridArea);
final GC nattableGC = new GC(nattableImage);
gridLayer.getLayerPainter().paintLayer(gridLayer, nattableGC, 0, 0, totalGridArea, configRegistry);//nattable API, which draws an image into a specified gc
restoreGridLayerState();
return nattableImage;
}
return null;
}
Are there any tricks to create such huge images or may be API? Is Java Advanced Imaging Api suitable for this purpose?
Any suggestions are appreciated.
ImageMagick is neat tool for image processing like this.. new CG is not the way, definitely.. If you'll join all spare images to the big one, there should be no problem at all..
There is a simple solution for storing larger images in Java. BigBufferedImage stores the image on the hard drive in a very fast way:
https://stackoverflow.com/a/53205617/2631710

How should I do image processing in 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.

slicing up a very big jpg map image , 49000* 34300 pixel

i want to write a mapviewer, i must to work small tile of big map image file and there is need to tiling the big image, the problem now is to tiling big image to small tiles (250 * 250 pixel or like this size)
so on, i used ImageMagic program to do it but there was problem
now is any other programing method or application that do tiling?
can i do it with JAI in java? how?
Have you tried doing it in java yourself? I tried this with (WARNING, big image, can crash your browser, use "save as...") this image. Needed to run with extra memory though (-Xmx400M).
public class ImageTile {
public static void main(String[] args) throws IOException {
Dimension tileDim = new Dimension(250, 250);
BufferedImage image = ImageIO.read(new File(args[0]));
Dimension imageDim = new Dimension(image.getWidth(), image.getHeight());
for(int y = 0; y < imageDim.height; y += tileDim.height) {
for(int x = 0; x < imageDim.width; x += tileDim.width) {
int w = Math.min(x + tileDim.width, imageDim.width) - x;
int h = Math.min(y + tileDim.height, imageDim.height) - y;
BufferedImage tile = image.getSubimage(x, y, w, h);
ImageIO.write(tile, "JPG", new File("tile-"+x+"-"+y+".jpg"));
}
}
}
}
For the large images sizes like you have, you will be best served with lossless editing of the JPEG files. Not only is this faster, since the image doesn't need to be rendered, but it also preserves quality, since the image is not recompressed.
Lossless editing works on blocks, typically 16px square. While restrictive for some applications, this seems a good fit for mapping. You could implement tiling at different zoom levels by first losslessly cropping the image to sized pieces. (This is quick an efficient since the image is not rendered.) This gives you tiles for full-zoom. To create lower-levels of zoom, combine 2x2 tiles and scale these down to the size of 1 tile. The next level uses 4x4 tiles, and 8x8 and so on, each time scaling down to one tile. At some point when the number of tiles beecomes too large, you can choose to use zoomed tiles as the base resource. For example, at zoom level 8, that would require 256x256 tiles. This might be too much to handle, so you could use 16x16 tiles from zoom level 4.
Wikipedia has more on lossless editing, and links to some implementing libraries.
imagemagick does tiling using -tile. It's more of a repitition of an image, but might be useful esp. since youre already using it. However If you mean generated seamless tiling I'm not sure if imagemagick can do that or not.
GDAL comes with a script called gdal2tiles.py that does exactly what you want, including formatting the tiles for use with Google Maps, OpenLayers, etc.
There seems to be an newer version of GDAL2Tiles as well.
How about a megatexture with an r-tree for efficient access? Apparently it can use images 128000x128000 pixels.
JAI is platform dependent and seems like a dead project today.
I advise using the open-source program imagemagick. Although it is platform dependent, it is available for the same platforms as JAI, but with full community support.
The trick about large images about imagemagick is using its "stream"-command instead of the convert command. Stream only reads the relevant portion of the image and saves the extracted part as raw data. You then need "convert" to save the small raw data as jpeg.
Example to save a tile from large.jpeg of size 800x600 from position 0x0 to tile.jpeg:
stream -extract 800x600+0+0 large.jpeg tile.rgb
convert -depth 8 -size 800x600 rgb:tile.rgb tile.jpeg
(When running on windows, be sure to use ImageMagick's convert.exe, as there is a windows command named "convert".)
When working with TIFF-images only, apache Sanselan could be the right choice - it is a pure-java imaging lib. Also, JAI seems to contain a platform independent codec for TIFF.

Categories