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);
}

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...