Marvin, awesome Image processing framework

Hello guys, its along time i wrote my blog post. these days i got little busy with my project work and deliverables so i took little time to write up this article. Suddenly i thought of working with simple sixth sensing device so i started working little bit and found a very good frameworks “OpenCV”,”JavaCV” and “Marvin” already i know how to use OpenCV now as of my personal interest i choose Marvin. so in this i try to write a very simple code to invert a image.

Please download the marvin framework from the below link.download marvin

now once your marvin framework got downloaded, create a project in your eclipse and the project structure should look as below.

ImageConvert

once your config is done, its time to write the code as below.

/**
 * 
 */
package com.spark.marving.samples;

import marvin.image.MarvinImage;
import marvin.io.MarvinImageIO;
import marvin.plugin.MarvinImagePlugin;
import marvin.util.MarvinPluginLoader;

/**
 * @author Sony
 * 
 */
public class InvertImages {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MarvinImage image = MarvinImageIO.loadImage("./images/Osmania.jpg");

		MarvinImagePlugin invertplugin = MarvinPluginLoader
				.loadImagePlugin("org.marvinproject.image.color.invert.jar");

		invertplugin.process(image, image);
		image.update();

		MarvinImageIO.saveImage(image, "./images/conertedImage.jpg");
	}

}

once this code is done and we start running the result is something like below.

Osmania

conertedImage

Advertisements

Webcam Access using Java and Maven

After lots of implementations on JMF,OpenCV and JavaCV i came across “Webcam Capture” a Generic Webcam Java Utility which has the capability of maven dependency management which is the best part i have seen. Following are the core features of the api.

Core Features

Simple and thread-safe API – write only few lines of code and start capturing images.
Work standalone – no additional software has to be installed to use it.
System independent – it will run without problems on most widely used operating systems – Windows (2k/XP/Vista/7, both 32- and 64-bit), Linux (x86, x64 and 32-bit ARM) and Mac OS X.
Built-in video driver – default built-in video grabber is provided, so you don’t need to install any additional software (such as JMF, etc).
Implementation independent – write your code once and don’t care which capture framework you will be using in future (LTI-CIVIL, JMF, QTJ, etc).
Automated resources management – you do not have to remember to deallocate resources used by your capturing device, library will always do that for you (while VM has been closed gracefully).
Motion detector – build in motion detector makes it easy to implement motion detecting features.
Available in Maven Central – it is available in Maven Central so your project can be deployed there too.
Re-streamer – simple JPEG/MJPEG re-streamer is build in, just run it and enjoy hosting your live stream.
Swing ready – contains Swing component to display images, together with painter API and thread-safe API which prevents Swing EDT from blocking issues when using camera.

How To Start

Webcam Capture is avilable in Maven Central, so if you can add its dependency to your project if you are using one of the dependency manager like Maven add the following code to your pom.xml file.

    <dependency>
      <groupId>com.github.sarxos</groupId>
      <artifactId>webcam-capture</artifactId>
      <version>0.3.7</version>
    </dependency>

here is the code for “MotionDetection” using Webcam Capture API

package com.spark.webcam;

import java.io.IOException;

import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamMotionDetector;
import com.github.sarxos.webcam.WebcamMotionEvent;
import com.github.sarxos.webcam.WebcamMotionListener;


/**
 * Detect motion.
 * 
 * @author Mantha Pavan Kumar
 */
public class DetectMotionExample implements WebcamMotionListener {

	public DetectMotionExample() {
		WebcamMotionDetector detector = new WebcamMotionDetector(Webcam.getDefault());
		detector.setInterval(100); // one check per 100 ms
		detector.addMotionListener(this);
		detector.start();
	}

	@Override
	public void motionDetected(WebcamMotionEvent wme) {
		System.out.println("Detected motion I, alarm turn on you have");
	}

	public static void main(String[] args) throws IOException {
		new DetectMotionExample();
		System.in.read(); // keep program open
	}
}

and for further details please visit: http://webcam-capture.sarxos.pl/ and follow the site.
Happy Coding 🙂

Converting a color Image to a grayscale image programatically using Java

Hi in the last couple of days i was working with one of my friend to create a website.We had a requirement in which, we should put lot of images that need to be converted as gray scale images from color, Then we had lot of work with photoshop to make every image into gray scale and then port into server which is little tedious job, then i have made this program which will work perfectly to make colored images into gray scale images and i looped it in the image list to get all images as gray scale on the fly. Thanks to Java API 🙂

The below code is self explanatory.

/**
*
*/
package com.spark.image.converter;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
* @author Mantha Pavan Kumar
*
*/
public class Color2Grayscale {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// Save the image file as a BufferedImage object
BufferedImage cat = ImageIO.read(new File("image.JPG"));

// Loop through all the pixels in the image (w = width, h = height)
for (int w = 0; w < cat.getWidth(); w++) {
for (int h = 0; h < cat.getHeight(); h++) {
// BufferedImage.getRGB() saves the colour of the pixel as a
// single integer.
// use Color(int) to grab the RGB values individually.
Color color = new Color(cat.getRGB(w, h));

// use the RGB values to get their average.
int averageColor = ((color.getRed() + color.getGreen() + color
.getBlue()) / 3);

// create a new Color object using the average colour as the
// red, green and blue
// colour values
Color avg = new Color(averageColor, averageColor, averageColor);

// set the pixel at that position to the new Color object using
// Color.getRGB().
cat.setRGB(w, h, avg.getRGB());
}
}

// save the newly created image in a new file.
ImageIO.write(cat, "jpg", new File("image_greyscale.jpg"));
}

}

Happy Coding.

Comparing two Images in Java

There are many scenarios where i tried to compare images but failed to compare them. Image comparison is a very deep concept where there involved lot many complex algorithms . In brief for Two images to be same we need to compare the two images pixel by pixel so i came across PixelGrabber class in java and started using it which gave a positive result, but not accurate.
The sample code for image comparision as follows:

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.PixelGrabber;

public class Compare {

static void processImage() {

String file1 = "img1.png";
String file2 = "img2.png";

Image image1 = Toolkit.getDefaultToolkit().getImage(file1);
Image image2 = Toolkit.getDefaultToolkit().getImage(file2);

try {

PixelGrabber grab1 =new PixelGrabber(image1, 0, 0, -1, -1, false);
PixelGrabber grab2 =new PixelGrabber(image2, 0, 0, -1, -1, false);

int[] data1 = null;

if (grab1.grabPixels()) {
int width = grab1.getWidth();
int height = grab1.getHeight();
data1 = new int[width * height];
data1 = (int[]) grab1.getPixels();
}

int[] data2 = null;

if (grab2.grabPixels()) {
int width = grab2.getWidth();
int height = grab2.getHeight();
data2 = new int[width * height];
data2 = (int[]) grab2.getPixels();
}

System.out.println("Pixels equal: " + java.util.Arrays.equals(data1, data2));

} catch (InterruptedException e1) {
e1.printStackTrace();
}
}

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

Happy Coding 🙂