Pages

Tuesday 19 November 2013

Implementing Noisy TV in OpenCV...wait WHAT?!?

If you are a 90's kid like me, you must admit that back then there was nothing more annoying than losing signals on your TV. Before the age of internet, TV was the main source of entertainment. When signals were lost, all you could see was an infinite race of millions of flies on your screen (a noisy image) and making it more intolerable was the loud noisy sound of the noise.



Everything has now changed, TVs now are digital with compensation to make the experience less annoying and most of the users are now shifting to online streaming websites like netflix. There seems to be something missing?? Yes the irritating noisyness of your television screen. While it can not be same again, google has tried to bring back some memories when they replaced error screen on youtube with this noisy image.

While doing my research, I listen to a lot of songs on youtube.. and when I came across this noisy image, it struck me.. I wanted to recreate it in my own way. I thought I could recreate it using OpenCV and it turned out pretty cool in the end (Yes I created the above gif from the output of my code).

So without any further wait.. here is the code:

#include <cv.h>
#include <highgui.h>
#include <iostream>

using namespace std;
using namespace cv;

#define C_DIM_COLS 640
#define C_DIM_ROWS 480
#define C_STEP_LOOP 4
#define C_RANDOM_MOD 100

int main ()
{
    Mat outDisp;
    char ch = NULL;
    outDisp = Mat::zeros(C_DIM_ROWS, C_DIM_COLS, CV_8UC1);

    while(ch != 27)
    {
        for(int i = 0; i < outDisp.cols; i+=C_STEP_LOOP )
        {
            for(int j = 0; j < outDisp.rows; j+=C_STEP_LOOP)
            {
                int val = uchar(((rand()%C_RANDOM_MOD)*255)/C_RANDOM_MOD);
                for(int ii = i; ii < i + C_STEP_LOOP; ii++)
                {
                    for(int jj = j; jj < j + C_STEP_LOOP; jj++)
                    {
                        outDisp.at<uchar>(jj,ii) = val;
                    }
                }  
            }
        }
      
        imshow("outDisp Image", outDisp);
        ch = waitKey(10);
    }
    return 1;
}