Sunday, 24 August 2014

Java : Drawing the shape of the detected object?

After detecting the people upperbody rect:
  1. Remove the rect background, keeping just the person upperbody.
  2. Binarize the image.
  3. Apply morphological boundary algorithm to trace the upperbody.
Example:
enter image description here
OpenCV provides these algorithms. However, the example above was developed using Marvin. The source code is presented below:
public class TraceShape {

    public TraceShape(){
        // Load Plug-in
        MarvinImagePlugin boundary = MarvinPluginLoader.loadImagePlugin( 
        "org.marvinproject.image.morphological.boundary");

        // Load image
        MarvinImage image = MarvinImageIO.loadImage("./res/person.jpg");

        // Binarize
        MarvinImage binImage = MarvinColorModelConverter.rgbToBinary(image, 245);
        MarvinImageIO.saveImage(binImage, "./res/person_bin.png");

        // Boundary
        boundary.process(binImage.clone(), binImage);
        MarvinImageIO.saveImage(binImage, "./res/person_boundary.png");
    }

    public static void main(String[] args) {
        new TraceShape();
    }
}

Thursday, 21 August 2014

Java : MultiKeyMap Example

  •  A Map implementation that uses multiple keys to map the value.
  • This class is the most efficient way to uses multiple keys to map to a value. The best way to use this class is via the additional map-style methods. These provide get, containsKey, put and remove for individual keys which operate without extra object creation.
  • The additional methods are the main interface of this map. As such, you will not normally hold this map in a variable of type Map.
  • The normal map methods take in and return a MultiKey. If you try to use put() with any other object type a ClassCastException is thrown. If you try to use null as the key in put() a NullPointerException is thrown.
  • This map is implemented as a decorator of a AbstractHashedMap which enables extra behaviour to be added easily.
    •  MultiKeyMap.decorate(new LinkedMap()) creates an ordered map
    •  MultiKeyMap.decorate(new LRUMap()) creates an least recently used map.
    • MultiKeyMap.decorate(new ReferenceMap()) creates a garbage collector sensitive map. 
    class MultiKeyMapDemo {
    public static void main(String args[]){
    MultiKeyMap multiKeyMap = new MultiKeyMap();
    multiKeyMap.put("Key 1A","Key 1B","Value 1");
    multiKeyMap.put("Key 2A","Key 2B","Value 2");
    multiKeyMap.put("Key 3A","Key 3B","Value 3");
    MapIterator it = multiKeyMap.mapIterator();
    while (it.hasNext()) {
    it.next();
    MultiKey mk = (MultiKey) it.getKey();
    // Option 1
    System.out.println(mk.getKey(0));
    System.out.println(mk.getKey(1));
    // Option 2
    for (Object subkey : mk.getKeys())
    System.out.println(subkey);
    System.out.println(it.getValue());
    }
    }
    }
  • Note that IdentityMap and ReferenceIdentityMap are unsuitable for use as the key comparison would work on the whole MultiKey, not the elements within.
  • Note that MultiKeyMap is not synchronized and is not thread-safe. If you wish to use this map from multiple threads concurrently, you must use appropriate synchronization. This class may throw exceptions when accessed by concurrent threads without synchronization.
  • Apache Commons Collections to create a MultiKeyMap that will store two keys with one corresponding value and then using MapIterator to walk through the map.
  • MultiKeyMap keys are instances of MultiKey, which has a getKey(int index) method. There is also a getKeys() method which returns Object[].

Tuesday, 12 August 2014

Java Program to Implement Doubly Linked List

/*
* Java Program to Implement Doubly Linked List
*/
import java.util.Scanner;
/* Class Node */
class Node {
protected int data;
protected Node next, prev;
/* Constructor */
public Node() {
next = null;
prev = null;
data = 0;
}
/* Constructor */
public Node(int d, Node n, Node p) {
data = d;
next = n;
prev = p;
}
/* Function to set link to next node */
public void setLinkNext(Node n) {
next = n;
}
/* Function to set link to previous node */
public void setLinkPrev(Node p) {
prev = p;
}
/* Funtion to get link to next node */
public Node getLinkNext() {
return next;
}
/* Function to get link to previous node */
public Node getLinkPrev() {
return prev;
}
/* Function to set data to node */
public void setData(int d) {
data = d;
}
/* Function to get data from node */
public int getData() {
return data;
}
}
/* Class linkedList */
class linkedList {
protected Node start;
protected Node end;
public int size;
/* Constructor */
public linkedList() {
start = null;
end = null;
size = 0;
}
/* Function to check if list is empty */
public boolean isEmpty() {
return start == null;
}
/* Function to get size of list */
public int getSize() {
return size;
}
/* Function to insert element at begining */
public void insertAtStart(int val) {
Node nptr = new Node(val, null, null);
if (start == null) {
start = nptr;
end = start;
} else {
start.setLinkPrev(nptr);
nptr.setLinkNext(start);
start = nptr;
}
size++;
}
/* Function to insert element at end */
public void insertAtEnd(int val)
{
Node nptr = new Node(val, null, null);
if (start == null) {
start = nptr;
end = start;
} else {
nptr.setLinkPrev(end);
end.setLinkNext(nptr);
end = nptr;
}
size++;
}
/* Function to insert element at position */
public void insertAtPos(int val, int pos) {
Node nptr = new Node(val, null, null);
if (pos == 1) {
insertAtStart(val);
return;
}
Node ptr = start;
for (int i = 2; i <= size; i++) {
if (i == pos) {
Node tmp = ptr.getLinkNext();
ptr.setLinkNext(nptr);
nptr.setLinkPrev(ptr);
nptr.setLinkNext(tmp);
tmp.setLinkPrev(nptr);
}
ptr = ptr.getLinkNext();
}
size++;
}
/* Function to delete node at position */
public void deleteAtPos(int pos) {
if (pos == 1) {
if (size == 1) {
start = null;
end = null;
size = 0;
return;
}
start = start.getLinkNext();
start.setLinkPrev(null);
size--;
return;
}
if (pos == size) {
end = end.getLinkPrev();
end.setLinkNext(null);
size--;
}
Node ptr = start.getLinkNext();
for (int i = 2; i <= size; i++) {
if (i == pos) {
Node p = ptr.getLinkPrev();
Node n = ptr.getLinkNext();
p.setLinkNext(n);
n.setLinkPrev(p);
size--;
return;
}
ptr = ptr.getLinkNext();
}
}
/* Function to display status of list */
public void display() {
System.out.print("\nDoubly Linked List = ");
if (size == 0) {
System.out.print("empty\n");
return;
}
if (start.getLinkNext() == null) {
System.out.println(start.getData());
return;
}
Node ptr = start;
System.out.print(start.getData() + " <-> ");
ptr = start.getLinkNext();
while (ptr.getLinkNext() != null) {
System.out.print(ptr.getData() + " <-> ");
ptr = ptr.getLinkNext();
}
System.out.print(ptr.getData() + "\n");
}
}
/* Class DoublyLinkedList */
public class DoublyLinkedList {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
/* Creating object of linkedList */
linkedList list = new linkedList();
System.out.println("Doubly Linked List Test\n");
char ch;
/* Perform list operations */
do {
System.out.println("\nDoubly Linked List Operations\n");
System.out.println("1. insert at begining");
System.out.println("2. insert at end");
System.out.println("3. insert at position");
System.out.println("4. delete at position");
System.out.println("5. check empty");
System.out.println("6. get size");
int choice = scan.nextInt();
switch (choice) {
case 1:
System.out.println("Enter integer element to insert");
list.insertAtStart(scan.nextInt());
break;
case 2:
System.out.println("Enter integer element to insert");
list.insertAtEnd(scan.nextInt());
break;
case 3:
System.out.println("Enter integer element to insert");
int num = scan.nextInt();
System.out.println("Enter position");
int pos = scan.nextInt();
if (pos < 1 || pos > list.getSize())
System.out.println("Invalid position\n");
else
list.insertAtPos(num, pos);
break;
case 4:
System.out.println("Enter position");
int p = scan.nextInt();
if (p < 1 || p > list.getSize())
System.out.println("Invalid position\n");
else
list.deleteAtPos(p);
break;
case 5:
System.out.println("Empty status = " + list.isEmpty());
break;
case 6:
System.out.println("Size = " + list.getSize() + " \n");
break;
default:
System.out.println("Wrong Entry \n ");
break;
}
/* Display List */
list.display();
System.out.println("\nDo you want to continue (Type y or n) \n");
ch = scan.next().charAt(0);
} while (ch == 'Y' || ch == 'y');
}
}