Saturday 24 August 2013

Finding minimum possible degree of a graph(n,e)

The following code snippet is to find the minimum possible degree of a graph whose number of vertices(n) and edges(e) are given.
The algorithm is such that a vertex can have maximum degree equal to (n-1) but if it is having degree 1 it means there is one more vertex whose degree is one and if its degree is 2 then there are 2 vertices whose degree is 1.
The here is the code for finding minimum degree of a graph

import java.io.*;
import java.util.Arrays;
public class MinDegree {
        //function to find sum of an array
 static int sum(int[] arr){
  int sum = 0;
  for(int a: arr){
   sum += a;
  }
  return sum;
 }
        //function to find minimum of an array
 static int minimum(int[] arr){
  int min = Integer.MAX_VALUE;
  for(int i=0; i<arr.length; i++){
    if(arr[i] < min)
       min = arr[i];
   }
  }
  return min;
 }
 public static void main(String[] ar) throws Exception{
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                //number of edges should not exceed n(n-1)/2
                System.out.println("Enter no of vertices and edges: n e");
  String[] in = br.readLine().split(" ");//input should be like 5 7
  int vertices = Integer.parseInt(in[0]);//parsing string into integers
  int edges = Integer.parseInt(in[1]);
  int[] degree = new int[vertices];//buffer to store degrees 
  int counter = 0;
  int round = 0;
  while(sum(degree) < (2*edges)){//sum of degrees must be less than 2*edges
   if(degree[counter] < (vertices-1)){
    degree[counter] += 1;
    degree[round+counter+1] += 1;//it will increase neighbours degree
    round++;
   }
   else{
    counter++;
    round = 0;
   }
  }
  System.out.println(minimum(degree));
 }
}

Happy Coding :)

Thursday 15 August 2013

Converting OpenCV Mat to BufferedImage of Java

While working with opencv and java i got stuck on how to display an image in java's JFrame, because i can't directly put Mat object of opencv in drawImage function of Java's Graphics class, so i need a hack to convert Mat object into BufferedImage which is as follows.
First think of how can we do it, we have some methods to make a BufferedImage such as
  • Directly passing the path of image, which is not the case.
  • Passing stream of bytes of image into BufferedImage, which can be useful, lets focus on it.

Suppose image is my Mat object
Mat image = Highgui.imread("/Users/Sumit/Desktop/image.jpg");
and i want to convert it into BufferedImage
So lets take an object of MatOfByte.
MatOfByte bytemat = new MatOfByte();
byte mat is to convert image into stream of bytes.
Highgui.imencode(".jpg", image, bytemat);
this will put the bytes into bytemat object
Now convert byte mat into array of bytes.
byte[] bytes = bytemat.toArray();
Pass these bytes into ByteArrayInputStream to get a stream of bytes.
InputStream in = new ByteArrayInputStream(bytes);
Now pass this stream in read method of ImageIO class.
BufferedImage img = ImageIO.read(in);
Now we have BufferedImage object, we can use it to display it in JFrame's paint method.

This is the complete snippet
Mat image = Highgui.imread("/Users/Sumit/Desktop/image.jpg");
MatOfByte bytemat = new MatOfByte();
Highgui.imencode(".jpg", image, bytemat);
byte[] bytes = bytemat.toArray();
InputStream in = new ByteArrayInputStream(bytes);
BufferedImage img = ImageIO.read(in);
Happy Coding!!! :)

Monday 5 August 2013

Installing opencv for java on MAC OS X and Configuring Eclipse

Opencv is an open source library for computer vision, Now i am going to show you how to install it on Mac OS X.
Steps:

  • Download a fresh copy of opencv-2.4.6.1 from here.
  • Extract it to some location in your Mac.
  • now open terminal go inside the extracted folder opencv-2.4.6.1.
  • Type following commands
  • mkdir build
  • cd build
  • Make sure your JAVA_HOME environment variable is set or explicitly type export JAVA_HOME=<location of java home folder>
  • cmake -G "Unix Makefiles" -D CMAKE_CXX_COMPILER=/usr/bin/g++ -D CMAKE_C_COMPILER=/usr/bin/gcc -D WITH_CUDA=ON .. 
  • make -j4 // here number 4 means the number of cores in your processor
  • make install

Configuring Eclipse with opencv


  • Open eclipse
  • Click on File-> New Project-> Java Project
  • Give an appropriate Project Name
  • Now right click on Project and choose Properties.

  • Select Java Build Path and then select Libraries.


  • Click on Add Library.


  • Choose User Library.
  • Click on User Libraries, then Select New and give a name to your library.


  • Now Select your library and click on Add External JARs, go to your build folder and then open bin folder, there you will find opencv-246.jar, Select this file.
  • If you are not able to find .jar then make sure your JAVA_HOME variable is set in the environment variables at the time of build.
  • Now Select Native Library Location and click on Edit.


  • Insert the path of your cv.so file, mine is in /build/lib.


  • Click on Finish, Now your project is configured with openCV library.