Super-resolution algorithm in Python

Abstract

Super-resolution algorithms is a family of algorithms used to obtain higher resolution image from set of images in lower resolutions. It's important to notice that by using those methods the higher resolution image is more detailed in opposition to resizing and debluring.

It may be applied on many different data, including spying and meteorological satellites, telescopes, tomographs and X-ray machines. Super-resolution algorithms can be also applied to 3D sceneries or sounds.

In this article, I will describe a super resolution algorithm proposed by Irani and Peleg and its simple implementation in Python.

One of low resolution images (for easier comparison it's resized to fit the estimated image):

low resolution image, super resolution algorithm in Python

The image estimated by using super resolution algorithm:

image reconstructed by using super resolution algorithm in Python

Renewable source of electricity from an old step motor

Recently I saw in old electronic magazine reprint of a project, where step motor was used as a source of power for LED torch. It's an interesting idea of using renewable and simple source of energy. Today I will present my own construction. It can be used as a source of energy for devices like a wall clock or a single LED.

Step motor as a source of electricity

HOWTO avoid mistakes in conditional instructions

In C or C++ it's easy to made mistake in expression of conditional instruction and use assignment instead of comparison. It's presented in below snippet, where we want to check if x is equal to 6:

int main(){
    int x;

    /* first case */
    /* OK */
    if(x==6){
        /* sth */
    }
    /* WRONG */
    if(x=6){
        /* sth */
    }

    /* second case */
    /* OK */
    if(x!=6){
        /* sth */
    }
    /* WRONG */
    if(x=!6){
        /* sth */
    }

    return 0;
}

This code will be compiled successfully because its syntax is valid.

Those mistakes may be reduced by using following practice: while comparing something unmodifiable (string, number..) and variable, variable is written always on the right side. It's presented below:

int main(){
    int x;

    /* first case */
    /* OK */
    if(6==x){
        /* sth */
    }
    /* WRONG */
    if(6=x){
        /* sth */
    }

    /* second case */
    /* OK */
    if(6!=x){
        /* sth */
    }
    /* WRONG */
    if(6=!x){
        /* sth */
    }

    return 0;
}

Now conditional instructions with mistakes contain incorrect syntax and may be caught and fixed during compilation:

rgawron@foo:~$ gcc s2.c
s2.c: In function ‘main’:
s2.c:10: error: lvalue required as left operand of assignment
s2.c:20: error: lvalue required as left operand of assignment

Protecting images against saving

If you share on the Internet your images, then you may want to protect them against saving. There're a lot of ways to do it - watermarks, cutting images into multiple smaller pieces and reassembling them by using CSS, tricks with CSS or JavaScript and others. None of them is accurate against experienced user, but they are sufficient in most cases.

Today I will show different approach to protecting images, it's a bit impractical due to high CPU usage on browser side, but it works. The idea is to convert image into HTML table, where each pixel is one cell with corresponding to this pixel location and color.

Project contains three parts: Python script, HTML template and CSS file. The script requires as an argument location of the input image, as an output it generates output.html file. This file should be stored in the same directory as inline_image.css file. The sources are available on GitHub (image2table directory).

Making captcha easier to break

Almost all sites use images with text that user has to retype to prove that he's a human not spambots. A lot of those images (called captcha) contains small lines, dots and other noises to make theirs analyze more difficult for spambots. In this post I will present how to easily remove this noise from a captcha.

Breaking Captcha in Python Breaking Captcha in Python

I used Python and its PIL library for processing captchas. The first step is to transform image to grey-scale (this makes further work easier) and blur it (it makes small objects less visible). PIL's blur filter is a bit poor for that, but SMOTH filter works great (but it needs to be applied twice made it blurred enough). Next step is to check all pixels if their value is higher that some certain constant, if yes, then pixels will become white, if not they will be black. This constant may be set from command line.

Less popular way to copy content of a structure

There is instance (let's call it p0) of structure, we create new instance (p1) of the same structure and want to copy content of fields from p0 to p1.

The simplest (IMHO also the best) way is to copy field by field. It's presented in following code (in case of p1 all fields are copied, in case of p2 only some).

/* output: 
foo (3.800000, 4.900000): #121212
bar (3.800000, 4.900000): #335566
*/
#include <stdlib.h>

struct Point {
    char desc[5];
    double x, y;
    char color[8];
};

int main() {
    struct Point p0 = {.desc = "foo", .x = 3.8, .y = 4.9, .color = "#121212"};

    struct Point p1;
    snprintf(p1.desc, sizeof(p1.desc), p0.desc);
    p1.x = p0.x;
    p1.y = p0.y;
    snprintf(p1.color, sizeof(p1.color), p0.color);
    printf("%s (%f, %f): %s\n", p1.desc, p1.x, p1.y, p1.color);

    struct Point p2;
    snprintf(p2.desc, sizeof(p2.desc), "bar");
    p2.x = p0.x;
    p2.y = p0.y;
    snprintf(p2.color, sizeof(p2.color), "#335566");
    printf("%s (%f, %f): %s\n", p2.desc, p2.x, p2.y, p2.color);

    return EXIT_SUCCESS;
}

The second way is use memcpy to copy content of memory allocated for p0 directly into p1. It's presented in next snippet.