Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Seeking help with converting opencv webcam output to pixel coordinates for integrating with sensory substitution program

strong textHi all,

I am trying to create a basic version of the vOICe (http://www.seeingwithsound.com) to run on Linux and devices similar to the raspberry pi. This involves writing C code. The last time I did anything in C was 1994 or perhaps 1996. Any how, I have most of the stuff worked out. There is basic soundscape generation code at http://www.seeingwithsound.com. That code works on Linux.

I am using the opencv library to capture images from a webcam on Linux. I am unable to integrate opencv's output with the code of the vOICe. The vOICe expects pixel coordinates as numbers. I need to replace the hard coded image coordinates with the output from a webcam. I am not sure how openccv gives said coordinates. Yes, I did search and found the answer but I cannot understand the data type conversions involved. The answer is at the opencv faq. http://opencv.willowgarage.com/wiki/faq#Howtoaccessimagepixels

I am pasting the opencv code and then the code of the vOICe. Note: If extracting the code from this message becomes a problem, I have the 2 files in dropbox. Openn cv code: https://dl.dropboxusercontent.com/u/3688386/ot.c the link to the code for the vOICe https://dl.dropboxusercontent.com/u/3688386/i2s.c

//text of opencv code

include <stdio.h>

include <stdlib.h>

include "stdafx.h"

include <math.h>

include <cv.h>

include <highgui.h>

void main(int argc,char argv[]) { int c; IplImage color_img; CvCapture* cv_cap = cvCaptureFromCAM(0); color_img = cvQueryFrame(cv_cap); // get frame

int nl= color_img->height;
int nc= color_img->width * color_img->nChannels;

unsigned char *data= reinterpret_cast<unsigned char *>(color_img->imageData);

cvNamedWindow("Video",0); // create window for(;;) { if(color_img != 0) cvShowImage("Video", color_img); // show frame c = cvWaitKey(10); // wait 10 ms or for key stroke if(c == 27) break; // if ESC, break and quit } /* clean up */ cvReleaseCapture( &cv_cap ); cvDestroyWindow("Video"); }

//The code of the vOICe /* Simple C program for .wav soundscape generation. (C) P.B.L. Meijer 1996 */

define FNAME "arti1.wav"

define N 64 /* Resolution, i.e., # rows and columns */

define FL 500 /* Lowest frequency (Hz) in visual sound */

define FH 5000 /* Highest frequency (Hz) */

define FS 20000 /* Sample frequency (Hz) */

define T 1.05 /* Image to sound conversion time (s) */

define D 1 /* Linear|Exponential=0|1 distribution */

include <stdio.h>

include <stdlib.h>

include <math.h>

define C_ALLOC(number, type) ((type *) calloc((number),sizeof(type)) )

define TwoPi 6.283185307179586476925287

define WHITE 1.00

define DGREY 0.25

define BLACK 0.00

FILE fp; unsigned long ir=0L, ia=9301L, ic=49297L, im=233280L; void wi(unsigned int i) { int b1,b0; b0=i%256; b1=(i-b0)/256; putc(b0,fp); putc(b1,fp); } void wl(long l) { unsigned int i1,i0; i0=l%65536L; i1=(l-i0)/65536L; wi(i0); wi(i1); } double rnd(void){ ir = (iria+ic) % im; return ir / (1.0*im); }

int main(int argc, char argv[]) { int i, j, b, d = D; long k = 0L, ns = (long) (FS * T), m = ns / N; double *A, t, dt = 1.0 / FS, w, *phi0, s, y, yp, z, tau1, tau2, scale = 0.5/sqrt(N); w = C_ALLOC(N, double); phi0 = C_ALLOC(N, double); A = C_ALLOC(N, double *); for (i=0; i<n; i++)="" a[i]="C_ALLOC(N," double);="" <="" em=""> N x N pixel matrix */

/* Set hard-coded image: diagonal, filled rectangle, 3 horizontal lines */ for (j= 0; j< N; j++) for (i=0; i< N; i++) if (i==j) A[i][j] = WHITE; else A[i][j] = BLACK; for (j=39; j<60; j++) for (i=9; i<27; i++) if (j<47) A[i][j] = DGREY; else A[i][j] = WHITE; for (j= 4; j< 8; j++) A[40][j] = WHITE; for (j= 8; j<12; j++) A[31][j] = WHITE; for (j=12; j<16; j++) A[40][j] = WHITE;

/* Set lin|exp (0|1) frequency distribution and random initial phase / if (d) for (i=0; i<n; i++)="" w[i]="TwoPi" *="" fl="" *="" pow(1.0<="" em=""> FH/FL,1.0*i/(N-1)); else for (i=0; i<n; i++)="" w[i]="TwoPi" *="" fl="" +="" twopi="" *="" (fh-fl)="" *i="" (n-1)="" ;="" for="" (i="0;" i<n;="" i++)="" phi0[i]="TwoPi" *="" rnd();<="" p="">

/* Write 8-bit .wav visual sound file, using rectangular time window / fp = fopen(FNAME,"wb"); fprintf(fp,"RIFF"); wl(ns+36L); fprintf(fp,"WAVEfmt "); wl(16L); wi(1); wi(1); wl(0L+FS); wl(0L+FS); wi(1); wi(8); fprintf(fp,"data"); wl(ns); tau1 = 0.5 / w[N-1]; tau2 = 0.25 * tau1tau1; y = z = 0.0; while (k < ns) { /* Not optimized for speed (or anything else) */ s = 0.0; t = k * dt; j = k / m; if (j>N-1) j=N-1; for (i=0; i<n; i++)="" s="" +="A[i][j]" *="" sin(w[i]="" *="" t="" +="" phi0[i]);="" if="" (k="" &lt;="" ns="" (5*n))="" s="(2.0*rnd()-1.0)" scale;="" *="" "click"="" *="" yp="y;" y="tau1/dt" +="" tau2="" (dt*dt);="" y="(s" +="" y="" *="" yp="" +="" tau2="" dt="" *="" z)="" (1.0="" +="" y);="" z="(y" -="" yp)="" dt;="" b="128" +="" (int)="" (scale="" *="" 128="" *="" y);="" *="" y="2nd" order="" filtered="" s="" *="" if="" (b="" &gt;="" 255)="" b="255;" if="" (b="" &lt;="" 0)="" b="0;" putc(b,fp);="" k++;="" }="" if="" ((ceil(ns="" 2))!="(ns/2))" putc(0,fp);="" fclose(fp);="" return(0);="" }<="" p="">

Pranav

click to hide/show revision 2
No.2 Revision

updated 2013-05-27 01:58:35 -0500

berak gravatar image

Seeking help with converting opencv webcam output to pixel coordinates for integrating with sensory substitution program

strong textHi all,

I am trying to create a basic version of the vOICe (http://www.seeingwithsound.com) to run on Linux and devices similar to the raspberry pi. This involves writing C code. The last time I did anything in C was 1994 or perhaps 1996. Any how, I have most of the stuff worked out. There is basic soundscape generation code at http://www.seeingwithsound.com. That code works on Linux.

I am using the opencv library to capture images from a webcam on Linux. I am unable to integrate opencv's output with the code of the vOICe. The vOICe expects pixel coordinates as numbers. I need to replace the hard coded image coordinates with the output from a webcam. I am not sure how openccv gives said coordinates. Yes, I did search and found the answer but I cannot understand the data type conversions involved. The answer is at the opencv faq. http://opencv.willowgarage.com/wiki/faq#Howtoaccessimagepixels

I am pasting the opencv code and then the code of the vOICe. Note: If extracting the code from this message becomes a problem, I have the 2 files in dropbox. Openn cv code: https://dl.dropboxusercontent.com/u/3688386/ot.c the link to the code for the vOICe https://dl.dropboxusercontent.com/u/3688386/i2s.c

//text of opencv code

include <stdio.h>

include <stdlib.h>

include "stdafx.h"

include <math.h>

include <cv.h>

include <highgui.h>

code #include <stdio.h> #include <stdlib.h> #include "stdafx.h" #include <math.h> #include <cv.h> #include <highgui.h> void main(int argc,char argv[]) *argv[]) { int c; IplImage IplImage* color_img; CvCapture* cv_cap = cvCaptureFromCAM(0); color_img = cvQueryFrame(cv_cap); // get frame

frame

    int nl= color_img->height;
 int nc= color_img->width * color_img->nChannels;

 unsigned char *data= reinterpret_cast<unsigned char *>(color_img->imageData);

cvNamedWindow("Video",0); // create window for(;;) { if(color_img != 0) cvShowImage("Video", color_img); // show frame c = cvWaitKey(10); // wait 10 ms or for key stroke if(c == 27) break; // if ESC, break and quit } /* clean up */ cvReleaseCapture( &cv_cap ); cvDestroyWindow("Video"); }

} //The code of the vOICe /* Simple C program for .wav soundscape generation. (C) P.B.L. Meijer 1996 */

define */ #define FNAME "arti1.wav"

define "arti1.wav" #define N 64 /* Resolution, i.e., # rows and columns */

define */ #define FL 500 /* Lowest frequency (Hz) in visual sound */

define */ #define FH 5000 /* Highest frequency (Hz) */

define */ #define FS 20000 /* Sample frequency (Hz) */

define */ #define T 1.05 /* Image to sound conversion time (s) */

define */ #define D 1 /* Linear|Exponential=0|1 distribution */

include <stdio.h>

include <stdlib.h>

include <math.h>

define */ #include <stdio.h> #include <stdlib.h> #include <math.h> #define C_ALLOC(number, type) ((type *) calloc((number),sizeof(type)) )

define ) #define TwoPi 6.283185307179586476925287

define 6.283185307179586476925287 #define WHITE 1.00

define 1.00 #define DGREY 0.25

define 0.25 #define BLACK 0.00

0.00 FILE fp; *fp; unsigned long ir=0L, ia=9301L, ic=49297L, im=233280L; void wi(unsigned int i) { int b1,b0; b0=i%256; b1=(i-b0)/256; putc(b0,fp); putc(b1,fp); } void wl(long l) { unsigned int i1,i0; i0=l%65536L; i1=(l-i0)/65536L; wi(i0); wi(i1); } double rnd(void){ ir = (iria+ic) (ir*ia+ic) % im; return ir / (1.0*im); }

} int main(int argc, char argv[]) *argv[]) { int i, j, b, d = D; long k = 0L, ns = (long) (FS * T), m = ns / N; double *A, **A, t, dt = 1.0 / FS, w, *w, *phi0, s, y, yp, z, tau1, tau2, scale = 0.5/sqrt(N); w = C_ALLOC(N, double); phi0 = C_ALLOC(N, double); A = C_ALLOC(N, double *); for (i=0; i<n; i++)="" a[i]="C_ALLOC(N," double);="" <="" em=""> i<N; i++) A[i] = C_ALLOC(N, double); /* N x N pixel matrix */

*/ /* Set hard-coded image: diagonal, filled rectangle, 3 horizontal lines */ for (j= 0; j< N; j++) for (i=0; i< N; i++) if (i==j) A[i][j] = WHITE; else A[i][j] = BLACK; for (j=39; j<60; j++) for (i=9; i<27; i++) if (j<47) A[i][j] = DGREY; else A[i][j] = WHITE; for (j= 4; j< 8; j++) A[40][j] = WHITE; for (j= 8; j<12; j++) A[31][j] = WHITE; for (j=12; j<16; j++) A[40][j] = WHITE;

WHITE; /* Set lin|exp (0|1) frequency distribution and random initial phase / */ if (d) for (i=0; i<n; i++)="" w[i]="TwoPi" *="" fl="" *="" pow(1.0<="" em=""> i<N; i++) w[i] = TwoPi * FL * pow(1.0* FH/FL,1.0*i/(N-1)); else for (i=0; i<n; i++)="" w[i]="TwoPi" *="" fl="" +="" twopi="" *="" (fh-fl)="" *i="" (n-1)="" ;="" for="" (i="0;" i<n;="" i++)="" phi0[i]="TwoPi" *="" rnd();<="" p="">

i<N; i++) w[i] = TwoPi * FL + TwoPi * (FH-FL) *i/(N-1) ; for (i=0; i<N; i++) phi0[i] = TwoPi * rnd(); /* Write 8-bit .wav visual sound file, using rectangular time window / */ fp = fopen(FNAME,"wb"); fprintf(fp,"RIFF"); wl(ns+36L); fprintf(fp,"WAVEfmt "); wl(16L); wi(1); wi(1); wl(0L+FS); wl(0L+FS); wi(1); wi(8); fprintf(fp,"data"); wl(ns); tau1 = 0.5 / w[N-1]; tau2 = 0.25 * tau1tau1; tau1*tau1; y = z = 0.0; while (k < ns) { /* Not optimized for speed (or anything else) */ s = 0.0; t = k * dt; j = k / m; if (j>N-1) j=N-1; for (i=0; i<n; i++)="" s="" +="A[i][j]" *="" sin(w[i]="" *="" t="" +="" phi0[i]);="" if="" (k="" &lt;="" ns="" (5*n))="" s="(2.0*rnd()-1.0)" scale;="" *="" "click"="" *="" yp="y;" y="tau1/dt" +="" tau2="" (dt*dt);="" y="(s" +="" y="" *="" yp="" +="" tau2="" dt="" *="" z)="" (1.0="" +="" y);="" z="(y" -="" yp)="" dt;="" b="128" +="" (int)="" (scale="" *="" 128="" *="" y);="" *="" y="2nd" order="" filtered="" s="" *="" if="" (b="" &gt;="" 255)="" b="255;" if="" (b="" &lt;="" 0)="" b="0;" putc(b,fp);="" k++;="" }="" if="" ((ceil(ns="" 2))!="(ns/2))" putc(0,fp);="" fclose(fp);="" return(0);="" }<="" p=""> i<N; i++) s += A[i][j] * sin(w[i] * t + phi0[i]); if (k < ns/(5*N)) s = (2.0*rnd()-1.0) / scale; /* "click" */ yp = y; y = tau1/dt + tau2/(dt*dt); y = (s + y * yp + tau2/dt * z) / (1.0 + y); z = (y - yp) / dt; b = 128 + (int) (scale * 128 * y); /* y = 2nd order filtered s */ if (b > 255) b = 255; if (b < 0) b = 0; putc(b,fp); k++; } if ((ceil(ns/2))!=(ns/2)) putc(0,fp); fclose(fp); return(0); }

Pranav

Pranav

Seeking help with converting opencv webcam output to pixel coordinates for integrating with sensory substitution program

strong textHi all,

I am trying to create a basic version of the vOICe (http://www.seeingwithsound.com) to run on Linux and devices similar to the raspberry pi. This involves writing C code. The last time I did anything in C was 1994 or perhaps 1996. Any how, I have most of the stuff worked out. There is basic soundscape generation code at http://www.seeingwithsound.com. That code works on Linux.

I am using the opencv library to capture images from a webcam on Linux. I am unable to integrate opencv's output with the code of the vOICe. The vOICe expects pixel coordinates as numbers. I need to replace the hard coded image coordinates with the output from a webcam. I am not sure how openccv gives said coordinates. Yes, I did search and found the answer but I cannot understand the data type conversions involved. The answer is at the opencv faq. http://opencv.willowgarage.com/wiki/faq#Howtoaccessimagepixels

I am pasting the opencv code and then the code of the vOICe. Note: If extracting the code from this message becomes a problem, I have the 2 files in dropbox. Openn cv code: https://dl.dropboxusercontent.com/u/3688386/ot.c the link to the code for the vOICe https://dl.dropboxusercontent.com/u/3688386/i2s.c

//text of opencv code


#include <stdio.h> #include <stdlib.h> #include "stdafx.h" #include <math.h> #include <cv.h> #include <highgui.h> <highgui.h>

void main(int argc,char *argv[])
{
int c;
IplImage* color_img;
CvCapture* cv_cap = cvCaptureFromCAM(0);
color_img = cvQueryFrame(cv_cap); // get frame

    int nl= color_img->height;
    int nc= color_img->width * color_img->nChannels;

    unsigned char *data= reinterpret_cast<unsigned char *>(color_img->imageData);



cvNamedWindow("Video",0); // create window
for(;;) {
if(color_img != 0)
cvShowImage("Video", color_img); // show frame
c = cvWaitKey(10); // wait 10 ms or for key stroke
if(c == 27)
break; // if ESC, break and quit
}
/* clean up */
cvReleaseCapture( &cv_cap );
cvDestroyWindow("Video");
}
 </code>

//The code of the vOICe
/* Simple C program for .wav soundscape generation. (C) P.B.L. Meijer 1996 */
 <code>
#define FNAME "arti1.wav"
#define N     64   /* Resolution, i.e., # rows and columns   */
#define FL   500   /* Lowest  frequency (Hz) in visual sound */
#define FH  5000   /* Highest frequency (Hz)                 */
#define FS 20000   /* Sample  frequency (Hz)                 */
#define T   1.05   /* Image to sound conversion time (s)     */
#define D      1   /* Linear|Exponential=0|1 distribution    */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define C_ALLOC(number, type) ((type *) calloc((number),sizeof(type)) )
#define TwoPi 6.283185307179586476925287
#define WHITE 1.00
#define DGREY 0.25
#define BLACK 0.00

FILE *fp; unsigned long ir=0L, ia=9301L, ic=49297L, im=233280L;
void wi(unsigned int i) { int b1,b0; b0=i%256; b1=(i-b0)/256; putc(b0,fp); putc(b1,fp); }
void wl(long l) { unsigned int i1,i0; i0=l%65536L; i1=(l-i0)/65536L; wi(i0); wi(i1); }
double rnd(void){ ir = (ir*ia+ic) % im; return ir / (1.0*im); }

int main(int argc, char *argv[])

{ int i, j, b, d = D; long k = 0L, ns = (long) (FS * T), m = ns / N; double **A, *A, t, dt = 1.0 / FS, *w, *phi0, s, y, yp, z, tau1, tau2, scale = 0.5/sqrt(N); w = C_ALLOC(N, double); phi0 = C_ALLOC(N, double); A = C_ALLOC(N, double *); for (i=0; i<N; i++) A[i] = C_ALLOC(N, double); /* i<n; i++)="" a[i]="C_ALLOC(N," double);="" <="" em=""> N x N pixel matrix */ */

/* Set hard-coded image: diagonal, filled rectangle, 3 horizontal lines */ for (j= 0; j< N; j++) for (i=0; i< N; i++) if (i==j) A[i][j] = WHITE; else A[i][j] = BLACK; for (j=39; j<60; j++) for (i=9; i<27; i++) if (j<47) A[i][j] = DGREY; else A[i][j] = WHITE; for (j= 4; j< 8; j++) A[40][j] = WHITE; for (j= 8; j<12; j++) A[31][j] = WHITE; for (j=12; j<16; j++) A[40][j] = WHITE; WHITE;

/* Set lin|exp (0|1) frequency distribution and random initial phase */ / if (d) for (i=0; i<N; i++) w[i] = TwoPi * FL * pow(1.0* i<n; i++)="" w[i]="TwoPi" *="" fl="" *="" pow(1.0<="" em=""> FH/FL,1.0*i/(N-1)); else for (i=0; i<N; i++) w[i] = TwoPi * FL + TwoPi * (FH-FL) *i/(N-1) ; for (i=0; i<N; i++) phi0[i] = TwoPi * rnd(); i<n; i++)="" w[i]="TwoPi" *="" fl="" +="" twopi="" *="" (fh-fl)="" *i="" (n-1)="" ;="" for="" (i="0;" i<n;="" i++)="" phi0[i]="TwoPi" *="" rnd();<="" p="">

/* Write 8-bit .wav visual sound file, using rectangular time window */ / fp = fopen(FNAME,"wb"); fprintf(fp,"RIFF"); wl(ns+36L); fprintf(fp,"WAVEfmt "); wl(16L); wi(1); wi(1); wl(0L+FS); wl(0L+FS); wi(1); wi(8); fprintf(fp,"data"); wl(ns); tau1 = 0.5 / w[N-1]; tau2 = 0.25 * tau1*tau1; tau1tau1; y = z = 0.0; while (k < ns) { /* Not optimized for speed (or anything else) */ s = 0.0; t = k * dt; j = k / m; if (j>N-1) j=N-1; for (i=0; i<N; i++) s += A[i][j] * sin(w[i] * t + phi0[i]); if (k < ns/(5*N)) s = (2.0*rnd()-1.0) / scale; /* "click" */ yp = y; y = tau1/dt + tau2/(dt*dt); y = (s + y * yp + tau2/dt * z) / (1.0 + y); z = (y - yp) / dt; b = 128 + (int) (scale * 128 * y); /* y = 2nd order filtered s */ if (b > 255) b = 255; if (b < 0) b = 0; putc(b,fp); k++; } if ((ceil(ns/2))!=(ns/2)) putc(0,fp); fclose(fp); return(0); } i<n; i++)="" s="" +="A[i][j]" *="" sin(w[i]="" *="" t="" +="" phi0[i]);="" if="" (k="" &lt;="" ns="" (5*n))="" s="(2.0*rnd()-1.0)" scale;="" *="" "click"="" *="" yp="y;" y="tau1/dt" +="" tau2="" (dt*dt);="" y="(s" +="" y="" *="" yp="" +="" tau2="" dt="" *="" z)="" (1.0="" +="" y);="" z="(y" -="" yp)="" dt;="" b="128" +="" (int)="" (scale="" *="" 128="" *="" y);="" *="" y="2nd" order="" filtered="" s="" *="" if="" (b="" &gt;="" 255)="" b="255;" if="" (b="" &lt;="" 0)="" b="0;" putc(b,fp);="" k++;="" }="" if="" ((ceil(ns="" 2))!="(ns/2))" putc(0,fp);="" fclose(fp);="" return(0);="" }="" <="" code=""> Pranav

Pranav