// Histogram Contrast Stretching
void Filterling ::contrastStretching (IplImage *srcImage, IplImage *dstImage) {
	// histogram연산을 위해 사용할 배열메모리를 할당 
	 unsigned int *histogramArray = new unsigned int[256],
		*sumHistogramArray = new unsigned int[256] ;
	memset (histogramArray, 0, sizeof (unsigned int) * 256) ;
	memset (sumHistogramArray, 0, sizeof (unsigned int) * 256) ;

	// 영상의 histogram을 계산 
	for (size_t i = 0 ; i < srcImage ->height ; i++) 
		for (size_t j = 0 ; j < srcImage ->width ; j++) 
			histogramArray[(unsigned int)cvGetReal2D (srcImage, i, j)]++ ;

	// histogram의 정규화된 합을 계산 
	unsigned int sum = 0 ;
	const float SCALE_FACTOR = 128.0f / (float)(srcImage ->height * srcImage ->width) ;

	for (size_t i = 0 ; i < 256 ; i++) {
		sum += histogramArray[i] ;
		sumHistogramArray[i] = (unsigned int)((sum * SCALE_FACTOR) + 0.5) ;
	}

	// LUT로써 sumHistogramArray 배열을 사용하여 영상을 변환 
	for (size_t i = 0 ; i < srcImage ->height ; i++) 
		for (size_t j = 0 ; j < srcImage ->width ; j++) 
			// pixel indexing
			cvSetReal2D (dstImage, i, j, (double) sumHistogramArray[(unsigned int)cvGetReal2D (srcImage, i, j)]) ;

	delete [] (histogramArray) ;
	delete [] (sumHistogramArray) ;
}
Beispiel #2
0
//--------------------------------------------------------------
void fluid001::opticalFlowToFluid() {

	int								x, y;
	float							dx, dy;

	float iw					= 1.0f/flow.captureWidth;
	float iy					= 1.0f/flow.captureHeight;

	int particleEmitCounter		= 0;
	float flowThreshold			= 100;
	float opticalFlowFluidMult	= 0.001;
	float multX					= (float)ofGetWidth()/flow.captureWidth;
	float multY					= (float)ofGetHeight()/flow.captureHeight;

	for ( y = 0; y < flow.captureHeight; y+=flow.captureRowsStep ){
		for ( x = 0; x < flow.captureWidth; x+=flow.captureColsStep ){

			dx					= cvGetReal2D( flow.vel_x, y, x );
			dy					= cvGetReal2D( flow.vel_y, y, x );
			if(dx*dx+dy*dy > flowThreshold) {

			addToFluid((float)x/flow.captureWidth, (float)y/flow.captureHeight, dx*opticalFlowFluidMult, dy*opticalFlowFluidMult);

			}
		}
	}
}
	/**
	 * Test the Image
	 */
	float NN::test(IplImage* Image){

		CvMat* Sample = cvCreateMat(1,IMGSIZE,CV_8U);

		IplImage *resized = cvCreateImage(cvSize(IMGWIDTH,IMGHEIGHT),Image->depth,1);
		cvResize(Image,resized,0);
		cvReleaseImage(&Image);

		for (int i=0; i< Image->width; i++)
			for (int j=0; j< Image->height; j++)
				cvSet2D(Sample,0,i*resized->width + j,cvGet2D(resized,i,j));

		cvReleaseImage(&resized);

		 CvMat *output = cvCreateMat(1,Length,CV_32F);
		 NNModel.predict(Sample,output);

		 int min = 10000;
		 for (int i=0; i<Length;i++)
		 {
			 cout << cvGetReal2D(output,0,i) << "  ";
			 if (min > cvGetReal2D(output,0,i))
				 min = i;
		 }

		 return min;
	}
Beispiel #4
0
	void CamaraLucida::convertKKopencv2opengl(CvMat* opencvKK, float width, float height, float near, float far, float* openglKK)
	{
		float fx = (float)cvGetReal2D( opencvKK, 0, 0 );
		float fy = (float)cvGetReal2D( opencvKK, 1, 1 );
		float cx = (float)cvGetReal2D( opencvKK, 0, 2 );
		float cy = (float)cvGetReal2D( opencvKK, 1, 2 );
		
		float A = 2. * fx / width;
		float B = 2. * fy / height;
		float C = 2. * (cx / width) - 1.;
		float D = 2. * (cy / height) - 1.;
		float E = - (calib.far + calib.near) / (calib.far - calib.near);
		float F = -2. * calib.far * calib.near / (calib.far - calib.near);
		
	//	col-major
		openglKK[0]= A;		openglKK[4]= 0.;	openglKK[8]= C;		openglKK[12]= 0.;
		openglKK[1]= 0.;	openglKK[5]= B;		openglKK[9]= D;		openglKK[13]= 0.;
		openglKK[2]= 0.;	openglKK[6]= 0.;	openglKK[10]= E;	openglKK[14]= F;
		openglKK[3]= 0.;	openglKK[7]= 0.;	openglKK[11]= -1.;	openglKK[15]= 0.;	
		
//		another solution by Kyle McDonald...
//		https://github.com/kylemcdonald/ofxCv/blob/master/libs/ofxCv/src/Calibration.cpp
//		glFrustum(
//				  nearDist * (-cx) / fx, nearDist * (w - cx) / fx,
//				  nearDist * (cy - h) / fy, nearDist * (cy) / fy,
//				  nearDist, farDist);
	}
bool ofxCvOpticalFlowLK::getBiggestFlowPoint(ofPoint *pos, ofPoint *vel) {
	
	float sqrMax = 0;
	float sqrMaxX = -1;
	float sqrMaxY = -1;
	
	for (int yy = 0; yy < captureHeight; yy+=2 ){
		for (int xx = 0; xx < captureWidth; xx+=2 ){
			float dx = cvGetReal2D( vel_x, yy, xx );
			float dy = cvGetReal2D( vel_y, yy, xx );
			float sqrDist = dx*dx + dy*dy;
			if(sqrMax<sqrDist) {
				sqrMaxX = xx;
				sqrMaxY = yy;
				sqrMax = sqrDist;
				vel->x = dx;
				vel->y = dy;
			}
		}
	}
	if(sqrMax>0) {
		pos->x = sqrMaxX/captureWidth;
		pos->y = sqrMaxY/captureHeight;
		return true;
	} else {
		return false;
	}
}
void ofxCvOpticalFlowLK::draw(float x,float y,float w, float h) {

	glLineWidth(1);
	ofSetColor(0xffffff);
	float speed;

	int xx, yy, dx, dy;
	float multW = w/captureWidth;
	float multH = h/captureHeight;
	glPushMatrix();
	{
		glTranslatef(x, y, 0);
		glScalef(multW, multH, 1);
		for ( yy = 0; yy < captureHeight; yy+=captureRowsStep ){
			for ( xx = 0; xx < captureWidth; xx+=captureColsStep ){

				dx = (int)cvGetReal2D( vel_x, yy, xx );
				dy = (int)cvGetReal2D( vel_y, yy, xx );
				//speed = dx * dx + dy * dy;
				ofLine(xx, yy, xx+dx * 2, yy+dy * 2);

			}
		}
	}
	glPopMatrix();
}
Beispiel #7
0
void Panoramic::ShowStitch(IplImage *profile,IplImage *center)
{
	double profileFaceDate = 0.;
	double Alpha = 0.5;//allocate middle of stich image a half of center and lateral image
	double Blendling = 0.;

	for(int j = 0;j < m_height;j++)
	{
		for(int i = 0;i < m_width;i++)
		{
			double linePosition = m_slope*i+m_intercept-j;

			profileFaceDate = cvGetReal2D(profile,j,i);

			if(linePosition <= -m_lineT)
			{
				cvSetReal2D(m_PanoramicFace,j,i,profileFaceDate);
			}

			else if(linePosition > -m_lineT && linePosition < m_lineT)
			{
				double getAlpha = LinearBlending(m_lineT,m_slope*i+m_intercept-j,Alpha);
				double getBeta  = 1-getAlpha;

				Blendling = getAlpha*cvGetReal2D(center,j,i) + getBeta*profileFaceDate;

				if(Blendling > 255)	  Blendling = 255;
				else if(Blendling < 0)Blendling = 0;
				cvSetReal2D(m_PanoramicFace,j,i,Blendling) ;
			}
		}
	}
}
Beispiel #8
0
/*
 * COG_edges
 * find intersection of chalkline with either edge
 */
void COG_edges(float *Y_left, float *Y_right)
{
    float intensity = 0.0, accum = 0.0;
    int x, y;

    *Y_left = 0.0;
    *Y_right = 0.0;

    for(x = 0; x < 40; x++)
    {
        for(y = 0; y < thresh->height; y++)
        {
            intensity = (float)(cvGetReal2D(thresh, y, x));
            *Y_left += y*intensity;
            accum += intensity;
        }
    }
    *Y_left /= accum;

    accum = 0.0;
    for(x = thresh->width-40; x < thresh->width; x++)
    {
        for(y = 0; y < thresh->height; y++)
        {
            intensity = (float)(cvGetReal2D(thresh, y, x));
            *Y_right += y*intensity;
            accum += intensity;
        }
    }
    *Y_right /= accum;
}
//--------------------------------------------------------------
void openniTracking::getVelAtNorm(float x, float y, float *u, float *v) {
	int ix = x * _width;
	int iy = y * _height;
	if(ix<0) ix = 0; else if(ix>=_width) ix = _width - 1;
	if(iy<0) iy = 0; else if(iy>=_height) iy = _height - 1;
	*u = cvGetReal2D( opticalFlow.getVelX(), iy, ix );
	*v = cvGetReal2D( opticalFlow.getVelY(), iy, ix );
}
Beispiel #10
0
void MotionTracker::getVelAtNorm(float x, float y, float *u, float *v) {
	int ix = x * camWidth;
	int iy = y * camHeight;
	if(ix<0) ix = 0; else if(ix>=camWidth) ix = camWidth - 1;
	if(iy<0) iy = 0; else if(iy>=camHeight) iy = camHeight - 1;
	*u = cvGetReal2D( opticalFlow.vel_x, iy, ix );
	*v = cvGetReal2D( opticalFlow.vel_y, iy, ix );
}
ofPoint ofxCvOpticalFlowLK::flowAtPoint(int x, int y){
	if(x >= captureWidth || x < 0 || y >= captureHeight || y < 0){
		return 0.0f;
	}	
	float fdx = cvGetReal2D( vel_x, y, x );
	float fdy = cvGetReal2D( vel_y, y, x );
	return ofPoint(fdx, fdy);
}
//Get the velocity of each block given the x and y of that block 
ofPoint ofxOpticalFlowBM :: getBlockVel( int x, int y ) {
  ofPoint p;
  if( x < flowSize.width && y < flowSize.height ) {
    p.x = cvGetReal2D(opFlowVelX, y, x);   //NOTE: y then x ... annoying
    p.y = cvGetReal2D(opFlowVelY, y, x);   //NOTE: y then x ... annoying
  }
  return p;
}
Beispiel #13
0
	void CamaraLucida::printM(CvMat* M, bool colmajor)
	{
		if (ofGetLogLevel() != OF_LOG_VERBOSE)
		{
			return;
		}
		
		int i,j;
		if (colmajor)
		{
			for (i = 0; i < M->rows; i++)
			{
				printf("\n");				
				switch( CV_MAT_DEPTH(M->type) )
				{
					case CV_32F:
					case CV_64F:
						for (j = 0; j < M->cols; j++)
							printf("%9.3f ", (float)cvGetReal2D( M, i, j ));
						break;
					case CV_8U:
					case CV_16U:
						for (j = 0; j < M->cols; j++)
							printf("%6d",(int)cvGetReal2D( M, i, j ));
						break;
					default:
						break;
				}
			}
		}
		else
		{
			for (j = 0; j < M->cols; j++)
			{
				printf("\n");				
				switch( CV_MAT_DEPTH(M->type) )
				{
					case CV_32F:
					case CV_64F:
						for (i = 0; i < M->rows; i++)
							printf("%9.3f ", (float)cvGetReal2D( M, i, j ));
						break;
					case CV_8U:
					case CV_16U:
						for (i = 0; i < M->rows; i++)
							printf("%6d",(int)cvGetReal2D( M, i, j ));
						break;
					default:
						break;
				}
			}
		}
		printf("\n");
	}
Beispiel #14
0
void MotionTracker::getVelAverageComponents(float *u, float *v, ofRectangle* bounds){
	bool cleanBounds = false;
	if(bounds == NULL){
		bounds = new ofRectangle(0, 0, optFlowWidth, optFlowHeight);
		cleanBounds = true;
	}
	
	// normalize the bounds to optical flow system
	bounds->x = (float)bounds->x/(float)camWidth*(float)optFlowWidth; // normalize co-ords from camera size to optical flow size
	bounds->y = (float)bounds->y/(float)camHeight*(float)optFlowHeight;
	bounds->width = (float)bounds->width/(float)camWidth*(float)optFlowWidth;
	bounds->height = (float)bounds->height/(float)camHeight*(float)optFlowHeight;
	
	// fix out of bounds
	if(bounds->x < 0){
		bounds->width += bounds->x;
		bounds->x = 0;
	}
	else if(bounds->x + bounds->width > optFlowWidth){
		bounds->width -= ((bounds->x + bounds->width) - optFlowWidth);
	}
	if(bounds->y < 0){
		bounds->height += bounds->y;
		bounds->y = 0;
	}
	else if(bounds->y + bounds->height > optFlowHeight){
		bounds->height -= ((bounds->y + bounds->height) - optFlowHeight);
	}
	
/**	cout << "--bounds:norm--" << endl;
	cout << "x:" << bounds->x << endl;
	cout << "y:" << bounds->y << endl;
	cout << "w:" << bounds->width << endl;
	cout << "h:" << bounds->height << endl;
	cout << "--------------------" << endl;*/
	
	*u = 0.0;
	*v = 0.0;
	for(int i=bounds->x; i < bounds->x+(int)bounds->width; i++){
		for(int j=bounds->y; j < bounds->y+(int)bounds->height; j++){
			*u += cvGetReal2D( opticalFlow.vel_x, j, i);
			*v += cvGetReal2D( opticalFlow.vel_y, j, i);
//			cout << i << "," << j << "::" << *u << "," << *v << endl;
		}
	}
	
	*u /= 2.0 * (float)bounds->height; // effectively: u / (width*height) * width/2.0
	*v /= 2.0 * (float)bounds->width; // effectively: v / (width*height) * height/2.0
	
	if(cleanBounds)
		delete bounds;
}
Beispiel #15
0
static void dance_measurement(const CvMat* x_k,
                              const CvMat* n_k,
                              CvMat* z_k)
{
    cvSetReal2D(z_k, 0, 0, cvGetReal2D(x_k, 0, 0));
    cvSetReal2D(z_k, 1, 0, cvGetReal2D(x_k, 1, 0));

    /* as above, skip this step when n_k is null */
    if(n_k)
    {
        cvAdd(z_k, n_k, z_k);
    }
}
 void Calibration::printM( CvMat* M, bool colmajor )
 {
     int i,j;
     if (colmajor)
     {
         for (i = 0; i < M->rows; i++)
         {
             printf("\n");				
             switch( CV_MAT_DEPTH(M->type) )
             {
                 case CV_32F:
                 case CV_64F:
                     for (j = 0; j < M->cols; j++)
                         printf("%9.3f ", (float)cvGetReal2D( M, i, j ));
                     break;
                 case CV_8U:
                 case CV_16U:
                     for (j = 0; j < M->cols; j++)
                         printf("%6d",(int)cvGetReal2D( M, i, j ));
                     break;
                 default:
                     break;
             }
         }
     }
     else
     {
         for (j = 0; j < M->cols; j++)
         {
             printf("\n");				
             switch( CV_MAT_DEPTH(M->type) )
             {
                 case CV_32F:
                 case CV_64F:
                     for (i = 0; i < M->rows; i++)
                         printf("%9.3f ", (float)cvGetReal2D( M, i, j ));
                     break;
                 case CV_8U:
                 case CV_16U:
                     for (i = 0; i < M->rows; i++)
                         printf("%6d",(int)cvGetReal2D( M, i, j ));
                     break;
                 default:
                     break;
             }
         }
     }
     printf("\n\n");
 };
void adjustRMatrixAndZForMeasurementSize(CvMat*& R, CvMat*& z, unsigned int nmeas)
{
	if(nmeas == 0)
	{
		return;
	}

	unsigned int nrows_prev = (R)->rows;
	unsigned int nrows_z_prev = (z)->rows;
	if(nrows_prev < 4)
	{
		fprintf(stderr, "adjustRMatrixForMeasurementSize Error:  Existing R is too small.\n");
		return;
	}

	/* x, y, th each meas + z */
	unsigned int nrows_now = 3*nmeas + 1;
	/* same number of measurements -> do nothing */
	if(nrows_now == nrows_prev && nrows_now == nrows_z_prev)
	{
		return;
	}

//	printf("adjR a  %d %d\n", nrows_prev, nrows_now);
	double sx = cvGetReal2D(R, 0, 0);
	double sy = cvGetReal2D(R, 1, 1);
	double sth = cvGetReal2D(R, 2, 2);
	double sz = cvGetReal2D(R, nrows_prev-1, nrows_prev-1);

//	printf("adjR b\n");
	cvReleaseMat(&R);
	cvReleaseMat(&z);
//	printf("adjR c\n");
	R = cvCreateMat(nrows_now, nrows_now, CV_64FC1);
	z = cvCreateMat(nrows_now, 1, CV_64FC1);
//	printf("adjR d\n");
	cvZero(R);
	cvZero(z);
//	printf("adjR e\n");
	for(unsigned int i = 0; i < nmeas; i++)
	{
		cvSetReal2D(R, i*3 + 0, i*3 + 0, sx);
		cvSetReal2D(R, i*3 + 1, i*3 + 1, sy);
		cvSetReal2D(R, i*3 + 2, i*3 + 2, sth);
	}
//	printf("adjR f\n");
	cvSetReal2D(R, nrows_now-1, nrows_now-1, sz);
}
IplImage* Saliency::Get(const IplImage* imgIn)
{
	if (imgIn != NULL)
	{
		IplImage* tt = cvCloneImage(imgIn);
		Mat sal(tt);
		Mat img3f(tt);
		img3f.convertTo(img3f, CV_32FC3, 1.0/255);//图片转换,可能是0-255之间转换到0-1之间,归一化
		sal = GetRC(img3f);
		sal.convertTo(sal,CV_32FC3,255);
		IplImage qImg(sal);

		IplImage * result = cvCreateImage(cvGetSize(imgIn),8,1);
		for (int r=0;r<imgIn->height;r++)
		{
			for (int c=0;c<imgIn->width;c++)
			{
				CvScalar col;
				col.val[0] = cvGetReal2D(&qImg,r,c);
				cvSet2D(result,r,c,col);
			}
		}
		return result;
	}
	return NULL;
}
void PintarFeatures(IplImage& vx,IplImage& vy,IplImage& windowBackground,CvPoint2D32f frame1_features[],int number_of_features,float optical_flow_feature_error[],char optical_flow_found_feature[])
{
	for(int i = 0; i < number_of_features; i++)
	{
		int line_thickness=1;			
		CvScalar line_color = CV_RGB(0,255,0);

		CvPoint p,q;
		p.x = (int) frame1_features[i].x;
		p.y = (int) frame1_features[i].y;
		q.x =p.x+ cvGetReal2D(&vx, p.y,p.x);
		q.y =p.y + cvGetReal2D(&vy, p.y,p.x);

		PaintPoint(p,q,windowBackground,line_thickness,line_color,1);
	}
}
Beispiel #20
0
	TiXmlElement* FileFormatUtils::createXMLMatrix(const char* element_name, const CvMat *matrix) {
		if (!matrix) return NULL;

		TiXmlElement* xml_matrix = new TiXmlElement(element_name);
		int precision;
		if (cvGetElemType(matrix) == CV_32F) {
			xml_matrix->SetAttribute("type", "CV_32F");
			precision = std::numeric_limits<float>::digits10 + 2;
		}
		else if (cvGetElemType(matrix) == CV_64F) {
			xml_matrix->SetAttribute("type", "CV_64F");
			precision = std::numeric_limits<double>::digits10 + 2;
		}
		else {
			delete xml_matrix;
			return NULL;
		}

		xml_matrix->SetAttribute("rows", matrix->rows);
		xml_matrix->SetAttribute("cols", matrix->cols);

		for (int r = 0; r < matrix->rows; ++r) {
			for (int c = 0; c < matrix->cols; ++c) {
				TiXmlElement *xml_data = new TiXmlElement("data");
				xml_matrix->LinkEndChild(xml_data);
				std::stringstream ss;
				ss.precision(precision);
				ss<<cvGetReal2D(matrix, r, c);
				xml_data->LinkEndChild(new TiXmlText(ss.str().c_str()));
			}
		}
		return xml_matrix;
	}
Beispiel #21
0
// Generates and returns the histogram of a GRAYSCALE image.
IplImage* DrawHistogram(IplImage* img)
{
	CvSize imgSize = cvGetSize(img);
	int area = imgSize.width*imgSize.height;

	// Holds the actual histogram image
	IplImage* ret = cvCreateImage(cvSize(257, 100), 8, 1);
	cvZero(ret);

	int freq[256] = {0};
	int max=0;

	// Loop through each pixel of the image
	for(int x=0;x<imgSize.width;x++)
	{
		for(int y=0;y<imgSize.height;y++)
		{
			// Increment the frequency
			int curr = (int)cvGetReal2D(img, y, x);
			freq[curr]++;
			if(freq[curr]>max)
				max = freq[curr];
		}
	}

	// Finally, draw the actual histogram
	for(int k=0;k<256;k++)
	{
		int value = ((float)(100*freq[k])/(float)max);
		cvLine(ret, cvPoint(k, 100), cvPoint(k, 100-value), cvScalar(255,255,255));
	}
	
	cvNot(ret, ret);
	return ret;
}
Beispiel #22
0
//--------------------------------------------------------------
void testApp::draw(){
    cameraImg.draw(0,0);
    grayImg.draw(320,0);
    pastImg.draw(0,240);
    
    for(int y = 0; y < rows; y++){
        for(int x = 0; x < cols; x++){
            int dx = (int) cvGetReal2D (velx, y, x);
            int dy = (int) cvGetReal2D (vely, y, x);
            int xx = x * block_size;
            int yy = y * block_size;
            ofLine(xx, yy, xx + dx, yy + dy);
        }
    }
    
}
Beispiel #23
0
CvScalar myCvPointANNF(IplImage* img, CvPoint p, int size, double l_channel_weight) {
    assert(size%2 == 1);
    CvMat* mat = cvCreateMat(size, size, CV_32FC3);
    CvMat* d = cvCreateMat(size, size, CV_32FC1); 
//    CvMat* w = cvCreateMat(size, size, CV_32FC1);
    
    int rad = (size - 1)/2;

    for (int r = -rad; r <= rad; r++) { // y-axis
	CvPoint center = cvPoint(p.x, p.y + r);
	int y = rad + r;
	for (int rr = -rad; rr <= rad; rr++) { // x-axis
	    CvPoint this = myCvHandleEdgePoint(
		cvPoint(center.x + rr, center.y),
		img->width,
		img->height
		);
	    int x = rad + r;
	    cvSet2D(mat, x, y, cvGet2D(img, this.y, this.x)); // inversed xy
	}
    }
    
    double max_d = .0;
    double sum = .0;
    for (int i = 0; i < size; i++) {
	for (int j = 0; j < size; j++) {
	    double dd = .0;
	    // compute d(i, j)
	    for (int k = 0; k < size; k++) {
		for (int l = 0; l < size; l++) {
		    if (!(k == i && l == j)) {
			dd += euDist(cvGet2D(mat, i, j), cvGet2D(mat, k, l), l_channel_weight);
		    }
		}
	    }
	    if (dd > max_d)
		max_d = dd;
	    sum += dd;
	    cvSetReal2D(d, i, j, dd);
	}
    }

    double denom = max_d*size*size - sum;

    CvScalar re = cvScalar(0, 0, 0, 0);
    for (int i = 0; i < size; i++) {
	for (int j = 0; j < size; j++) {
	    // compute weight
	    double ww = (max_d - cvGetReal2D(d, i, j))/denom;
	    for (int k = 0; k < 4; k++) {
		re.val[k] += ww*cvGet2D(mat, i, j).val[k];
	    }
	}
    }

    cvReleaseMat(&mat);
    cvReleaseMat(&d);

    return re;
}
Beispiel #24
0
/*
 * findEdge
 * 1 for leftEdge, 2 for rightEdge
 */
void findEdge(IplImage *image)
{
    gray = cvCreateImage(cvSize(image->width, image->height), IPL_DEPTH_8U, 1);
    bw = cvCreateImage(cvSize(image->width, image->height), IPL_DEPTH_8U, 1);

    cvCvtColor(frame, gray, CV_BGR2GRAY);
    cvThreshold(gray, bw, 200, 255, CV_THRESH_BINARY);

    int intensity = 0, left_accum = 0, right_accum = 0;
    int x, y;

    for(x = 0; x < bw->width; x++)
    {
        for(y = 0; y < bw->height; y++)
        {
            intensity = (int)(cvGetReal2D(bw, y, x)/255);
            if(x <= 40) left_accum += intensity;
            else if(x >= bw->width-40) right_accum += intensity;
        }
    }

    //edge on left
    if((left_accum>5000) && (left_accum>right_accum)) flag = 1;
    //edge on right
    else if((right_accum>5000) && (right_accum>left_accum)) flag = 2;
    //neither edge
    else flag  = 0;
}
void GS_rotate( IplImage* srcImg, IplImage *srcImgRotated, int angle )
{
    int i, j;
    double new_x, new_y, var;    

    int height = srcImg->height;
    int width  = srcImg->width;
    
	double radius = -angle*(M_PI/180.0);
    double sin_value = sin(radius);
    double cos_value = cos(radius);

    int centerX = height/2;
    int centerY = width/2;

    for(i=0; i<height; i++)
    {
        for(j=0; j<width; j++)
        {
            new_x = (i-centerX)*cos_value - (j-centerY)*sin_value + centerX;
            new_y = (i-centerX)*sin_value + (j-centerY)*cos_value + centerY;

            if(new_x < 0 || new_x > height) 
            {
                var = 0.0; 
            } else if(new_y < 0 || new_y > width) {
                var = 0.0; 
            } else {
                var = cvGetReal2D( srcImg, (int)new_x, (int)new_y );
            }

            cvSetReal2D( srcImgRotated, i, j, var );
        }
    }    
}
mat load_mat(IplImage* img)

{

       int i,j;

       int width=img->width;

       int height=img->height;  

       mat m = mat_new (height, width);

       for(i=0;i<height;i++)

       {

              for(j=0;j<width;j++)

              {

                     m[i][j]=cvGetReal2D(img, j, i );

              }

       }

       return m;

}
Beispiel #27
0
void MT_Display_CvMat(const CvMat* M, const char* name, FILE* f)
{
    if(name)
    {
        fprintf(f, "Matrix %s ", name);
        for(unsigned int i = 0; i < 35 - strlen(name); i++)
        {
            fprintf(f, "-");
        }
        fprintf(f, "\n");
    }

    if(!M && name)
    {
        fprintf(f, "   (uninitialized)\n");
        return;
    }
    
    for(unsigned int i = 0; i < M->rows; i++)
    {
        for(unsigned int j = 0; j < M->cols; j++)
        {
            fprintf(f, "%4.3f\t", cvGetReal2D(M, i, j));
        }
        fprintf(f, "\n");
    }
}
Beispiel #28
0
void myCvPixelPaletteDescriptor(MyQuantizedImage* quant, MyCvDescriptor* _descriptor, CvPoint _pixel, int _neighbor_size) {
    assert((_descriptor->type == MY_DESCRIPTOR_TYPE_PALETTE) && (_neighbor_size >= 0));

    // initialization
    CvMat* labelMat = quant->labelMat;
    int paletteSize = quant->tableSize;

    MyCvPalette* palette = myCvCreatePalette(paletteSize);
    CvScalar* ct = calloc(quant->tableSize, sizeof(CvScalar));

    float* proportion = calloc(quant->tableSize, sizeof(float));
    for (int i = 0; i < quant->tableSize; i++) {
	proportion[i] = .0;
	ct[i] = (quant->colorTable)[i];
    }

    CvMat* interest_rect = sub_square_mat(labelMat, _pixel, _neighbor_size);
    for (int row = 0; row < interest_rect->rows; row++) {
	//const int* ptr = (const int*) (interest_rect->data.ptr) + row*interest_rect->step;
	for (int col = 0; col < interest_rect->cols; col++) {
	    int label = (int) cvGetReal2D(interest_rect, row, col);
	    proportion[label] += 1;
	}
    }
    
    int size = interest_rect->rows*interest_rect->cols;
    for (int i = 0; i < quant->tableSize; i++) {
	proportion[i] = proportion[i]/size;
    }
    palette->proportion = proportion;
    palette->colorTable = ct;

    _descriptor->data = palette;
}
ofPoint ofxOpticalFlowLK :: getVelAtPixel ( int x, int y )
{
	x = ofClamp( x, 0, sizeSml.width  - 1 );
	y = ofClamp( y, 0, sizeSml.height - 1 );
	
	ofPoint p;
	p.x = cvGetReal2D( opFlowVelX, y, x );
	p.y = cvGetReal2D( opFlowVelY, y, x );

    if( opticalFlowMin > 0 )
        limitMin( p, opticalFlowMin );
    
    if( opticalFlowMax > 0 )
        limitMax( p, opticalFlowMax );
	
	return p;
}
/*
Calculates interpolated pixel contrast.  Based on Eqn. (3) in Lowe's paper.

@param dog_pyr difference of Gaussians scale space pyramid
@param octv octave of scale space
@param intvl within-octave interval
@param r pixel row
@param c pixel column
@param xi interpolated subpixel increment to interval
@param xr interpolated subpixel increment to row
@param xc interpolated subpixel increment to col

@param Returns interpolated contrast.
*/
 float SiftGPU::InterpContr( IplImage*** dog_pyr, int octv, int intvl, int r,
							int c, float xi, float xr, float xc )
{
	CvMat* dD, X, T;
	float t[1], x[3] = { xc, xr, xi };

	cvInitMatHeader( &X, 3, 1, CV_64FC1, x, CV_AUTOSTEP );
	cvInitMatHeader( &T, 1, 1, CV_64FC1, t, CV_AUTOSTEP );
	dD = Deriv3D( dog_pyr, octv, intvl, r, c );

	//cvGEMM( dD, &X, 1, NULL, 0, &T,  CV_GEMM_A_T );
	t[0] = cvGetReal2D(dD, 0, 0) * x[0] + cvGetReal2D(dD, 1, 0) * x[1] + cvGetReal2D(dD, 2, 0) * x[2];

	cvReleaseMat( &dD );

	return pixval32f( dog_pyr[octv][intvl], r, c ) + t[0] * 0.5;
}