"Learning gives Creativity,Creativity leads to Thinking, Thinking provides Knowledge, Knowledge makes you Great"
Monday, 9 June 2014
What are you looking for?
The current, stable version is known simply as "ImageJ"—or sometimes "ImageJ1" or "IJ1" to differentiate it from ImageJ 2.x, which is still in beta. Read more... | ImageJ2 ImageJ 2.0.0, referred to as "ImageJ2" or "IJ2" for short, is currently in development. It is a complete rewrite of ImageJ, but includes ImageJ1 with a compatibility layer, so that old-style plugins and macros can run the same as in IJ1.Read more... |
Fiji Fiji is a distribution of ImageJ (both ImageJ1 and ImageJ2!) for the life sciences. It provides a large number of additional plugins to facilitate analysis of life sciences images, particularly microscopy images.Read more... |
Saturday, 31 May 2014
How to Avoid ConcurrentModificationException when using an Iterator
Java Collection classes are fail-fast which means that if the Collection
will be changed while some thread is traversing over it using iterator,
the iterator.next() will throw a ConcurrentModificationException.
This situation can come in case of multithreaded as well as single threaded environment.
Lets explore this scenario with the following example :
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class IteratorExample { | |
public static void main(String args[]){ | |
List<String> myList = new ArrayList<String>(); | |
myList.add("1"); | |
myList.add("2"); | |
myList.add("3"); | |
myList.add("4"); | |
myList.add("5"); | |
Iterator<String> it = myList.iterator(); | |
while(it.hasNext()){ | |
String value = it.next(); | |
System.out.println("List Value:"+value); | |
if(value.equals("3")) myList.remove(value); | |
} | |
Map<String,String> myMap = new HashMap<String,String>(); | |
myMap.put("1", "1"); | |
myMap.put("2", "2"); | |
myMap.put("3", "3"); | |
Iterator<String> it1 = myMap.keySet().iterator(); | |
while(it1.hasNext()){ | |
String key = it1.next(); | |
System.out.println("Map Value:"+myMap.get(key)); | |
if(key.equals("2")){ | |
myMap.put("1","4"); | |
//myMap.put("4", "4"); | |
} | |
} | |
} | |
} | |
/* OUTPUT | |
List Value:1 | |
List Value:2 | |
List Value:3 | |
Exception in thread "main" java.util.ConcurrentModificationException | |
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372) | |
at java.util.AbstractList$Itr.next(AbstractList.java:343) | |
at com.journaldev.java.IteratorExample.main(IteratorExample.java:27) */ |
From the output stack trace, its clear that the exception is coming
when we call iterator next() function. If you are wondering how Iterator
checks for the modification, its implementation is present in
AbstractList class where an int variable modCount is defined that
provides the number of times list size has been changed. This value is
used in every next() call to check for any modifications in a function
checkForComodification().
Now comment the list part and run the program again.
Output will be:
Map Value:3 Map Value:2 Map Value:4
Since we are updating the existing key value in the myMap, its size
has not been changed and we are not getting
ConcurrentModificationException. Note that the output may differ in your
system because HashMap keyset is not ordered like list. If you will
uncomment the statement where I am adding a new key-value in the
HashMap, it will cause ConcurrentModificationException.
To Avoid ConcurrentModificationException in multi-threaded environment:
1.
You can convert the list to an array and then iterate on the array.
This approach works well for small or medium size list but if the list
is large then it will affect the performance a lot.
2. You can
lock the list while iterating by putting it in a synchronized block.
This approach is not recommended because it will cease the benefits of
multithreading.
3. If you are using JDK1.5 or higher then you can
use ConcurrentHashMap and CopyOnWriteArrayList classes. It is the
recommended approach.
To Avoid ConcurrentModificationException in single-threaded environment:
You
can use the iterator remove() function to remove the object from
underlying collection object. But in this case you can remove the same
object and not any other object from the list.
Let us run an example using Concurrent Collection classes:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.journaldev.java; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.concurrent.ConcurrentHashMap; | |
import java.util.concurrent.CopyOnWriteArrayList; | |
public class ThreadSafeIteratorExample { | |
public static void main(String[] args) { | |
List<String> myList = new CopyOnWriteArrayList<String>(); | |
myList.add("1"); | |
myList.add("2"); | |
myList.add("3"); | |
myList.add("4"); | |
myList.add("5"); | |
Iterator<String> it = myList.iterator(); | |
while(it.hasNext()){ | |
String value = it.next(); | |
System.out.println("List Value:"+value); | |
if(value.equals("3")){ | |
myList.remove("4"); | |
myList.add("6"); | |
myList.add("7"); | |
} | |
} | |
System.out.println("List Size:"+myList.size()); | |
Map<String,String> myMap = | |
new ConcurrentHashMap<String,String>(); | |
myMap.put("1", "1"); | |
myMap.put("2", "2"); | |
myMap.put("3", "3"); | |
Iterator<String> it1 = myMap.keySet().iterator(); | |
while(it1.hasNext()){ | |
String key = it1.next(); | |
System.out.println("Map Value:"+myMap.get(key)); | |
if(key.equals("1")){ | |
myMap.remove("3"); | |
myMap.put("4", "4"); | |
myMap.put("5", "5"); | |
} | |
} | |
System.out.println("Map Size:"+myMap.size()); | |
} | |
} | |
/* Output is: | |
List Value:1 | |
List Value:2 | |
List Value:3 | |
List Value:4 | |
List Value:5 | |
List Size:6 | |
Map Value:1 | |
Map Value:null | |
Map Value:4 | |
Map Value:2 | |
Map Size:4 */ |
Tuesday, 27 May 2014
How to Column Selection in IDE Eclipse
Eclipse used to need a column mode plugin to be able to select a rectangular selection.
Since Eclipse 3.5, you just need to type Alt+Shift+A: see its News and Noteworthy section. (On OS X it's Option-Command-A.)
Or activate the 'Editor Presentation
' action set ( Window > Customize Perspective menu) to get a tool bar button for toggling the block selection mode.
Thursday, 22 May 2014
Merging two images
Just create a new BufferedImage with transparency, then paint the other two images (with full or semi-transparency) on it.
This is how it will look like:
Sample code (images are called 'image.png' and 'overlay.png'):
File path = ... // base path of the images
// load source images
BufferedImage image = ImageIO.read(new File(path, "image.png"));
BufferedImage overlay = ImageIO.read(new File(path, "overlay.png"));
// create the new image, canvas size is the max. of both image sizes
int w = Math.max(image.getWidth(), overlay.getWidth());
int h = Math.max(image.getHeight(), overlay.getHeight());
BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
// paint both images, preserving the alpha channels
Graphics g = combined.getGraphics();
g.drawImage(image, 0, 0, null);
g.drawImage(overlay, 0, 0, null);
// Save as new image
ImageIO.write(combined, "PNG", new File(path, "combined.png"));
Wednesday, 21 May 2014
Version 1.49a 15 May 2014
Version 1.49a (upgrade)
- Thanks to Kenneth Sloan, added a "Paint on overlay" option to the built in brush tool's options dialog. Add the built in brush tool to the toolbar by selecting "Brush" from the toolbar's ">>" menu.
- Thanks to Daniel Pensold, Analyze>Tools>Save XY Coordinatessaves the coordinates and values of pixels inside selections to a .csv file.
- Thanks to Volker Wirth, added a "Title:" field to the dialog displayed by Edit>Selection>Straighten when the line width is one or when processing a stack.
- Selections created by the selection brush tool are deleted after they are added to an overlay.
- Thanks to Jon Harman, Plugins>Install recognizes .zip files.
- Thanks to Jerome Mutterer, added the makeArrow() macro function (example).
- Thanks to Federico Luzzati, added the Stack.toggleChannel() macro function (example).
- Thanks to Norbert Vischer, the showProgress(progress) macro function displays subordinate progress bars as moving dots when 'progress' is negative (macro example, script example).
- Thanks to Messaoudi Cedric, Edit>Options>Memory & Threadsno longer limits the number of threads to 32.
- Thanks to Wilhelm Burger, added the IJ.setProperty(String,Object) and IJ.getProperty(String) methods, which are useful for passing data between plugins.
- Thanks to Stephan Preibisch, added the MultiLineLabel#setText() method (example).
- Fixed a bug that caused the recording mode to not be saved when quitting ImageJ with the Recorder open.
- Thanks to Mats Nilsson, fixed a bug that caused the getInfo() macro function and ImagePlus#getStringProperty() method to not work with DICOM tags containing letters.
- Thanks to Norbert Vischer, fixed a bug the caused the Array.findMaxima() macro function to throw an exception when the array length was one.
- Fixed a bug that caused images with overlays containing black transparent images to not be correctly saved and restored.
- Thanks to Jan Brocher, fixed a v1.48 regression that caused changes made in the Image>Overlay>Add to Overlay dialog (displayed when you press alt+b) to not be remembered.
- Thanks to Kees Straatman, fixed a bug that caused the recorder to incorrectly record the Process>Binary>Convert to Mask command.
- Thanks to Chris Weisiger, fixed a bug in Image>Stacks>Othogonal Viewsthat caused it to fail with extened StackWindows with custom controls.
- Thanks to Philippe Carl, fixed a v1.48t regression that caused the Plot#drawNormalizedLine() method to not work with log plots.
Labels:
ImageJ,
Version 1.49a
Location:
Ahmedabad, Gujarat, India
Monday, 19 May 2014
Draw a semi ring - JavaFX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SemiDemo extends Application { | |
@Override | |
public void start(Stage primaryStage) { | |
Group root = new Group(); | |
root.getChildren().add(drawSemiRing(120, 120, 100, 50, Color.LIGHTGREEN, Color.DARKGREEN)); | |
root.getChildren().add(drawSemiRing(350, 350, 200, 30, Color.LIGHTSKYBLUE, Color.DARKBLUE)); | |
Scene scene = new Scene(root, 300, 250); | |
primaryStage.setScene(scene); | |
primaryStage.show(); | |
} | |
private Path drawSemiRing(double centerX, double centerY, double radius, double innerRadius, Color bgColor, Color strkColor) { | |
Path path = new Path(); | |
path.setFill(bgColor); | |
path.setStroke(strkColor); | |
path.setFillRule(FillRule.EVEN_ODD); | |
MoveTo moveTo = new MoveTo(); | |
moveTo.setX(centerX + innerRadius); | |
moveTo.setY(centerY); | |
ArcTo arcToInner = new ArcTo(); | |
arcToInner.setX(centerX - innerRadius); | |
arcToInner.setY(centerY); | |
arcToInner.setRadiusX(innerRadius); | |
arcToInner.setRadiusY(innerRadius); | |
MoveTo moveTo2 = new MoveTo(); | |
moveTo2.setX(centerX + innerRadius); | |
moveTo2.setY(centerY); | |
HLineTo hLineToRightLeg = new HLineTo(); | |
hLineToRightLeg.setX(centerX + radius); | |
ArcTo arcTo = new ArcTo(); | |
arcTo.setX(centerX - radius); | |
arcTo.setY(centerY); | |
arcTo.setRadiusX(radius); | |
arcTo.setRadiusY(radius); | |
HLineTo hLineToLeftLeg = new HLineTo(); | |
hLineToLeftLeg.setX(centerX - innerRadius); | |
path.getElements().add(moveTo); | |
path.getElements().add(arcToInner); | |
path.getElements().add(moveTo2); | |
path.getElements().add(hLineToRightLeg); | |
path.getElements().add(arcTo); | |
path.getElements().add(hLineToLeftLeg); | |
return path; | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
Subscribe to:
Posts (Atom)