1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
int main() {
int filterSize = 128;
int orientNum = 4;
int scaleStart = 3; // edited
int scaleFinish = 4; // edited
int scaleStep = 1;
int scaleNum = 1;
int imageSegments = 4;
int width=320;
int height=240;
int imgSize;
int bytesRead;
int numImages = 0;
unsigned char *imgData;
list<list<unsigned char*> > *gabors;
IplImage *gabImg, *gBuffer, *cur, *gPrev, *gCur, *diff, *binaryImg, *rotatedImg, *flippedImage, *extractedPortion;
// Sridhar added <start>
unsigned int count = 1;
char filename[100];
unsigned char *stimArray;
unsigned char *tmpImg;
unsigned char *sendStimulus;
char *loadStimulus;
int i, j;
int sort;
int resolution;
int min, max;
int sectionSize = 25;
int stimCount;
int stimArraySize;
// Sridhar added <end>
V4LStreamer *cam = new V4LStreamer(IO_METHOD_MMAP, "/dev/video0", true, width, height, 1, 4, V4L2_PIX_FMT_YUYV, V4L2_FIELD_INTERLACED, V4L2_STD_NTSC_M);
GaborFilter *gab = new GaborFilter(filterSize, orientNum, scaleStart, scaleFinish, scaleStep, scaleNum, imageSegments);
cur = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
gBuffer = cvCreateImage(cvSize(height, width), IPL_DEPTH_8U, 1);
gPrev = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
gCur = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
diff = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
gabImg = cvCreateImage(cvSize(filterSize, filterSize), IPL_DEPTH_8U, 1);
// Sridhar added <start>
flippedImage = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
extractedPortion = cvCreateImage(cvSize(filterSize, filterSize), IPL_DEPTH_8U, 1);
binaryImg = cvCreateImage(cvSize(filterSize, filterSize), IPL_DEPTH_8U, 1);
rotatedImg = cvCreateImage(cvSize(filterSize, filterSize), IPL_DEPTH_8U, 1);
resolution = gabImg->imageSize;
stimCount = abs(resolution/sectionSize) + 1;
stimArraySize = stimCount * sectionSize;
stimArray = new unsigned char[resolution];
tmpImg = new unsigned char[resolution];
loadStimulus = new char[stimArraySize];
sendStimulus = new unsigned char[sectionSize];
// Sridhar added <end>
imgSize = gPrev->imageSize;
imgData = new unsigned char[imgSize];
cvNamedWindow( "OpenCV Capture", CV_WINDOW_AUTOSIZE );
cam->startCapture();
//Sridhar added <start>
for(i = 0; i < resolution; i++)
stimArray[i] = 0;
for(i = 0; i < sectionSize; i++)
sendStimulus[i] = 0;
for(i = 0; i < stimArraySize; i++)
loadStimulus[i] = 0;
//Sridhar added <end>
for ( ; ; ) {
cam->readFrame(cur->imageData, bytesRead);
cvCvtColor(cur, gCur, CV_BGR2GRAY);
//gAbsDiff(gPrev, gCur, diff, width, height, width); // Used for sequence of frames
memcpy(diff->imageData, gCur->imageData, imgSize);
gFlip90(diff, gBuffer, width, height, height, width);
memcpy(imgData, gBuffer->imageData, imgSize);
gabors = gab->filter(imgData, gBuffer->height, gBuffer->width);
memcpy(gabImg->imageData, gabors->front().front(), filterSize*filterSize);
cvShowImage( "OpenCV Capture", gabImg);
// Sridhar added <start>
// Saving current frame grabbed by the camera
sprintf(filename,"./images/current_image_%d.png",count);
// cvSaveImage(filename, cur);
// Flip original image horizontally
flipOriginalImage(gCur, flippedImage, width, height, count);
// Extract central portion of the flipped image
extractCentralPortion(flippedImage, extractedPortion, filterSize, filterSize, width, height, count);
// Saving gabor filter image
sprintf(filename,"./images/gabor_image_%d.png",count);
// cvSaveImage(filename, gabImg);
//processColorImage(cur);
memcpy(stimArray, gabImg->imageData, resolution); // Array that needs to be modified
memcpy(tmpImg, stimArray, resolution); // Array that is used to sort the data and get maximum and minimum values of the array
// Sorting data in ascending order
for(i=0; i<resolution; i++)
{
for(j=i+1; j<resolution; j++)
{
if(tmpImg[i] > tmpImg[j])
{
sort = tmpImg[i];
tmpImg[i] = tmpImg[j];
tmpImg[j] = sort;
}
}
}
min = tmpImg[0];
max = tmpImg[resolution-1];
// Applying scaling factor to generate a binary image
for(i=0; i<resolution; i++)
{
stimArray[i] = 255.0 * abs((stimArray[i]-min)/(max-min));
}
// Transferring data to IplImage (binaryImg)
memcpy(binaryImg->imageData, stimArray, resolution);
// Saving binary result of the gabor filter image
sprintf(filename,"./images/binary_image_%d.png",count);
// cvSaveImage(filename, binaryImg);
// Rotate the gabor image by an angle
// rotate(gabImg, rotatedImg, count);
// Transforming data into binary format to send to NCS
for(i = 0; i < resolution; i++)
{
if(stimArray[i] == 255.0)
{
stimArray[i] = 1;
}
}
string s = string (stimArray);
cout << s;
// Press 'Esc' key to exit
if( (cvWaitKey(10) & 255) == 27 ) break;
cvReleaseImage(&gPrev);
gPrev = cvCloneImage(gCur);
numImages++;
printf("numImages: %d\n", numImages);
// Eliminate this if structure for capturing sequence of images
if(count == 1)
{
break;
}
count++;
// Sridhar added <end>
delete gabors;
}
cam->stopCapture();
cvDestroyWindow( "mywindow" );
return 0;
}
|