<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-4745797930549018249</id><updated>2011-07-30T15:07:23.477-07:00</updated><title type='text'>Programs</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://paulruegger.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4745797930549018249/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://paulruegger.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Paul Ruegger</name><uri>http://www.blogger.com/profile/13147133491710381045</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4745797930549018249.post-5759320935097986336</id><published>2009-10-01T13:41:00.000-07:00</published><updated>2009-10-09T12:27:58.778-07:00</updated><title type='text'>Merging hashes in Perl</title><content type='html'>Recently I needed to merge two Perl hashes such that the values of the merged hash would be the sum of the two hashes whenever the keys were the same and just the value of one hash whenever they were unique.&lt;br /&gt;I found several examples of merging hashes on the Internet but none did exactly what I needed. &amp;nbsp;This is a common example:&lt;br /&gt;&lt;br /&gt;%mergedhash = (%hash1, %hash2);&lt;br /&gt;&lt;br /&gt;This method does make the keys of&amp;nbsp;%mergedhash the union of the other hash's keys. &amp;nbsp;However,&amp;nbsp;when the keys of the two hashes are the same&amp;nbsp;the values of&amp;nbsp;%mergedhash simply take on the values of the second hash in the list (%hash2), not the addition of %hash1 and %hash2 values as I needed.&lt;br /&gt;Obviously, one could write a loop to do this but I wanted something faster because I was dealing with very large hashes.&lt;br /&gt;The solution I found uses the &lt;b&gt;map&lt;/b&gt; function:&lt;br /&gt;&lt;br /&gt;%mergedhash = &lt;b&gt;map&lt;/b&gt; { &lt;b&gt;$_&lt;/b&gt;&amp;nbsp;=&amp;gt; $hash1{&lt;b&gt;$_&lt;/b&gt;} + $hash2{&lt;b&gt;$_&lt;/b&gt;} } (&lt;b&gt;keys&lt;/b&gt; %hash1, &lt;b&gt;keys&lt;/b&gt; %hash2);&lt;br /&gt;&lt;br /&gt;Another way is to combine the two methods:&lt;br /&gt;&lt;br /&gt;%mergedhash = (%hash1, %hash2);&lt;br /&gt;%mergedhash =&amp;nbsp;&lt;b&gt;map&lt;/b&gt;&amp;nbsp;{&amp;nbsp;&lt;b&gt;$_&amp;nbsp;&lt;/b&gt;=&amp;gt;&amp;nbsp;$hash1{&lt;b&gt;$_&lt;/b&gt;} + $hash2{&lt;b&gt;$_&lt;/b&gt;} }&amp;nbsp;&lt;b&gt;keys&lt;/b&gt;&amp;nbsp;%mergedhash;&lt;br /&gt;&lt;br /&gt;The advantage here is that the &lt;b&gt;map&lt;/b&gt; function is not adding the two values two separate times; it is receiving the keys only once, not twice as in the prior example. &amp;nbsp;I haven't check to see which is faster. &amp;nbsp;If you do, let me know what you find out!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4745797930549018249-5759320935097986336?l=paulruegger.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://paulruegger.blogspot.com/feeds/5759320935097986336/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://paulruegger.blogspot.com/2009/10/merging-hashes-in-perl.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4745797930549018249/posts/default/5759320935097986336'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4745797930549018249/posts/default/5759320935097986336'/><link rel='alternate' type='text/html' href='http://paulruegger.blogspot.com/2009/10/merging-hashes-in-perl.html' title='Merging hashes in Perl'/><author><name>Paul Ruegger</name><uri>http://www.blogger.com/profile/13147133491710381045</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4745797930549018249.post-6783116920898488938</id><published>2009-07-20T14:07:00.001-07:00</published><updated>2009-09-01T16:04:29.588-07:00</updated><title type='text'>Text Pic To Image: An ImageJ Plugin</title><content type='html'>&lt;div&gt;Below is a quicky plugin I wrote for ImageJ to convert text pictures into tiff format.&lt;/div&gt;&lt;div&gt;Beware that this was made for me, not you, and it does not have the "safeguards" that you would get with a commercial program. I.e., there is very little error checking and it may crash easily.&lt;/div&gt;&lt;div&gt;That being said, if you happen to "make" a text picture using ImageJ's Batch_Converter plugin this plugin will convert it into a tiff image (something the Batch_Converter doesn't do currently), probably without crashing. Once you've got the tiff image you can use Batch_Converter (or other program) to change to whatever other image type you may need.&lt;/div&gt;&lt;div&gt;You are free to copy, use and/or modify the program so long as you are a nice person, and credit me in the obscure and mostly unread comments of your program. Failure to do so will cause untold misery in your life, such as guilt, angst, worry, nervousness, headaches, nightmares, rashes, dandruff, etc.&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;import ij.*;&lt;/div&gt;&lt;div&gt;import ij.gui.*;&lt;/div&gt;&lt;div&gt;import ij.plugin.*;&lt;/div&gt;&lt;div&gt;import ij.process.*;&lt;/div&gt;&lt;div&gt;import java.io.*;&lt;/div&gt;&lt;div&gt;import java.util.Scanner;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;/* TextPicToImage_ */&lt;/div&gt;&lt;div&gt;/* Written by Paul Ruegger */&lt;/div&gt;&lt;div&gt;/* Adapted from ImageJ's Batch_Converter plugin by Wayne Rasband */&lt;/div&gt;&lt;div&gt;/* 5/3/09&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;/**&lt;/div&gt;&lt;div&gt;TextPicToImage_ converts a folder of text images, created by&lt;/div&gt;&lt;div&gt;ImageJ's Batch_Converter plugin, into 16-bit TIFF images. The&lt;/div&gt;&lt;div&gt;plugin displays three dialogs. In the first, select the source&lt;/div&gt;&lt;div&gt;folder. In the second, select the format you want to convert&lt;/div&gt;&lt;div&gt;to (currently, only supports conversion to 16-bit TIFF). In the&lt;/div&gt;&lt;div&gt;third, select the destination folder.&lt;/div&gt;&lt;div&gt;**/&lt;/div&gt;&lt;div&gt;public class TextPicToImage_ implements PlugIn {&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;private static String[] choices = {"TIFF"};&lt;/div&gt;&lt;div&gt;private static String format = "TIFF";&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;public void run(String arg) {&lt;/div&gt;&lt;div&gt;String dir1 = IJ.getDirectory("Select source folder...");&lt;/div&gt;&lt;div&gt;if (dir1==null) return;&lt;/div&gt;&lt;div&gt;if (!showDialog()) return;&lt;/div&gt;&lt;div&gt;String dir2 = IJ.getDirectory("Select destination folder...");&lt;/div&gt;&lt;div&gt;if (dir2==null) return;&lt;/div&gt;&lt;div&gt;convert(dir1, dir2, format);&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;boolean showDialog() {&lt;/div&gt;&lt;div&gt;GenericDialog gd = new GenericDialog("Text Image to TIFF Converter");&lt;/div&gt;&lt;div&gt;gd.addChoice("Convert to: ", choices, format);&lt;/div&gt;&lt;div&gt;gd.showDialog();&lt;/div&gt;&lt;div&gt;if (gd.wasCanceled())&lt;/div&gt;&lt;div&gt;return false;&lt;/div&gt;&lt;div&gt;format = gd.getNextChoice();&lt;/div&gt;&lt;div&gt;return true;&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;public void convert(String dir1, String dir2, String format) {&lt;/div&gt;&lt;div&gt;IJ.log("\\Clear");&lt;/div&gt;&lt;div&gt;IJ.log("Converting to "+format);&lt;/div&gt;&lt;div&gt;IJ.log("dir1: "+dir1);&lt;/div&gt;&lt;div&gt;IJ.log("dir2: "+dir2);&lt;/div&gt;&lt;div&gt;String[] list = new File(dir1).list();&lt;/div&gt;&lt;div&gt;if (list==null) return;&lt;/div&gt;&lt;div&gt;for (int i=0; i&lt;/div&gt;&lt;div&gt;IJ.showProgress(i, list.length);&lt;/div&gt;&lt;div&gt;IJ.log((i+1)+": "+list[i]);&lt;/div&gt;&lt;div&gt;IJ.showStatus(i+"/"+list.length);&lt;/div&gt;&lt;div&gt;boolean isDir = (new File(dir1+list[i])).isDirectory();&lt;/div&gt;&lt;div&gt;int width = 0;&lt;/div&gt;&lt;div&gt;int height = 0;&lt;/div&gt;&lt;div&gt;short pixels[] = null;&lt;/div&gt;&lt;div&gt;if (!isDir &amp;amp;&amp;amp; !list[i].startsWith(".") &amp;amp;&amp;amp; list[i].endsWith(".txt")) {&lt;/div&gt;&lt;div&gt;String tempLine;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;try {&lt;/div&gt;&lt;div&gt;//Text images contain no header information so we must&lt;/div&gt;&lt;div&gt;//first read the file to calculate the image's dimensions&lt;/div&gt;&lt;div&gt;Scanner textimage = new Scanner(new BufferedReader (new FileReader(dir1+list[i])));&lt;/div&gt;&lt;div&gt;tempLine = textimage.nextLine();&lt;/div&gt;&lt;div&gt;//Calculate width&lt;/div&gt;&lt;div&gt;width = (tempLine.split("\t")).length;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;//Calculate height&lt;/div&gt;&lt;div&gt;while( textimage.hasNextLine() ) {&lt;/div&gt;&lt;div&gt;tempLine = textimage.nextLine();&lt;/div&gt;&lt;div&gt;//TODO find a better way to stop&lt;/div&gt;&lt;div&gt;//textimage.hasNext(pattern);&lt;/div&gt;&lt;div&gt;height++;&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;div&gt;textimage.close();&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;//Dim the short array used to store the pixel values from the file&lt;/div&gt;&lt;div&gt;pixels = new short[width*height];&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;//Re-open and read the file, storing the values therein.&lt;/div&gt;&lt;div&gt;Scanner tf = new Scanner(new BufferedReader (new FileReader(dir1+list[i])));&lt;/div&gt;&lt;div&gt;String[] stringElts = new String[width];&lt;/div&gt;&lt;div&gt;int pindex = 0;&lt;/div&gt;&lt;div&gt;for(int j=0; j&lt;/div&gt;&lt;div&gt;tempLine = tf.nextLine();&lt;/div&gt;&lt;div&gt;stringElts = (tempLine.split("\t"));&lt;/div&gt;&lt;div&gt;//Convert each string (number) into a short&lt;/div&gt;&lt;div&gt;for(int k=0; k&lt;/div&gt;&lt;div&gt;if(stringElts[k].isEmpty()) {&lt;/div&gt;&lt;div&gt;continue;&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;div&gt;if(Integer.parseInt(stringElts[k]) &amp;gt; 65535) {&lt;/div&gt;&lt;div&gt;pixels[pindex] = (short) 65535;&lt;/div&gt;&lt;div&gt;} else {&lt;/div&gt;&lt;div&gt;pixels[pindex] = (short) Integer.parseInt(stringElts[k]);&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;div&gt;pindex++;&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;} catch (FileNotFoundException e) {&lt;/div&gt;&lt;div&gt;// TODO Auto-generated catch block&lt;/div&gt;&lt;div&gt;e.printStackTrace();&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;div&gt;ImageProcessor ip = new ShortProcessor(width, height, pixels, null);&lt;/div&gt;&lt;div&gt;ImagePlus img = new ImagePlus(list[i], ip);&lt;/div&gt;&lt;div&gt;//    img.show();&lt;/div&gt;&lt;div&gt;WindowManager.setTempCurrentImage(img);&lt;/div&gt;&lt;div&gt;IJ.saveAs(format, dir2+list[i]);&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;div&gt;IJ.showProgress(1.0);&lt;/div&gt;&lt;div&gt;IJ.showStatus("");&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;Labels: ImageJ text pic picture convert converter&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4745797930549018249-6783116920898488938?l=paulruegger.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://paulruegger.blogspot.com/feeds/6783116920898488938/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://paulruegger.blogspot.com/2009/07/text-pic-to-image-imagej-plugin.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4745797930549018249/posts/default/6783116920898488938'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4745797930549018249/posts/default/6783116920898488938'/><link rel='alternate' type='text/html' href='http://paulruegger.blogspot.com/2009/07/text-pic-to-image-imagej-plugin.html' title='Text Pic To Image: An ImageJ Plugin'/><author><name>Paul Ruegger</name><uri>http://www.blogger.com/profile/13147133491710381045</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
