Monday, November 04, 2013

Converting a Panoromic Image into a circular Image

rect2circ.c is a small piece of C code based on OpenCV to convert a panoramic image into a circular image. My idea was to convert a speech signal recorded for a long duration of time continuously

to be represented in a way the time during which it was recorded. In the below image it is between 10 and 5.
While this is simple, I did not find anything readily available and hence write this small piece of C code using OpenCV. The usage (when compiled) is
Usage: ./rect2circ -> an input rectangle image -> output circular image -> start time (0 .. 12) -> number of hours (0 .. 12) -> inner radius (0 .. 200)
The source code.
/*======
Convert a panoramic image into a circular image.
La updated by Sunil Kumar Kopparapu at 21/10/2013 (23:07)
===== */

#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "opencv/cv.h"
#include "opencv/cxcore.h"
#include "opencv/highgui.h"

#define FAIL -1
#define DISPLAY 0 
#define COMMENT 0 
#define my_min(x,y)(xy?x:y)
#define PI 4*atan(1.0)
#define a_RADIUS 100


int main(int argc, char** argv) {

IplImage *im = 0;
int height,width,step,channels;
uchar *data;
int i,j,k;
int out_height;
int c_x, c_y, X, Y, x, y;
int radius = -1, my_radius, a_radius;
double span = -1.0, start = -1.0;
double i_span = -1.0, i_start = -1.0;

IplImage *out = 0;

if(argc != 6){
  fprintf(stderr,"Usage: %s     \n", argv[0]);
  fprintf(stderr,"\t  -> an input rectangle image\n");
  fprintf(stderr,"\t  -> output circular image\n");
  fprintf(stderr,"\t  -> start time (0 .. 12)\n");
  fprintf(stderr,"\t  -> number of hours (0 .. 12)\n");
  fprintf(stderr,"\t  -> inner radius (0 .. 200)\n");
  return(FAIL);
}

im = cvLoadImage(argv[1], 1);

if(!im) {
  fprintf(stderr,"Unable to Read %s\n",argv[1]);
  return(FAIL);
}
else {
  height    = im->height;
  width     = im->width;
  step      = im->widthStep;
  channels  = im->nChannels;
  data      = (uchar *)im->imageData;
  fprintf(stderr,"Processing %s, %d x %d image with %d channels\n",argv[1], height, width, channels);

  if(DISPLAY) {
    cvShowImage("Original", im);
    cvWaitKey(0);
  }
}

/* Create the output image */
a_radius = atoi(argv[5]);

if(a_radius < 0) a_radius = a_RADIUS;
radius = (a_radius + im->height);
i_start = atof(argv[3]);
i_span = atof(argv[4]);

if(i_start < 0.0) {
 i_start = 0.0;
}
start = 2.0*(i_start/12.0)*PI;

if(i_span <0 .0="" adius:="" argv="" channels="" d="" f="" fprintf="" i_span="12.0;" if="" image="" ipl_depth_8u="" n="" out-="" out="cvCreateImage(cvSize(out_height," out_height="" radius="" reating="" s="" span="" start="" stderr="" with="" x="">height, out->width, out->nChannels);
fprintf(stderr,"Clock display starts at %.2f and displays for %.2f hours\n", i_start, i_span);

c_x = (int) (out->height/2);
c_y = (int) (out->width/2);

if(COMMENT) fprintf(stderr,"center: %d %d\n",c_x,c_y);


for (x=0; x< im->width; x++){
  for(y=0; y< my_max(1,im->height); y++){
      my_radius = radius - y;
      X = my_radius*sin((((float)x/(float)im->width)*span)+start);
      Y = my_radius*cos((((float)x/(float)im->width)*span)+start);
      X = c_x + X;
      Y = c_y - Y;
      for (k=0; k< my_max(1,channels); k++){
      out->imageData[Y*(out->widthStep) + X*channels + k] =im->imageData[y*(im->widthStep) + x*channels + k];
    }
  }
}


cvSaveImage(argv[2], out, 0);

if(DISPLAY) {
  cvShowImage("Output", out);
  cvWaitKey(0);
}

return(1);
}

Sunday, September 15, 2013

Math Problem: When is Sn = n^2+20n+12 a perfect square?

This is a question that appeared in RMO in 2012 I guess.

Let 
  1. Sn = n^2+20n+12,  
  2. n is a positive integer. 
  3. What is the sum of all possible values of n for which Sn is a perfect square.
 Solution:
 

The solution was suggested by one of my colleagues Ashish Panda.

Wednesday, July 17, 2013

Some interesting -OLD- posts

Some interesting posts (penned by me!) from wordpress ;-)

Thursday, July 11, 2013

Remembering -My Umbrella-

May 2006

Baba. Mamoni says that I can not play outside because it is drizzling. Can we play inside? says Sumit my 4+ year son. It is 630 am on the penultimate day of May 2006. I seldom make a note of when the monsoon starts in Mumbai because I depend on MRF advertisement to tell about the MRF rainday! I don’t know if the newspaper still carries an advertisement about the possible rainday because of late I have switched loyalties to another new(s)paper who were prepared to supply newspaper for one full year for the price of 1 month subscription of my earlier paper. As I listen to Sumit my mind goes back in time down the memory lane trying to sense my own reaction to Mumbai monsoon.

Mumbai monsoon can be very unwelcome especially if one is staying in Tiruchirapalli, a small sleepy town in south India, which hardly saw any rains in my three years stay there was substituted by the monsoon in Mumbai. The feeling can be overwhelming if this change happens in a span of 24 hours after a long unreserved rail journey from Tiruchirapalli to Mumbai.

That was fifteen long years ago. One Mumbai (then Bombay) monsoon day I railed into Bombay to pursue my PhD in IIT Bombay. Unaware of the need for an umbrella, was pretty much written on my drenched clothes! The first thing that I was told to do was to get an umbrella immediately, which my seniors assured me, would repeat every year once or twice depending on how far away you are to your PhD degree. With folded pants and a half sharing umbrella I went to make one of my priced possessions. Price was the detrimental factor in selection of an umbrella because as a research scholar 15 years ago you lived on tight budget - trying not to get any resources from your parents who have already seen the eccentric side of you. Why is my boy not like others? Why can not he get a job in the US and earn in dollars, get married, settle down in life.

Year after year the umbrella went into safe keeping and came back into open on the MRF rainday. As years passed by, I believed that I had a priced possession in my umbrella. I built a quiver with my favorite torn jeans and carried the umbrella like an arrow. True to their words, everyone seem to loose their umbrella every season at least once, while I seem to have a special bonding with my umbrella. I rejoiced it and believed that I could take the bond further. The bonding continued, I defended my thesis in monsoon and still the umbrella was with me. It was time to do a post-doc down under, the umbrella automatically came along - probably a Vintage piece to an Australian it stayed and served me in Brisbane. It was time to return to Mumbai to settle down and raise a family and my umbrella came along safe and sound in the hand baggage.

Namita was not too happy with my umbrella. More so because probably I never lost a chance to mention about its age when ever it made sense or not. She like any good spouse purchased an excellent umbrella, the highest on the price list available in the market and gifted to me. Though not sure I needed one, I accepted it as a gift. Now I had two umbrellas, my own favorite and the new one competing for my attention. Not to cause any unpleasant feelings to Namita or me, both the umbrellas stayed with me in my car, just in case there was a need.

It is 26/7/2005. Namita calls me and tells that I need to get back home because Mumbai is flooding. I start leisurely from work. I am stuck on the flyover about 4 km from home in my car and it is pouring with no sign of ending. I hear whispers, if this continues it might take more than 24 hours for the water to drain. My mobile doesn’t work and I make up my mind to reach home to be with Sumit and Namita, I might have to wade through waist deep water for a couple of kilometers – doable. I start out of the car, lock it, when a stranger is shivering outside his car and doesn’t have an umbrella asks me if I have a spare umbrella. I have one new and my own favorite, I debate for full 2 minutes and finally hand over my not new umbrella to him. He is thankful and promises to return. We go our way.

I knew he meant it but I was also sure it was very unlikely that we would meet again. Almost a year later I fondly hope that I will meet this gentleman and get my umbrella back soon. I feel the loss but am happy that it even served on the most publicised monsoon day in Mumbai. Well done umbrella. Hats off to your endurance. I miss you very much.

Friday, June 21, 2013

Driving in Mumbai

Driving in Mumbai can be crazy especially if you are a new driver or if you have only driven on rules following drivers. However there are stretches in Mumbai depending on the time of the day and the direction of the ride that allows you to drive quite comfortably. So if you are new driver or a driver new to Mumbai roads, do not panic. It is quite alright to drive in Mumbai. I thought some real and actual drives might be useful for someone who might be wondering what is it to
  • drive in Mumbai
  • drive on Eastern Express (Freeway) Highway
  • drive in Monsoon
  1. Eastern Express Highway Driving on the Mumbai Eastern Express (Video) Highway (EEH) is not so bad if you are driving against the traffic! What started off as an intent to capture the long jam along the EEH turned out to be a nice view of the EEH.
    • Happy Video Eastern Express Ride ;-)
    • Weekdays mornings from Chembur to Thane and Weekdays evenings from Thane to Chembur
    • While the recommended max speed is 60 kmph; unless you drive at 80 kmph you will get a lot of stare from all those who overtake you (including folks on motor bikes!)
  2. Eastern Freeway This is an amazing road patch which connects Chembur to CST. While there are many better videos that capture the route. I captured when I drove back from CST to Chembur on a Sunday evening. While the speed limit is 60 kmph but most of them drive 80-100 kmph; but on a Sunday you can drive your pace without any honks!
    • Happier Video Eastern Freeway Ride
    • Anytime; best Sunday.
    • To watch: Beautiful Tunnel almost 500m
    • You will see people walking in the tunnel (yeah. it is a freeway!).

Some Useful Stuff that I use (Linux/Video)

These are some of the command like stuff that I use regularly. I thought I will put all of this in one place; though you will find a lot more detail if you GoBiYa!
  • Playing a VCD
    • $ mplayer vcd://1
    • $ mplayer-cdrom-device /dev/cdrom vcd://1
  • Playing a DVD
    • $ mplayer dvd://[title [start_title]-end_title] [options]
  • Ripping a DVD
    • $ mplayer -dumpstream dvd://2 -dumpfile ripped_output.avi
  • Creating an MPEG-4 file from JPG
    • all the JPEG files in the current directory
      • $ mencoder mf://*.jpg -mf w=800:h=600:fps=1:type=jpg -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell -oac copy -o output.avi
    • list of JPEG files (list.txt in current directory contains the list of files to use as source, one per line)
      • $ mencoder mf://@list.txt -mf w=800:h=600:fps=25:type=jpg -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell -oac copy -o output.avi
  • Combining two videos
    • concatenate 2 avi files
      • $ cat 1.avi 2.avi > 12.avi 
    • combing audio and video files
      • $ mencoder -oac copy -ovc copy -o output_movie.avi -audiofile input_audio.mp2 input_video.avi
  • Extracting audio from an avi file
    • $ mplayer -ao pcm:file=output_audio.wav input.avi
  • Rotating video by 90 degree
    • $ mencoder -vf rotate=1 -o out_rotated_90.mov -oac pcm -ovc lavc in_original.mov
  • Something that I had used for ;-)
    $ mencoder input.mpg -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=1100:vhq:vqmin=2:vlelim=-4:vcelim=9:lumi_mask=0.05:dark_mask=0.01 -vf pp=md scale=640:480, -oac lavc -o output.avi

Saturday, March 02, 2013

SMC2F: Helping Socalize


I got a call from one of my friends asking me if I had called. Shamelessly I admitted that I had not called because that was the truth; but the fact was that I had always wanted to call him but finding a non-busy slot which is an intersection of not-busy slot of my friend and the not busy-slot of me was null for a good long time. Nevertheless to say we never called each other for a long time. Nonetheless we eventually ended up talking for a quite a few minutes.

I guess this is a very common scenario and most of us can relate to this in one form or other. We want to socialize but do not know when or how. When the number of days from the lasttime we called increases, the resistance to make the connect increases. So the connect is not made for a long time and sometimes it is a life time.

Can technology help? Mobile phones are omni present. They have our contact telephone number and they have a good memory. Why not mix all this up and enable Smart Missed Call to Connect two Friends (SMC2F) application?

SMC2F is a program running on your phone which keeps a watch on the call and the message log of your phone and is aware of friends in your contact list. Simple analysis determines which friend in the contact list has not been called for a while now and the smart applications calls the friends number and disconnects before the call is picked by the friend and goes back to sleep. Allowing the two friends to make the voice connect.

Is there such an ap? Go figure out.

A Lesson Hidden in Two Simple Words

Sometimes profound ideas are hidden in the most ordinary places. Recently, while looking at two common English words, I stumbled upon an int...