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
|
#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/video/background_segm.hpp"
#include "opencv2/features2d/features2d.hpp"
#include <stdio.h>
#include <stdlib.h>
#include "cv.h"
#include "cvaux.h"
#include "cxcore.h"
#include "GraphUtils.h"
#include <math.h>
#include <vector>
#include <iostream>
#include <fstream>
#include <cstdlib> // for exit function
#include <cstring>
#include "covF.h"
#include "opencv2/core/core.hpp"
#include "opencv2/ml/ml.hpp"
using std::cerr;
using std::endl;
using namespace cv;
using namespace std;
using std::ofstream;
IplImage** Feature(IplImage* in,IplImage Gab0,IplImage Gab85,IplImage Gab180,IplImage Gab265 )
{
CvSize sz = cvGetSize( in );
IplImage **covarInterim = new IplImage*[12];
//IplImage* imgX = cvCreateImage( sz, in->depth,1 );
//IplImage* imgY = cvCreateImage( sz, in->depth,1 );
IplImage* xsobel = cvCreateImage(sz, IPL_DEPTH_16S,1);
IplImage* ysobel = cvCreateImage(sz, IPL_DEPTH_16S,1);
IplImage* b = cvCreateImage( sz, in->depth,1 );
IplImage* r = cvCreateImage(sz, in->depth,1 );
IplImage* g = cvCreateImage( sz, in->depth,1 );
IplImage* imgX = cvCreateImage( sz, in->depth,1 );
IplImage* imgY = cvCreateImage( sz, in->depth,1 );
//Gradient orientation
float temp_gradient;
const float PI = 3.14159265f;
//IplImage* O = cvCreateImage( sz, IPL_DEPTH_32F,1 );
IplImage* O = cvCreateImage( sz, 8,1 );
cvSetZero(O);
cvSplit(in,b,g,r,0); //R,G,B channel image
IplImage* img_gray = cvCreateImage(cvGetSize(in), IPL_DEPTH_8U,1);
cvCvtColor(in,img_gray, CV_RGB2GRAY);
cvSobel( img_gray, xsobel, 1,0, 3 ); //gradient in X direction
cvSobel( img_gray, ysobel, 0, 1, 3 ); //gradient in Y direction
IplImage* xsobel_dest = cvCreateImage(sz,in->depth,1);
IplImage* ysobel_dest = cvCreateImage(sz, in->depth,1);
cvConvertScaleAbs(xsobel,xsobel_dest, 1, 0);
cvConvertScaleAbs(ysobel,ysobel_dest, 1, 0);
cvReleaseImage(&xsobel);
cvReleaseImage(&ysobel);
cvReleaseImage(&xsobel);
cvReleaseImage(&img_gray);
for (int y = 0; y <sz.height; y++) {
/* ptr1 and ptr2 point to beginning of the current row in the xsobel and ysobel images respectively. ptrs point to the beginning of the current rows in the O images */
float* ptr1 = (float*) (xsobel_dest->imageData + y * (xsobel_dest->widthStep));
float* ptr2 = (float*) (ysobel_dest->imageData + y * (ysobel_dest->widthStep));
float* ptrs = (float*) (O->imageData + y * (O->widthStep));
/*For every pixel in a row gradient orientation and magnitude are calculated and corresponding values set for the bin images. */
for (int x = 0; x <sz.width; x++) {
/* if the xsobel derivative is zero for a pixel, a small value is added to it, to avoid division by zero. atan returns values in radians, which on being converted to degrees, correspond to values between -90 and 90 degrees. 90 is added to each orientation, to shift the orientation values range from {-90-90} to {0-180}. This is just a matter of convention. {-90-90} values can also be used for the calculation. */
if (ptr1[x] == 0){
//temp_gradient = (float)(((atan(ptr2[x] / (ptr1[x] + 0.00001)) *(180/ PI))) + 90.0000);
temp_gradient = ((atan(ptr2[x] /(ptr1[x] + 0.00001))) * (180/ PI)) + 90;
}
else{
//temp_gradient = (float)(((atan(ptr2[x] / ptr1[x])) *(180 / PI)) + 90.0000);
temp_gradient = ((atan(ptr2[x] / ptr1[x])) * (180 / PI)) + 90;
}
//temp_magnitude = sqrt((ptr1[x] * ptr1[x]) + (ptr2[x] * ptr2[x]));
ptrs[x] = temp_gradient;
}
}
for (int i = 0; i<in->height;i++ ){
for (int j = 0; j<in->width;j++ ){
CV_IMAGE_ELEM( imgX, int, i,j )= j; //spatial layout of X
CV_IMAGE_ELEM( imgY, int, i,j )= i; //spatial layout of Y
}
}
//Assign Feature
covarInterim[0]= imgX;
covarInterim[1]= imgY;
covarInterim[2]= r;
covarInterim[3]= g;
covarInterim[4]= b;
covarInterim[5]= xsobel_dest;
covarInterim[6]= ysobel_dest;
covarInterim[7]= O;
covarInterim[8]= &Gab0;
covarInterim[9]= &Gab85;
covarInterim[10]= &Gab180;
covarInterim[11]= &Gab265;
return covarInterim;
}
IplImage** ArrCov (IplImage* in, int patch_size, CvRect window,IplImage Gab0,IplImage Gab85,IplImage Gab180,IplImage Gab265 ){
int block_start_x, block_start_y;
int block_width = patch_size, block_height = patch_size;
CvMat vector_block;
int startc = 0;
CvSize sz = cvGetSize( in );
IplImage **ArrayCov = new IplImage*[40]; //Declare Covariance matrix array
IplImage* avgImage = cvCreateImage( sz, in->depth, 1); //Declare average image
IplImage* SingleF= cvCreateImage( sz, in->depth, 1);
cvZero(SingleF);
IplImage **ARCO = Feature(in,Gab0,Gab85,Gab180, Gab265); //get total Features
//showImage(ARCO[4], 0, "bubu");
for (block_start_y = window.y; block_start_y <= window.y + window.height - block_height; block_start_y += (block_height/2))
{
for (block_start_x = window.x; block_start_x<= window.x + window.width - block_width; block_start_x += (block_width/2))
{
IplImage **temp= new IplImage*[12];
for(int ArC=0; ArC<12; ArC++){
/* get properties, needed to create ROI image */
cvCopy(ARCO[ArC],SingleF);
cvSetImageROI( SingleF , cvRect(block_start_x, block_start_y, block_width, block_height));
temp[ArC] = cvCreateImage(cvGetSize(SingleF ),SingleF->depth,SingleF->nChannels);
cvCopy(SingleF, temp[ArC], NULL);
cvResetImageROI(SingleF);
cvZero(SingleF);
}
cvCalcCovarMatrix((const CvArr**)temp, 12, ArrayCov[startc], avgImage, CV_COVAR_NORMAL); //calculate covmatrix
cvReleaseImage(temp);
startc++;
}
}
cvReleaseImage(&avgImage);
cvReleaseImage(ARCO);
return (ArrayCov);
}
//return covarInterim;}
|