Skip to content

Category: english

OpenCV: adding two images

This is a very simple example of how to open two images and display them added.

I got two pictures at project Commons from Wikimedia that were highlighted on Featured Pictures. I did a crop on both to have the same size, as I’m trying to make this example as simple as possible.

The first one is a photo of our Milky Way, taken at Paranal Observatory by Stéphane Guisard.

milkyway

The second one is a California surfer inside wave, taken by Mila Zinkova.

surfer

In this simple OpenCV code below, we open the images, create a new one to display the result and use cvAdd to add them. We do not save the result or handle more than the ordinary case of two images with the same size.

#include 
#include 
#include 

int main( int argc, char **argv ){
    IplImage *surfer, *milkyway, *result;
    int key = 0;
    CvSize size;

    /* load images, check, get size (both should have the same) */
    surfer = cvLoadImage("surfer.jpg", CV_LOAD_IMAGE_COLOR);
    milkyway = cvLoadImage("milkyway.jpg", CV_LOAD_IMAGE_COLOR);
    if((!surfer)||(!milkyway)){
        printf("Could not open one or more images.");
        exit -1;
    }
    size = cvGetSize(surfer);

    /* create a empty image, same size, depth and channels of others */
    result = cvCreateImage(size, surfer->depth, surfer->nChannels);
    cvZero(result);

    /* result = surfer + milkyway (NULL mask)*/
    cvAdd(surfer, milkyway, result, NULL);
 
    /* create a window, display the result, wait for a key */
    cvNamedWindow("example", CV_WINDOW_AUTOSIZE);
    cvShowImage("example", result);
    cvWaitKey(0);

    /* free memory and get out */
    cvDestroyWindow("example");
    cvReleaseImage(&surfer);
    cvReleaseImage(&milkyway);
    cvReleaseImage(&result);
    return 0;
}

/* gcc add.c -o add `pkg-config opencv --libs --cflags` */

Compile it (on a well configured OpenCV development environment) and run it:

gcc add.c -o add `pkg-config opencv –libs –cflags`
./add

The result got pretty cool, a milky way surfer.

surfer in the milk way

OpenCV: Edge Detection

This is a simple example of how pass edge detection in a video using OpenCV. It uses the built-in OpenCV Canny edge detector algorithm.

#include 
#include 
#include 

int main(int argc, char *argv[]) {
    int delay = 0, key=0, i=0;
    char *window_name;
    CvCapture *video = NULL;
    IplImage  *frame = NULL;
    IplImage  *grey  = NULL;
    IplImage  *edges = NULL;

    /* check for video file passed by command line */
    if (argc>1) {
        video = cvCaptureFromFile(argv[1]);
    } else {
        printf("Usage: %s VIDEO_FILE\n", argv[0]);
        return 1;
    }

    /* check file was correctly opened */
    if (!video) {
        printf("Unable to open \"%s\"\n", argv[1]);
        return 1;
    }

    /* create a video window with same name of the video file, auto sized */
    window_name = argv[1];
    cvNamedWindow(window_name, CV_WINDOW_AUTOSIZE);

    /* Get the first frame and create a edges image with the same size */
    frame = cvQueryFrame(video);
    grey  = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);
    edges = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);

    /* calculate the delay between each frame and display video's FPS */
    printf("%2.2f FPS\n", cvGetCaptureProperty(video, CV_CAP_PROP_FPS));
    delay = (int) (1000/cvGetCaptureProperty(video, CV_CAP_PROP_FPS));

    while (frame) {
	/* Edges on the input gray image (needs to be grayscale) using the Canny algorithm.
           Uses two threshold and a aperture parameter for Sobel operator. */
        cvCvtColor(frame, grey, CV_BGR2GRAY);
        cvCanny( grey, edges, 1.0, 1.0, 3);

	/* show loaded frame */
        cvShowImage(window_name, edges);
	
	/* load and check next frame*/
        frame = cvQueryFrame(video);
	if(!frame) {
		printf("error loading frame.\n");
		return 1;
	}

	/* wait delay and check for the quit key */
        key = cvWaitKey(delay);
        if(key=='q') break;
    }
}

To compile it in a well configured OpenCV development environment:

gcc edgeplayer.c -o edgeplayer `pkg-config opencv –libs –cflags`

To run it call edgeplayer and the name of the video:

./edgeplayer rick.avi

The result is something similar to this:

rick roll edge

Simple Face Detection Player

Here’s a simple video player that also performs facial detection thought the Open Computer Vision Library.

Here’s a code developed using codes from nashruddin.com and samples from OpenCV, including the haar classifier xml. More detailed explanation on the theory about how the OpenCV face detection algorithm works can be found here.

The code:

#include 
#include 
#include 

CvHaarClassifierCascade *cascade;
CvMemStorage *storage;

int main(int argc, char *argv[]) {
    CvCapture *video = NULL;
    IplImage *frame = NULL;
    int delay = 0, key, i=0;
    char *window_name = "Video";
    char *cascadefile = "haarcascade_frontalface_alt.xml";

    /* check for video file passed by command line */
    if (argc>1) {
        video = cvCaptureFromFile(argv[1]);
    }
    else {
        printf("Usage: %s VIDEO_FILE\n", argv[0]);
        return 1;
    }

    /* check file was correctly opened */
    if (!video) {
        printf("Unable to open \"%s\"\n", argv[1]);
        return 1;
    }

    /* load the classifier */
    cascade = ( CvHaarClassifierCascade* )cvLoad( cascadefile, 0, 0, 0 );
    if(!cascade){
        printf("Error loading the classifier.");
	return 1;
    }

    /* setup the memory buffer for the face detector */
    storage = cvCreateMemStorage( 0 );
    if(!storage){
        printf("Error creating the memory storage.");
	return 1;
    }

    /* create a video window, auto size */
    cvNamedWindow(window_name, CV_WINDOW_AUTOSIZE);

    /* get a frame. Necessary for use the cvGetCaptureProperty */
    frame = cvQueryFrame(video);

    /* calculate the delay between each frame and display video's FPS */
    printf("%2.2f FPS\n", cvGetCaptureProperty(video, CV_CAP_PROP_FPS));
    delay = (int) (1000/cvGetCaptureProperty(video, CV_CAP_PROP_FPS));

    while (frame) {
	/* show loaded frame */
        cvShowImage(window_name, frame);
	
	/* wait delay and check for the quit key */
        key = cvWaitKey(delay);
        if(key=='q') break;
	/* load and check next frame*/
        frame = cvQueryFrame(video);
	if(!frame) {
		printf("error loading frame.\n");
		return 1;
	}

        /* detect faces */
        CvSeq *faces = cvHaarDetectObjects(
            frame, /* image to detect objects in */
            cascade, /* haar classifier cascade */
            storage, /* resultant sequence of the object candidate rectangles */
            1.1, /* increse window by 10% between the subsequent scans*/
            3, /* 3 neighbors makes up an object */
            0 /* flags CV_HAAR_DO_CANNY_PRUNNING */,
            cvSize( 40, 40 ) 
        );

        /* for each face found, draw a red box */
        for( i = 0 ; i < ( faces ? faces->total : 0 ) ; i++ ) {
             CvRect *r = ( CvRect* )cvGetSeqElem( faces, i );
             cvRectangle( frame,
                  cvPoint( r->x, r->y ),
                  cvPoint( r->x + r->width, r->y + r->height ),
                  CV_RGB( 255, 0, 0 ), 1, 8, 0 );
        }
    }
}

Yeah, I know the code needs a few adjustments. ¬¬

To compile it in a well configured OpenCV development environment:

gcc faceplayer.c -o faceplayer `pkg-config opencv ‑‑libs ‑‑cflags`

To run it you have to put in the same directory of the binary the XML classifier (haarcascade_frontalface_alt.xml) that comes with OpenCV sources at OpenCV-2.0.0/data/haarcascades/. And so:

./faceplayer video.avi

The results I got so far is that it works well for faces but sometimes its also detects more than faces. And here a video of it working live.

A example of good result:

rick roll face detection

A example of bad result:

rick roll face detection bad result

Maybe with some adjustments it could performs even better. But was really easy to create it using OpenCV.

Converting videos to n800

southpark running on Nokia n800

I found this useful tip about how to convert videos to watch on Nokia n800 using Mencoder.

mencoder input.ogg -vf scale=400:240 -oac mp3lame -ovc lavc -o output.avi

It’s converts a filed called input.ogg to a avi file output.avi with height 240 and width 400 (the device resolution is 800×480) , mp3lame audio codec and libavcodec video.

As this has become a daily operation to me, I create this simple script called 2n800:

#!/bin/sh
if [ $# -ge 1 ];
then
        mencoder $1 -vf scale=400:240 -oac mp3lame -ovc lavc -o ${1%\.*}.avi
else
        echo Usage:
        echo "\t$0 FILE"
fi

It transforms the first parameter like something.flv to something.avi. Putting this script as executable on your path like on /usr/bin/ you can easily call the command 2n800 followed with tha name of your video you want to convert. If is readable by Mplayer, it will be converted.

After you converted you video and sent to your n800, you can watch on Mplayer to Maemo. The result is perfect.

Java: Accessing Private Members

Using reflection to change the accessibility of a private object field and access it at runtime.

import java.lang.reflect.Field;

class Life {
    private int meaning = 42;
}

class Hack {
    public static void main(String args[]){
        Life life = new Life();
        try {
            Field field = life.getClass().getDeclaredField("meaning");
            field.setAccessible(true);
            System.out.println(field.get(life));
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e){
            e.printStackTrace();
        }
    }
}

Output:

42

Jedi Name

lightsaber

A simple Java class that calculates your Jedi name based on your first, last name, mother’s maiden name and the city you grew up.

/*
 Jedi first name: 1st 3 letters of last name + 1st 2 letters of first name
 Jedi last name: 1st 2 letters of mother's maiden name + 1st 3 letters of city you were raised in
 */

public class JediName {
   private static String capitalize(String s){
      return s.substring(0,1).toUpperCase() + s.substring(1,s.length()).toLowerCase();
   }

   private static String sub(String s, int begin, int end){
      return s.substring(begin, (s.length()>=end)?end:s.length());
   }

   public static String generate(String firstName, String lastName, String city, String mother ){
      String jediFirstName = capitalize(sub(lastName, 0, 3) + sub(firstName, 0,2));
      String jediLastName = capitalize(sub(mother,0,2)+ sub(city,0,3));
      return jediFirstName + " " + jediLastName;
   }
}

I used inline conditional expression ()?: to avoid raise a IndexOutOfBoundsException when the string is to small to cut the proper size we want.

And a example of how to use it:

public class JediNameTester {
	public static void main(String args[]){
		String myJediName = JediName.generate("Pedro", "Madeira", "Fortaleza", "Maria");
		System.out.println(myJediName);
	}
}

My jedi name is Siljo Iruba. 🙂

The Fork Bomb Tattoo

I thought about these two problems my entire life.

Graphical arts have a significant role in everything I do. I always expressed my feelings thorough graphical drawings. First on paper, so notebooks, then walls and others surfaces like the computer screen. But the possibility of transform my body, my own skin in a art canvas always fascinated me. This was a natural step.

But the tattoo is an art that raises a tricky philosophical question, it’s something that you can not undo it. Once you did it, it’s done. You can change or add the meaning with others tattoos but you can not remove meaning of a draw in a negative way by erasing it like other forms of arts. Of course there’s some kinds of treatments with different approaches and variant results but I see no point on think about tattoos looking on how remove them. It seems obvious that doesn’t matter how cool and incredible something could appear or how sure I am about it, I could always change my mind and repent. This puzzle was around my mind for a long time until finally I saw way out.

The key is that the life you live itself is also a tattoo. You say words that cannot be unsaid. You see things that you can not unseen.You feel such strong emotions that you can not heal your scars. Every moment it’s also a tattoo in the thin skin of the time and there’s nothing you can do about that because the time flows only in one direction. Of course you don’t paint all your life with ink in your own skin but at least, you can choose some parts of it to express as a beautiful tattoo.

The second problem is witch draw do. There are hundred of drawings that I love and besides I also enjoy old school tattoo drawings, I choose a piece of programming code. More specifically the Jaromil most elegant forkbomb code ever written:

forkbomb_small

This small piece of code fork itself twice in parallel creating more processes that will be forked again and again until stop the entire system. You can read a more detailed explanation on its operation, history and variations on Wikipedia’s Fork bomb article.

It has several meanings to me, from the aesthetics from the computer science field to the shape and possibilities the draw allow. The exponential grow of the number of forked process it’s also another beautiful aspect of this code. From the viewer perspective, even a casual one, it has a beautiful as a puzzle to be decipher or a totem pole of different emoticons.

Blood and ink.

I tried for months several different fonts and styles to use on it, from mono space to stylized draws. In the end I choose the Bitstream Vera Sans by Jim Lyles. A free (as in freedom) font very common on GNU/Linux systems. I also did small change in the draw by reducing almost entirely the spacing between the braces. This made it look better when looking it by side creating a stronger emoticon illusion.

Blood stain

Blood stain in the paper towel after dry the finished tattoo.

The process almost doesn’t involve pain and hurt, in contrast with what I thought. It’s like a little cat scratch in your arm for almost 2 hours of painting. I did in the Freedom of Tattoo studio, from the well-known here tattoo artist Dereka, with Thiago, a very skilled artist.

My thanks to the studio, to Dereka, to Thiago and to my friend Silvio who took the pictures and accompanied me. There’s a photo album with all tattoo photos. I’m very happy with the final work.