## Sunday, December 7, 2014

### Superpixel algorithm implemented in Java

This Superpixel code has been stored on my hard drive for a year. Too bad, I have only very little time for my recreational image processing programming.

But here is my interpretation for Superpixel algorithm in Java programming language. The computational process itself is iterative, but I am using the Cluster objects to make the algorithm slightly easier to understand and follow. Without objects, it could be slightly faster, I have not tried to code that kind of version myself.

Interesting things to try:
• change the proximity modifier 'm' from small to large
• change the cell size 'S' from small to large

Notes
1. The algorithm does not ensure superpixel connectivity, thus the result may have orphan pixels from 'wrong' cluster inside of other cluster.
2. This algorithm works in RGB color space, some other color space (Lab for example) could give you better end results.
3. The distance calculation can be done also using "approximated" distance without square root calculations, for performance reasons.
Image segmentation in Wikipedia: http://en.wikipedia.org/wiki/Image_segmentation
SLIC Superpixels - http://ivrg.epfl.ch/research/superpixels
jSLIC code in the GitHub - https://github.com/Borda/ij-CMP-BIA

Example images

Values used: left S=16, m=130, right S=24, m=130

Values used: left S=16, m=130, right S=24, m=130

Values used: left S=16, m=130, right S=24, m=130

Example usage
Using command line

java popscan.Superpixel "C:\java\flamingo.png" c:\java\sp_flamingo.png 16 130

Source

 package popscan; import java.awt.Color; import java.awt.image.BufferedImage; import java.io.File; import java.util.Arrays; import java.util.Vector; import javax.imageio.ImageIO; /** * @author tejopa, 2014 * @version 1 * http://popscan.blogspot.com */ public class Superpixel { // arrays to store values during process double[] distances; int[] labels; int[] reds; int[] greens; int[] blues; Cluster[] clusters; // in case of instable clusters, max number of loops int maxClusteringLoops = 50; /** * @param args */ public static void main(String[] args) { if (args.length!=4) { System.out.println("Usage: java popscan.Superpixel" + " [source image filename]" + " [destination image filename]" + " [cell width S (1-255)]" + " [proximity modifier m (1-255)"); return; } // parse arguments String src = args[0]; String dst = args[1]; double S = Integer.parseInt(args[2]); double m = Double.parseDouble(args[3]); BufferedImage img = loadImage(src); Superpixel sp = new Superpixel(); BufferedImage dstImage = sp.calculate(img,S,m); // save the resulting image saveImage(dst, dstImage); } public Superpixel() {    } public BufferedImage calculate(BufferedImage image, double S, double m) { int w = image.getWidth(); int h = image.getHeight(); BufferedImage result = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); long start = System.currentTimeMillis(); // get the image pixels int[] pixels = image.getRGB(0, 0, w, h, null, 0, w); // create and fill lookup tables distances = new double[w*h]; Arrays.fill(distances, Integer.MAX_VALUE); labels = new int[w*h]; Arrays.fill(labels, -1); // split rgb-values to own arrays reds = new int[w*h]; greens = new int[w*h]; blues = new int[w*h]; for (int y=0;y>16&0x000000FF; greens[pos] = color>> 8&0x000000FF; blues[pos]  = color>> 0&0x000000FF; } } // create clusters createClusters(image, S, m); // loop until all clusters are stable! int loops = 0; boolean pixelChangedCluster = true; while (pixelChangedCluster&&loops temp = new Vector(); int w = image.getWidth(); int h = image.getHeight(); boolean even = false; double xstart = 0; int id = 0; for (double y=S/2;y

## Tuesday, June 17, 2014

### Problem connecting to Netflix - my solution

Recently I encountered an error when trying to open Netflix app on my Lenovo Android tablet. Netflix used to work flawlessly, but suddenly it stopped working on just one of my devices.

When trying to login, an error message appeared. The message was "There is a problem connecting to Netflix. Please try again later. If the problem persists, check that your device has the correct date and time and restart the Netflix application."

Actually, as the device is in Finnish locale, the message was "Netflix-yhteyden luomisessa oli ongelma. Yritä myöhemmin uudelleen. Jos ongelma ei poistu, tarkista että laitteessasi on oikea päivämäärä ja aika ja käynnistä Netflix-sovellus uudelleen."

I tried to reset my router, set the time and date for my router and devices etc, nothing helped. I removed Netflix app and reinstalled it. Everything else was working perfectly (net, games, etc) but still I was not able to connect to Netflix. And my kids were getting angry.

Finally I tried to check the DNS settings on my tablet. They pointed to my router, where I have Google's servers configured. Well, I thought, why not set the same IPs to the tablet as well.

So, I set up the Google name server IPs as DNS servers... and Netflix started to work again.

Steps to set up the DNS in Android 4.x devices:
1. Select "Settings"
2. Select "Wi-Fi"
3. Long tap your wifi network item - a pop up chooser will appear
4. Select "Modify network config"
5. Select "Show advanced options" - new options will appear
6. Select "IP settings" - select "Static" from drop down menu
7. Set "DNS1" to "8.8.8.8" (write down the original IP, in case you need it later)
8. Set "DNS2" to "8.8.4.4" (write down the original IP, in case you need it later)
9. Select "Save"
10. Start watching Netflix ;)

## Sunday, April 20, 2014

### Watershed image segmentation algorithm with Java

I am very interested in image segmentation, that is why the watershed segmentation caught my attention this time. People are using the watershed algorithm at least in the medical imaging applications, and the F. Meyer's algorithm was mentioned to be "one of the most common" one [1]. It also looked like it is quite easy to implement.

My favorite method for studying and understanding algorithms is to actually implement the algorithm. During the implementation, you really have to understand what is happening in each step. Also the produced images (in image algorithms) during the development process are helpful. It is fun to analyze the errors and what causes the artifacts in the image. So, the result for my studies is here: the watershed image segmentation algorithm implemented with Java.

#### Watershed algorithm explained (italics are from Wikipedia):

1. "A set of markers, pixels where the flooding shall start, are chosen. Each is given a different label."
• Find all the minimum points from the image. This means that you need to loop through pixels and find the darkest pixel which has not been already selected and which is not close to already selected minimum pixel. You can find as many minimums you like or only selected count of minimums. These minimums are the lowest points of the "basins" which will be flooded.
• The label can be for example the index numbering or color values from a known premade palette.

2. "The neighboring pixels of each marked area are inserted into a priority queue with a priority level corresponding to the gray level of the pixel."
• For each found minimum point, add the surrounding pixels to the priority queue. Set the initial labels to the label table (result image). Sort the priority queue from dark to light pixels. Now you can start the flooding.

3. "The pixel with the highest priority level is extracted from the priority queue. If the neighbors of the extracted pixel that have already been labeled all have the same label, then the pixel is labeled with their label. All non-marked neighbors that are not yet in the priority queue are put into the priority queue."
• Take out the first pixel (the darkest one) from the priority queue. Check the neighboring pixels for that pixel. If all the labeled pixels are having the same label, label the current pixel with the same label; the current pixel is "inside" of the basin. Remember, there has to be at least one labeled pixel; the one that was previously next to the current pixel.
• If there are at least two pixels with different label, it means that we have found a boundary between two different flood basins. We can mark this pixel then as an "edge" pixel.
• Finally, add all those neighboring pixels that do not have label to the priority queue.

4. "Redo step 3 until the priority queue is empty."
• Loop until there are no pixels left in the priority queue. When the priority queue is empty, all the pixels from the image are processed.

#### Sample images

Image 1. This is the sample image I have been using for testing. I created it myself with GIMP.

Image 2. This is the result after executing the Watershed algorithm to the sample image. Red squares are the found minimums and the white borders are the basins edges.

Image 3. Here are the sample image and the result image combined in a same image. The boundaries have been colored to green.

#### Notes:

• In this implementation, the minimums are found by "sweeping" the pixels from top-left to bottom-right. This way the found minimum is always the "first" one with the lowest value. That is why the minimum centers (the red squares) are not in the exact center of the darkest area.
• If you rotate the source image, the result will be different, that is because the minimum locations are changing (see the note above) and this discussion [2].
• After segmentation, you probably need the remove the "background" pixels. What you need to do might change per application. See more examples for fine tuning the Watershed algorithm from http://cmm.ensmp.fr/~beucher/wtshed.html [3].

#### The Java source

Implementation notes:
• To find more speed in the pixel sorting, I implemented a crude SortedVector, which uses insertion sort like algorithm to keep the inserted pixels (FloodPoints) in sorted order.
• I am disabling the pixels from a rectangle shaped area around the minimum. A circle shape should work better in some cases.
• The source contains commented code for saving the image in different steps of the process. Just uncomment the lines to get the images saved on your drive.
• The source contains commented code for real time view of the process. Just uncomment the parts where the JFrame is accessed to see the final image being built. You should try that, it looks nice! :)
• You can run the code in 4-connected pixels or 8-connected pixels mode. The results are slightly different. You should try them both.
Please let me know if you spot errors or have any other comments. Thanks!

#### Usage

java Watershed [source file] [result file] [number of segments, 1-256] [minimum window width, 8-256] [connected pixels, 4|8]

For example:
java Watershed sample_image.png result.png 22 60 8

#### SortedVector.java

 import java.util.Vector; public class SortedVector { Vector> data; public SortedVector() { data = new Vector>(); } public void add(Object o) { if (o instanceof Comparable) { insert((Comparable