/*!
    \fn CvBinGabAdaFeatureSelect::init_weights()
 */
void CvBinGabAdaFeatureSelect::init_weights()
{
  weights = cvCreateMat(nsamples, 1, CV_32FC1);
  double posweight, negweight;
  posweight = 1.0/npositive/2.0;
  negweight = 1.0/nnegative/2.0;
  if( possub > 0 )  // face recogntion
  {
    for(int i = 0; i < nsamples; i++)
    {
      int id = (int)cvGetReal1D(classIdx, i);
      if(id == possub) cvSetReal1D(weights, i, posweight);
      else cvSetReal1D(weights, i, negweight);
    }
  }
  else   //gender detection
  {
    for(int i = 0; i < nsamples; i++)
    {
      int id = (int)cvGetReal1D(classIdx, i);
      bool sex = ((CvXm2vts*)database)->getGender( id );
      if(sex) cvSetReal1D(weights, i, posweight);
      else cvSetReal1D(weights, i, negweight);
    }
  }
}
Example #2
0
void radial_sample(int width, int height, char* data, IplImage *unwrapped, int slice)
{
	IplImage *cvcast = cvCreateImageHeader(cvSize(width, height),
			IPL_DEPTH_8U, 1);
	cvcast->imageData = data;

	// cvSaveImage("slice.png",cvcast);
	
	CvPoint center = cvPoint(cx,cy);

	unsigned char* linebuf;
	for(int sample = 0; sample < RADIAL_SAMPLES; sample++) {
		float theta = ((float)sample)*((2.0*PI)/(float)RADIAL_SAMPLES);
		CvPoint outer = calc_ray_outer(theta, center);

		// printf("%g:\t%d,%d\n", theta*(180.0/PI), outer.x, outer.y);
		cvClipLine(cvSize(width, height), &outer, &center);
		int linesize = abs(center.x-outer.x)+abs(center.y-outer.y)+1;
		linebuf = (unsigned char*)malloc(linesize);
		cvSampleLine(cvcast,outer,center,linebuf,4);
		
		IplImage *castline = cvCreateImageHeader(cvSize(linesize,1), IPL_DEPTH_8U, 1);
		castline->imageData = (char*)linebuf;

		IplImage *sobel = cvCreateImage(cvSize(linesize,1), IPL_DEPTH_8U, 1);

		cvSobel(castline, sobel, 1, 0, 3);

		int layer = 0;
		for(int i = 0; (i < linesize) && (layer < MAX_LAYERS); i++) {
			// printf(" %d,", (int)cvGetReal1D(sobel,i));
			if((int)cvGetReal1D(sobel,i) > SOBEL_THRESH) {
				int max = 0, max_i = 0;
				for(; i < linesize; i++) {
					int curval = (int)cvGetReal1D(sobel,i);
					if(curval == 0) break;
					if(curval > max) {
						max = curval;
						max_i = i;
					}
				}
				cvSetReal2D(unwrapped,slice,(layer*RADIAL_SAMPLES)+sample,cvGetReal1D(castline,max_i));
				// printf("%d\t",max);
				layer++;
			}
		}
		// printf("\n");
	
		/*	
		char filename[] = "line000.png";
		sprintf(filename,"line%03d.png",(int)(theta*(180.0/PI)));
		cvSaveImage(filename,sobel);
		*/
		
		cvReleaseImageHeader(&castline);
		cvReleaseImage(&sobel);

		free(linebuf);
	}
}
/*!
\fn CvGaborFeature::_XM2VTSMulti(const char *pathname, const CvMat* picIndex, const CvMat* subIndex) const
 */
CvTrainingData* CvGaborFeature::_XM2VTSMulti(const char *pathname, const CvMat* picIndex, const CvMat* subIndex) const
{
  CvSize size = cvGetSize(picIndex);
  int npic = size.width;
  size = cvGetSize(subIndex);
  int nosub = size.width;
  int nsample = nosub*npic;
  int n = 0;
  CvTrainingData *data = new CvTrainingData;
  data->init(nosub, nsample, 1);
  CvMat *mat = cvCreateMat(nsample, 1, CV_32FC1);
  double v;

  for (int i = 1; i <= nosub; i++)
  {
    for (int j = 1; j <= npic; j++)
    {
      v = XM2VTSdata( pathname, (int)cvGetReal1D(subIndex, (i-1)), (int)cvGetReal1D(picIndex, (j-1)) );
      cvSetReal1D(mat, n, v);
      data->setclsidxofsample(i, n);
      n++;
    }
  }
  data->setdata(mat);
  cvReleaseMat(&mat);
  data->statclsdist();
  return data; 
}
Example #4
0
IplImage* DrawHistogram(CvHistogram *hist, float scaleX=2, float scaleY=2){
    float histMax = 0;
    cvGetMinMaxHistValue(hist, 0, &histMax, 0, 0);
    IplImage* imgHist = cvCreateImage(cvSize(256*scaleX, 64*scaleY), 8 ,1);

    cvZero(imgHist); //clear all elements to be zeros.
    cvSet(imgHist, cvScalar(255)); //set all to be 255; white background.

    for(int i=0; i< 255; i++) {
    	//float histValue = cvQueryHistValue_1D(hist, i); //not working, use the one below instead.
    	float histValue = cvGetReal1D(hist->bins, i);
        //float nextValue = cvQueryHistValue_1D(hist, i+1); //not working.
        float nextValue = cvGetReal1D(hist->bins, i+1);

        CvPoint pt1 = cvPoint(i*scaleX, 64*scaleY);
        CvPoint pt2 = cvPoint(i*scaleX+scaleX, 64*scaleY);
        CvPoint pt3 = cvPoint(i*scaleX+scaleX, (64-nextValue*64/histMax)*scaleY);
        CvPoint pt4 = cvPoint(i*scaleX, (64-histValue*64/histMax)*scaleY);

        int numPts = 5;
        CvPoint pts[] = {pt1, pt2, pt3, pt4, pt1};
        cvFillConvexPoly(imgHist, pts, numPts, cvScalar(0)); //filled with black color.
    }
    return imgHist;
}
/*!
    \fn CvFaceSVMClassifier::Training_error(CvMat * train_data, CvMat * labels) const
 */
CvScalar CvFaceSVMClassifier::Training_error(CvMat * train_data, CvMat * labels) const
{
  CvSize size = cvGetSize(labels);
  int nsamples = size.width * size.height;
  
  int numpositive = 0, numnegative = 0;
  for(int i = 0; i < nsamples; i++)
  {
    double value = cvGetReal1D( labels, i );
    if ( value == 1.0 )
      numpositive++;
    else if( value == 2.0 )
      numnegative++;
  }
  
  assert((numpositive+numnegative) == nsamples );
  
  size = cvGetSize( train_data );
  
  int nelements = size.height;
  
  CvMat * sample = cvCreateMat(1, nelements, CV_32FC1);
  
  int numerror = 0, numtruepositive = 0, numfalsepositive = 0;
  for(int i = 0; i < nsamples; i++)
  {
    cvGetRow( train_data, sample, i );
    double pre_label = Predict( sample );
    double label = cvGetReal1D( labels, i );
    if((pre_label == 1.0)&&(label == 1.0))
    {
      numtruepositive++;
    }
    if(pre_label != label)
    {
      if((pre_label == 1.0)&&(label == 2.0))
      {
        numfalsepositive++;
      }
      //printf("%d   ", i);
      numerror++;
    }
  }
  printf("\n\n");
  double error = (double)numerror/(double)nsamples;
  double tp_rate = (double)numtruepositive/(double)numpositive;
  double fp_rate = (double)numfalsepositive/(double)numnegative;
  
  cvReleaseMat(&sample);
  
  CvScalar scalar = cvScalar(error, tp_rate, fp_rate, 0);
  
  return scalar;
}
/*!
    \fn CvFaceSVMClassifier::Training_error(CvGaborResponseData & gabordata, CvGaborFeaturePool & new_features) const
 */
CvScalar CvFaceSVMClassifier::Training_error(CvGaborResponseData & gabordata, CvGaborFeaturePool & new_features) const
{
  CvMat * train_data = GetDataFromFeatures( gabordata, new_features );
  CvMat * labels = GetLabelsFromFeatures( gabordata, new_features );
   
  CvSize size = cvGetSize(labels);
  int nsamples = size.width * size.height;
  
  int numpositive = 0, numnegative = 0;
  for(int i = 0; i < nsamples; i++)
  {
    double value = cvGetReal1D( labels, i );
    if ( value == 1.0 )
      numpositive++;
    else if( value == 2.0 )
      numnegative++;
  }
  
  assert((numpositive+numnegative) == nsamples );
  
  int nelements = new_features.getSize();
  
  CvMat * sample = cvCreateMat(1, nelements, CV_32FC1);
  
  int numerror = 0, numtruepositive = 0, numfalsepositive = 0;
  for(int i = 0; i < nsamples; i++)
  {
    cvGetRow( train_data, sample, i );
    double pre_label = Predict( sample );
    double label = cvGetReal1D( labels, i );
    if((pre_label == 1.0)&&(label == 1.0))
    {
      numtruepositive++;
    }
    if(pre_label != label)
    {
      if((pre_label == 1.0)&&(label == 2.0))
        numfalsepositive++;
      numerror++;
    }
  }
  
  double error = (double)numerror/(double)nsamples;
  double tp_rate = (double)numtruepositive/(double)numpositive;
  double fp_rate = (double)numfalsepositive/(double)numnegative;
  
  cvReleaseMat(&sample);
  cvReleaseMat(&train_data);
  cvReleaseMat(&labels);
  
  CvScalar scalar = cvScalar(error, tp_rate, fp_rate, 0);
  
  return scalar;
}
/*!
    \fn CvGaborFeature::_FERETBin_F(const CvFeret* feret, int possub, const CvMat *index) const
 */
CvTrainingData* CvGaborFeature::_FERETBin_F(const CvFeret* feret, int possub, const CvMat *index) const
{
  int nosub = feret->getSub();
  int nsamples = feret->getNum();
  /*    Generate filename   */
  string path = feret->getMainpath();
  char * filename = new char[50];
  sprintf(filename, "%s/%d/%d/%d_%d.xml", path.c_str(), iNu, iMu, ix, iy);
  
  
  CvMat* mat = (CvMat*)cvLoad( filename, NULL, NULL, NULL );
  //cvTranspose( mat, mat );
  CvTrainingData *bindata = new CvTrainingData;
  bindata->init(2, nsamples, 1);
  bindata->setdata( mat );
  
  for(int i = 0; i < nsamples; i++)
  {
    int id = (int)cvGetReal1D(index, i);
    if(id == possub) bindata->setclsidxofsample( 1, i);
    else bindata->setclsidxofsample( 2, i);
  }
  bindata->statclsdist();
  delete [] filename;
  return bindata;
}
/*!
    \fn CvBinGabAdaFeatureSelect::setSub(int nosub)
 */
void CvBinGabAdaFeatureSelect::setSub(int nosub)
{
  if (nosub > 0)
  {
    this->possub = nosub;
    npositive = 0;
    for(int i = 0; i < nsamples; i++)
    {
      int id = (int)cvGetReal1D(classIdx, i);
      if(id == possub) npositive++;
    }
    nnegative = nsamples - npositive;
  }
  else
  {
    this->possub = nosub;
    if(db_type == XM2VTS)
    {
      int m = 0;
      int f = 0;
      for (int i = 0; i < nClass; i++)
      {
        bool gen = ((CvXm2vts*)database)->getGender( i+1);
        if( gen ) m++;
        else f++;
      }
      npositive = m*nperClass;
      nnegative = f*nperClass;
    }
  }
}
FLOAT_POINT2D mcvGetVanishingPoint(const CameraInfo *cameraInfo)
{
    //get the vp in world coordinates
    FLOAT_MAT_ELEM_TYPE vpp[] = {sin(cameraInfo->yaw)/cos(cameraInfo->pitch),
                                 cos(cameraInfo->yaw)/cos(cameraInfo->pitch), 0};
    CvMat vp = cvMat(3, 1, FLOAT_MAT_TYPE, vpp);

    //transform from world to camera coordinates
    //
    //rotation matrix for yaw
    FLOAT_MAT_ELEM_TYPE tyawp[] = {cos(cameraInfo->yaw), -sin(cameraInfo->yaw), 0,
                                   sin(cameraInfo->yaw), cos(cameraInfo->yaw), 0,
                                   0, 0, 1};
    CvMat tyaw = cvMat(3, 3, FLOAT_MAT_TYPE, tyawp);
    //rotation matrix for pitch
    FLOAT_MAT_ELEM_TYPE tpitchp[] = {1, 0, 0,
                                     0, -sin(cameraInfo->pitch), -cos(cameraInfo->pitch),
                                     0, cos(cameraInfo->pitch), -sin(cameraInfo->pitch)};
    CvMat transform = cvMat(3, 3, FLOAT_MAT_TYPE, tpitchp);
    //combined transform
    cvMatMul(&transform, &tyaw, &transform);

    //
    //transformation from (xc, yc) in camra coordinates
    // to (u,v) in image frame
    //
    //matrix to shift optical center and focal length
    FLOAT_MAT_ELEM_TYPE t1p[] = {
        cameraInfo->focalLength.x, 0,
        cameraInfo->opticalCenter.x,
        0, cameraInfo->focalLength.y,
        cameraInfo->opticalCenter.y,
        0, 0, 1};
    CvMat t1 = cvMat(3, 3, FLOAT_MAT_TYPE, t1p);
    //combine transform
    cvMatMul(&t1, &transform, &transform);
    //transform
    cvMatMul(&transform, &vp, &vp);

    //
    //clean and return
    //
    FLOAT_POINT2D ret;
    ret.x = cvGetReal1D(&vp, 0);
    ret.y = cvGetReal1D(&vp, 1);
    return ret;
}
/*!
    \fn CvBinGabAdaFeatureSelect::errorthreshold()
 */
double CvBinGabAdaFeatureSelect::errorthreshold()
{

  double nsum = 0.0, psum = 0.0;
  //CvSize class_size = cvGetSize( classIdx );
  //CvSize weights_size = cvGetSize( weights );
  for(int i = 0; i < nsamples; i++)
  {
    int id = (int)cvGetReal1D( classIdx, i );
    double w = cvGetReal1D( weights, i );
    if( id == possub ) psum = psum + w;
    else nsum = nsum + w;
  }    
  
  return nsum/2.0;

}
/*!
    \fn CvGaborFeature::_XM2VTSGender_F(const CvXm2vts* xm2vts, int ntpic) const
 */
CvTrainingData* CvGaborFeature::_XM2VTSGender_F(const CvXm2vts* xm2vts, int ntpic) const
{
  int nosub = 200;
  int nopic = 8;
  
  /*    Generate filename   */
  char *filename = new char[100];
  char *ch_scale = new char[5];
  char *ch_orient = new char[5];
  char *ch_name = new char[10];
  strcpy( filename, xm2vts->getPath() );
  sprintf( ch_scale, "%d", iNu );
  strcat(filename, ch_scale);
  strcat(filename, "/");
  
  sprintf( ch_orient, "%d", iMu );
  strcat( filename, ch_orient );
  strcat(filename, "/");
  sprintf(ch_name, "%d_%d.xml", ix, iy);
  strcat(filename, ch_name);
  delete [] ch_scale;
  delete [] ch_orient;
  delete [] ch_name;
  /*  Generate filename   */
  
  /* Get data from the file */
  CvMat* mat = (CvMat*)cvLoad( filename, NULL, NULL, NULL );
  
  int numsample = nosub*ntpic;
  CvTrainingData *bindata = new CvTrainingData;
  CvMat* tmpmat = cvCreateMat(numsample, 1, CV_32FC1);
  bindata->init(2, numsample, 1);
  
  int n = 0;
  double v;
  for(int sub = 0; sub < nosub; sub++)
  {
    int ind;
    bool gender = xm2vts->getGender( sub+1 );
    if (gender) ind = 1;
    else ind = 2;
    for(int i = 0; i < ntpic; i++)
    {
      v = cvGetReal1D( mat, sub*nopic + i);
      cvSetReal1D( tmpmat, sub*ntpic+i, v);
      bindata->setclsidxofsample( ind, sub*ntpic+i);
    }
  }
  
  
  bindata->setdata(tmpmat);
  
  cvReleaseMat(&tmpmat);
  cvReleaseMat(&mat);
  delete [] filename;
  bindata->statclsdist();
  return bindata;
}
int main( int argc, char** argv )
{
    IplImage *src = 0;
    IplImage *histimg = 0;
    CvHistogram *hist = 0;
    
    int hbins = 255;     // 划分HIST的个数,越高越精确
	int maxcount = 255;
    float hranges_arr[] = {0,maxcount};//
    float* hranges = hranges_arr;
    int bin_w;  
    float max_val;
    int i;
    
    if((src=cvLoadImage("../cvtest/7newsample.jpg", CV_LOAD_IMAGE_GRAYSCALE)) == NULL)  // force to gray image
        return -1;
    
    cvNamedWindow( "Histogram", 0 );
    cvNamedWindow( "src", 0);
    
    hist = cvCreateHist( 1, &hbins, CV_HIST_ARRAY, &hranges, 1 );  // 计算直方图
    histimg = cvCreateImage( cvSize(320,200), 8, 3 );
    cvZero( histimg );
    cvCalcHist( &src, hist, 0, 0 ); // 计算直方图
   float min_val = 0;
    cvGetMinMaxHistValue( hist, &min_val, &max_val, 0, 0 );  // 只找最大值
	printf("max_val:%.4f,min:%.4f\n",max_val,min_val);
cvConvertScale( hist->bins, hist->bins, max_val ? 255. / max_val : 0., 0 ); // 缩放 bin 到区间 [0,255] 
    cvZero( histimg );
    bin_w = histimg->width / hbins;  // hbins: 条的个数,则 bin_w 为条的宽度
    
    // 画直方图
    for( i = 0; i < hbins; i++ )
    {
        double val = ( cvGetReal1D(hist->bins,i)/*255份中的几份*/*histimg->height/maxcount );
		
        CvScalar color = CV_RGB(255,255,0); //(hsv2rgb(i*180.f/hbins);
        cvRectangle( histimg, cvPoint(i*bin_w,histimg->height),
            cvPoint((i+1)*bin_w,(int)(histimg->height - val)),
            color, 1, 8, 0 );
		printf("(%d,%d),(%d,%d)\n",i*bin_w,histimg->height,(i+1)*bin_w,(int)(histimg->height - val));
    }
    
    cvShowImage( "src", src);
    cvShowImage( "Histogram", histimg );
    cvWaitKey(0);

    cvDestroyWindow("src");
    cvDestroyWindow("Histogram");
    cvReleaseImage( &src );
    cvReleaseImage( &histimg );
    cvReleaseHist ( &hist );
    
    return 0; 
}
/*!
    \fn CvGaborFeature::_XM2VTSBin(const char *pathname, int possub, const CvMat *index) const
 */
CvTrainingData* CvGaborFeature::_XM2VTSBin(const char *pathname, int possub, const CvMat *index) const
{
    int nopic = 4;
    int nosub = 200; 
    CvSize size = cvGetSize(index);
    int numpos = size.width;
    int numneg = (nosub-1)*4;
    int numsample = numneg + numpos;
    CvTrainingData *bindata = new CvTrainingData; 
    bindata->init(2, numsample, 1);
    CvMat *mat = cvCreateMat(numsample, 1, CV_32FC1);
    double v;
    int n = 0;
    int begin = (possub -1)*4;
    int end = (possub -1)*4+numpos;

    while(n < numsample)
    {
      int i, j;
      if ( n < begin )
      {
	i = (int)(n/4) + 1;
        j = (int)fmod((double)n,(double)4) + 1;
        v = XM2VTSdata(pathname, i, j);
        cvSetReal1D(mat, n, v);
	bindata->setclsidxofsample(2, n);
      }
      else if ((n >= begin) && ( n < end))   //postive samples
      {
	i = possub;
        j = n - begin + 1;
	v = XM2VTSdata(pathname, i, (int)cvGetReal1D(index,(j-1)));
	cvSetReal1D(mat, n, v);
	bindata->setclsidxofsample(1, n);

      }
      else  //negative
      {
	i = (int)((n - end)/4) + possub + 1;
        j = (int)fmod((double)(n-end),(double)4)+1;
	v = XM2VTSdata(pathname, i, j);
        cvSetReal1D(mat, n, v);
	bindata->setclsidxofsample(2, n);

      }
      n++;
    }
    bindata->setdata(mat);
    cvReleaseMat(&mat);
    bindata->statclsdist();
    return bindata;


}
Example #14
0
size_t calcularHistograma(IplImage *src, size_t *binsCount, size_t **bins) {
	if (src == NULL || bins == NULL) {
		return -1;
	}
	static int _trueChannels = 1;
	static int _trueChannel = 1;
	int channels = src->nChannels;
	int hist_size = 256;
	if (*bins == NULL) {
		*bins = (size_t *)calloc(channels * hist_size, sizeof(size_t));
		*binsCount = channels;
	}
	//if (channels == 3) printf("%d (%p)", *binsCount, *bins);

	//Actuo en funcion la cantidad de colores de la imagen
	if (channels == 1) {
		float range[] = { 0, 256 };
		float* ranges[] = { range };

		CvHistogram *hist_bw = cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1);
		cvCalcHist(&src, hist_bw, 0, NULL);

		for (int i = 0; i < hist_size; i++) {
			(*bins)[_trueChannel * hist_size + i] = cvRound(cvGetReal1D(hist_bw->bins, i));
		}

		cvReleaseHist(&hist_bw);
	} else if (src->nChannels == 3) {
		_trueChannels = 3;
		IplImage *channelA = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
		IplImage *channelB = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
		IplImage *channelC = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
		cvSplit(src, channelA, channelB, channelC, NULL);

		_trueChannel = 0;
		calcularHistograma(channelA, binsCount, bins);
		cvReleaseImage(&channelA);

		_trueChannel = 1;
		calcularHistograma(channelB, binsCount, bins);
		cvReleaseImage(&channelB);

		_trueChannel = 2;
		calcularHistograma(channelC, binsCount, bins);
		cvReleaseImage(&channelC);

		_trueChannels = 1;
		_trueChannel = 1;
	} else {
		return -1;
	}
	return 0;
}
Example #15
0
size_t calcularHistograma(IplImage *src, size_t *binsCount, size_t **bins) {
	if (src == NULL || bins == NULL) {
		return -1;
	}

	int channels = src->nChannels;
	int hist_size = 256;
	if (*bins == NULL) {
		*bins = (size_t*)calloc(channels * hist_size, sizeof(size_t));
		*binsCount = channels;
	}
	//if (channels == 3) printf("%d (%p)", *binsCount, *bins);

	//Actúo en función de la cantidad de colores de la imágen
	if (channels == 1) {
		float range[] = { 0, 256 };
		float* ranges[] = { range };

		float max = 0.0;
		float w_scale = 0.0;

		CvHistogram *hist_bw = cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1);
		cvCalcHist(&src, hist_bw, 0, NULL);
		//float max_value = 0.0;
		//cvGetMinMaxHistValue(hist_bw, 0, &max_value, 0, 0);
		//cvScale(hist_bw->bins, hist_bw->bins, (float)(src->width*src->height) / max_value, 0);

		for (int i = 1; i < hist_size; i++) {
			(*bins)[(*binsCount - 1) * hist_size + i] = cvRound(cvGetReal1D(hist_bw->bins, i));
		}

		cvReleaseHist(&hist_bw);
	} else if (src->nChannels == 3) {

		IplImage *channelA = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
		IplImage *channelB = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
		IplImage *channelC = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
		cvSplit(src, channelA, channelB, channelC, NULL);

		calcularHistograma(channelA, binsCount, bins);
		cvReleaseImage(&channelA);

		calcularHistograma(channelB, binsCount, bins);
		cvReleaseImage(&channelB);

		calcularHistograma(channelC, binsCount, bins);
		cvReleaseImage(&channelC);
	} else {
		return -1;
	}
	return 0;
}
int CvAdaptiveSkinDetector::Histogram::findCoverageIndex(double surfaceToCover, int defaultValue)
{
    double s = 0;
    for (int i = 0; i < HistogramSize; i++)
    {
        s += cvGetReal1D( fHistogram->bins, i );
        if (s >= surfaceToCover)
        {
            return i;
        }
    }
    return defaultValue;
};
Example #17
0
IplImage* th_plot_hist(CvHistogram* hist, int bins, const char* windowName, CvScalar lineColor) {
	th_create_image(&histImg, cvSize(512, 512 * 0.6), 8, 3);
	float xStep = histImg->width / bins;
	CvPoint p0, p1;
	cvSet(histImg, th_black, 0x0);

	float max_value = 0;
	cvGetMinMaxHistValue(hist, 0, &max_value, 0, 0);

	int i;
	for (i = 1; i < bins; i++) {
		float v0 = cvGetReal1D(hist->bins, i - 1) / max_value * histImg->height;
		float v1 = cvGetReal1D(hist->bins, i) / max_value * histImg->height;
		p0 = cvPoint((i - 1) * xStep, histImg->height - v0);
		p1 = cvPoint(i * xStep, histImg->height - v1);
		cvLine(histImg, p0, p1, lineColor, 2, 8, 0);
	}

	//cvhPutText(histImg, "hello", cvPoint(33, 33), cvScalar(0xff, 0, 0xff, 0));
	cvShowImage(windowName, histImg);
	return histImg;
}
IplImage* CamShiftPatch::drawHistImg(CvRect selectROI, CvScalar maskRange)
{
	IplImage* hue = 0;
	hue = cvCreateImage(cvGetSize(originImage), 8, 1);
	IplImage *mask = getInRangeMask(maskRange, hue);//CvScalar 0->Vmin  1->Vmax  2->Smin

	//---設定ROI和畫出直方圖---

	float max_val = 0.f;
	cvSetImageROI(hue, selectROI);
	cvSetImageROI(mask, selectROI);

	
	int hdims = 48; //劃分HIST的個數,越高越精確 
	float hranges_arr[] = { 0, 180 }; //直方圖範圍
	float* hranges = hranges_arr;//指向值方圖範圍
	histogram = cvCreateHist(1, &hdims, CV_HIST_ARRAY, &hranges, 1); //設定直方圖的格式
	cvCalcHist(&hue, histogram, 0, mask); //以hue資訊 得到直方圖 (只有ROI部分的資訊)
	cvGetMinMaxHistValue(histogram, 0, &max_val, 0, 0); //只找最大值 
	cvConvertScale(histogram->bins, histogram->bins, max_val ? 255. / max_val : 0., 0); //縮放bin到區間[0,255]  
	//input      output        尺度放大或縮小         全部顏色的增減       
	cvResetImageROI(hue); // remove ROI  
	cvResetImageROI(mask);
	//track_window = selectROI; //搜索窗即一開始ROI框

	//----畫直方圖hist to histimg------------------

	IplImage* histimg = cvCreateImage(cvSize(320, 200), 8, 3);  //直方圖顯示空間,三通道
	cvZero(histimg); //置背景為黑色

	int bin_w = 0;
	bin_w = histimg->width / hdims; // hdims:條的個數,則bin_w為條的寬度  

	for (int i = 0; i < hdims; i++)
	{
		int val = cvRound(cvGetReal1D(histogram->bins, i)*histimg->height / 255);  //cvGetReal1D(直方條高度,第幾條) 
		CvScalar color = hsv2rgb(i*180.f / hdims); //在RGB空間上,直方圖每條的顏色計算   180/hdims表示每一格多寬 , i*180/hdims代表到橫軸的哪個位置了 由那個位置的顏色資訊轉換成RGB  
		cvRectangle(histimg, cvPoint(i*bin_w, histimg->height), cvPoint((i + 1)*bin_w, histimg->height - val), color, -1, 8, 0);//畫出不同顏色的直方長條來
	}
	//---設定ROI和畫出直方圖 end---
	IplImage* returnImg = nullptr;
	returnImg = cvCloneImage(histimg);

	cvReleaseImage(&hue);
	cvReleaseImage(&mask);
	cvReleaseImage(&histimg);

	return returnImg;
}
Example #19
0
void COpenCVMFCView::OnImageHistogram()
{
	// TODO: Add your command handler code here

	IplImage *src;
	IplImage *histimg = 0;
	CvHistogram *hist = 0;

	int hdims = 256;     // Divide Number of HIST, the more the accurator
	float hranges_arr[] = {0,255};
	float* hranges = hranges_arr;
	int bin_w;  
	float max_val;
	int i;

	src = workImg;

	cvNamedWindow( "Histogram", 0 );

	hist = cvCreateHist( 1, &hdims, CV_HIST_ARRAY, &hranges, 1 );  // Create Hist
	histimg = cvCreateImage( cvSize(320,200), 8, 3 );
	cvZero( histimg );
	cvCalcHist( &src, hist, 0, 0 ); // Caculate Hist
	cvGetMinMaxHistValue( hist, 0, &max_val, 0, 0 );  // just wanna the Max
	cvConvertScale( hist->bins, hist->bins, 
		max_val ? 255. / max_val : 0., 0 ); // resize bin to [0,255] 
	cvZero( histimg );
	bin_w = histimg->width / hdims;

	// Draw It
	for( i = 0; i < hdims; i++ )
	{
		double val = ( cvGetReal1D(hist->bins,i)*histimg->height/255 );
		CvScalar color = CV_RGB(255,255,0); //(hsv2rgb(i*180.f/hdims);
		cvRectangle( histimg, cvPoint(i*bin_w,histimg->height),
			cvPoint((i+1)*bin_w,(int)(histimg->height - val)),
			color, 1, 8, 0 );
	}

	cvShowImage( "Histogram", histimg );

	cvReleaseImage( &histimg );
	cvReleaseHist ( &hist );
	cvWaitKey(0);

	cvDestroyWindow("Histogram");
}
Example #20
0
void Widget::zhifangtu(IplImage *hist_img)
{
    IplImage *histimg = 0;
    CvHistogram *hist = 0;                    //直方图
    int hdims=255;
     float hranges_arr[] = {0,255};
        float* hranges = hranges_arr;
        int bin_w;
        float max_val,min_val;
        int i;

        cvNamedWindow( "Histogram", 0 );
//        cvNamedWindow( "src", 0);
hist = cvCreateHist( 1, &hdims, CV_HIST_ARRAY, &hranges, 1 );   //创建直方图
histimg = cvCreateImage( cvSize(320,200), 8, 3 );
    cvZero( histimg );                                            //清零

    cvCalcHist( &hist_img, hist, 0, 0 );                               // 计算直方图,即每个bin的大小
    cvGetMinMaxHistValue( hist, &min_val, &max_val, 0, 0 );              // 只找最大值
    cvThreshHist(hist,50);
//qDebug("max:%4.2f",max_val);
//qDebug("min:%4.2f",min_val);
 cvConvertScale( hist->bins,hist->bins, max_val ? 255. / max_val : 0., 0 );             // 缩放 bin 到区间 [0,255]

    cvZero( histimg );
    bin_w = histimg->width / hdims;                               // hdims: 直方图竖条的个数,则 bin_w 为条的宽度

    // 画直方图
    for( i = 0; i < hdims; i++ )
    {
        double val = ( cvGetReal1D(hist->bins,i)*histimg->height/255 );
        CvScalar color = CV_RGB(255,255,0);                                 //(hsv2rgb(i*180.f/hdims);
        cvRectangle( histimg, cvPoint(i*bin_w,histimg->height),
            cvPoint((i+1)*bin_w,(int)(histimg->height - val)),
            color, 1, 8, 0 );
    }

//    cvShowImage( "src", hue);
        cvShowImage( "Histogram", histimg );
cv::waitKey(0);
//    cvDestroyWindow("src");
    cvDestroyWindow("Histogram");
    //cvReleaseImage( &src );
    cvReleaseImage( &histimg );
    cvReleaseHist ( &hist );
}
Example #21
0
void HistgramEqualization(IplImage* src,IplImage* dst)
{
    CvHistogram *hist = 0;
    const HDIM=256;
    int n = HDIM;     
    double nn[HDIM];
    uchar T[HDIM];
    CvMat *T_mat;
    
    int x;
    int sum = 0; // sum of pixels of the source image 图像中象素点的总和
    double val = 0;

    // calculate histgram 计算直方图
    hist = cvCreateHist( 1, &n, CV_HIST_ARRAY, 0, 1 );  
    cvCalcHist( &src, hist, 0, 0 ); 
    
    // Create Accumulative Distribute Function of histgram
    val = 0;
    for ( x = 0; x < n; x++)
    {
        val = val + cvGetReal1D (hist->bins, x);
        nn[x] = val;
    }

    // Compute intensity transformation 计算变换函数的离散形式
    sum = src->height * src->width;
    for( x = 0; x < n; x++ )
    {
        T[x] = (uchar) (255 * nn[x] / sum); // range is [0,255]
    }

    // Do intensity transform for source image
	cvCopyImage(src, dst);
    T_mat = cvCreateMatHeader( 1, 256, CV_8UC1 );
    cvSetData( T_mat, T, 0 );    
    // directly use look-up-table function 直接调用内部函数完成 look-up-table 的过程
    cvLUT( src, dst, T_mat ); 
    cvReleaseHist ( &hist );
}
void CvAdaptiveSkinDetector::Histogram::findCurveThresholds(int &x1, int &x2, double percent)
{
    double sum = 0;

    for (int i = 0; i < HistogramSize; i++)
    {
        sum += cvGetReal1D( fHistogram->bins, i );
    }

    x1 = findCoverageIndex(sum * percent, -1);
    x2 = findCoverageIndex(sum * (1-percent), -1);

    if (x1 == -1)
        x1 = GSD_HUE_LT;
    else
        x1 += GSD_HUE_LT;

    if (x2 == -1)
        x2 = GSD_HUE_UT;
    else
        x2 += GSD_HUE_LT;
};
Example #23
0
// A Simple Camera Capture Framework
int main() {

	CvCapture* capture = cvCaptureFromCAM( 0 );
	if( !capture ) {
		fprintf( stderr, "ERROR: capture is NULL \n" );
		return -1;
	}

	#ifdef HALF_SIZE_CAPTURE
	cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 352/2);
	cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 288/2);
	#endif

	// Create a window in which the captured images will be presented
	cvNamedWindow( "Source Image Window", CV_WINDOW_AUTOSIZE );
	cvNamedWindow( "Back Projected Image", CV_WINDOW_AUTOSIZE );
	cvNamedWindow( "Brightness and Contrast Window", CV_WINDOW_AUTOSIZE );
	cvNamedWindow( "Blob Output Window", CV_WINDOW_AUTOSIZE );
	cvNamedWindow( "Histogram Window", 0);

	cvNamedWindow( "Rainbow Window", CV_WINDOW_AUTOSIZE );

	// Capture one frame to get image attributes:
	source_frame = cvQueryFrame( capture );
	if( !source_frame ) {
		fprintf( stderr, "ERROR: frame is null...\n" );
		return -1;
	}

	cvCreateTrackbar("histogram\nnormalization", "Back Projected Image", &normalization_sum, 6000, NULL);
	cvCreateTrackbar("brightness", "Brightness and Contrast Window", &_brightness, 200, NULL);
	cvCreateTrackbar("contrast", "Brightness and Contrast Window", &_contrast, 200, NULL);
	cvCreateTrackbar("threshold", "Blob Output Window", &blob_extraction_threshold, 255, NULL);
	cvCreateTrackbar("min blob size", "Blob Output Window", &min_blob_size, 2000, NULL);
	cvCreateTrackbar("max blob size", "Blob Output Window", &max_blob_size, source_frame->width*source_frame->height/4, NULL);



	inputImage = cvCreateImage(cvGetSize(source_frame), IPL_DEPTH_8U, 1);
	histAdjustedImage = cvCreateImage(cvGetSize(source_frame), IPL_DEPTH_8U, 1);
	outputImage = cvCreateImage(cvGetSize(source_frame), IPL_DEPTH_8U, 3 );
	hist_image = cvCreateImage(cvSize(320,200), 8, 1);

	rainbowImage = cvCreateImage(cvGetSize(source_frame), IPL_DEPTH_8U, 3 );


	// object that will contain blobs of inputImage 
	CBlobResult blobs;
	CBlob my_enumerated_blob;

	cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX|CV_FONT_ITALIC, hScale, vScale, 0, lineWidth);





	// Some brightness/contrast stuff:
	bright_cont_image = cvCloneImage(inputImage);
	lut_mat = cvCreateMatHeader( 1, 256, CV_8UC1 );
	cvSetData( lut_mat, lut, 0 );




	while( 1 ) {

		// Get one frame
		source_frame = cvQueryFrame( capture );
		if( !source_frame ) {
			fprintf( stderr, "ERROR: frame is null...\n" );
			getchar();
			break;
		}
		cvShowImage( "Source Image Window", source_frame );
		// Do not release the frame!

		cvCvtColor(source_frame, inputImage, CV_RGB2GRAY);

		// Histogram Stuff!
		my_hist = cvCreateHist(1, hist_size_array, CV_HIST_ARRAY, ranges, 1);
		cvCalcHist( &inputImage, my_hist, 0, NULL );
		cvNormalizeHist(my_hist, normalization_sum);

		// NOTE: First argument MUST have an ampersand, or a segmentation fault will result
		cvCalcBackProject(&inputImage, histAdjustedImage, my_hist);



		// Histogram Picture
		int bin_w;
		float max_value = 0;
		cvGetMinMaxHistValue( my_hist, 0, &max_value, 0, 0 );
		cvScale( my_hist->bins, my_hist->bins, ((double)hist_image->height)/max_value, 0 );
		cvSet( hist_image, cvScalarAll(255), 0 );
		bin_w = cvRound((double)hist_image->width/hist_size);

		for(int i = 0; i < hist_size; i++ )
			cvRectangle( hist_image, cvPoint(i*bin_w, hist_image->height), cvPoint((i+1)*bin_w, hist_image->height - cvRound(cvGetReal1D(my_hist->bins,i))), cvScalarAll(0), -1, 8, 0 );
		cvShowImage( "Histogram Window", hist_image );
		cvShowImage("Back Projected Image", histAdjustedImage);






		// Brightness/contrast loop stuff:
		int brightness = _brightness - 100;
		int contrast = _contrast - 100;

		/*
		 * The algorithm is by Werner D. Streidt
		 * (http://visca.com/ffactory/archives/5-99/msg00021.html)
		 */
		if( contrast > 0 ) {
			double delta = 127.*contrast/100;
			double a = 255./(255. - delta*2);
			double b = a*(brightness - delta);
			for(int i = 0; i < 256; i++ )
			{
				int v = cvRound(a*i + b);
				if( v < 0 ) v = 0;
				if( v > 255 ) v = 255;
				lut[i] = (uchar)v;
			}
		}
		else {
			double delta = -128.*contrast/100;
			double a = (256.-delta*2)/255.;
			double b = a*brightness + delta;
			for(int i = 0; i < 256; i++ ) {
				int v = cvRound(a*i + b);
				if( v < 0 )
					v = 0;
				if( v > 255 )
					v = 255;
				lut[i] = (uchar)v;
			}
		}

		cvLUT( inputImage, bright_cont_image, lut_mat );
		cvShowImage( "Brightness and Contrast Window", bright_cont_image);







		// ---------------
		// Blob Manipulation Code begins here:

		// Extract the blobs using a threshold of 100 in the image
		blobs = CBlobResult( bright_cont_image, NULL, blob_extraction_threshold, true );

		// discard the blobs with less area than 5000 pixels
		// ( the criteria to filter can be any class derived from COperadorBlob ) 
		blobs.Filter( blobs, B_INCLUDE, CBlobGetArea(), B_GREATER_OR_EQUAL, min_blob_size);
		blobs.Filter( blobs, B_EXCLUDE, CBlobGetArea(), B_GREATER, max_blob_size);

		// build an output image equal to the input but with 3 channels (to draw the coloured blobs)
		cvMerge( bright_cont_image, bright_cont_image, bright_cont_image, NULL, outputImage );

		// plot the selected blobs in a output image
		for (int i=0; i < blobs.GetNumBlobs(); i++) {
			blobs.GetNthBlob( CBlobGetArea(), i, my_enumerated_blob );
			// Color 5/6 of the color wheel (300 degrees)
			my_enumerated_blob.FillBlob( outputImage, cv_hsv2rgb((float)i/blobs.GetNumBlobs() * 300, 1, 1));
		}
		// END Blob Manipulation Code
		// ---------------


		sprintf(str, "Count: %d", blobs.GetNumBlobs());
		cvPutText(outputImage, str, cvPoint(50, 25), &font, cvScalar(255,0,255));
		
		cvShowImage("Blob Output Window", outputImage);






/*
		// Rainbow manipulation:
		for (int i=0; i < CV_CAP_PROP_FRAME_WIDTH; i++) {
			for (int j=0; j < CV_CAP_PROP_FRAME_HEIGHT; j++) {
// This line is not figure out yet...
//				pixel_color_set = ((uchar*)(rainbowImage->imageData + rainbowImage->widthStep * j))[i * 3]

				((uchar*)(rainbowImage->imageData + rainbowImage->widthStep * j))[i * 3] = 30;
				((uchar*)(rainbowImage->imageData + rainbowImage->widthStep * j))[i * 3 + 1] = 30;
				((uchar*)(rainbowImage->imageData + rainbowImage->widthStep * j))[i * 3 + 2] = 30;
			}
		}
		cvShowImage("Rainbow Window", rainbowImage);
*/







		//If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
		//remove higher bits using AND operator
		if( (cvWaitKey(10) & 255) == 27 ) break;

	}

	cvReleaseImage(&inputImage);
	cvReleaseImage(&histAdjustedImage);
	cvReleaseImage(&hist_image);
	cvReleaseImage(&bright_cont_image);
	cvReleaseImage(&outputImage);
	cvReleaseImage(&rainbowImage);

	// Release the capture device housekeeping
	cvReleaseCapture( &capture );
	cvDestroyAllWindows();

	return 0;
}
Example #24
0
double cveGetReal1D(CvArr* arr, int idx0)
{
   return cvGetReal1D(arr, idx0);
}
Example #25
0
CvBox2D CamShiftIris::track( IplImage* image, CvRect selection, bool isIris){
	CamShiftIris camshift;
	select_object1=1;
	track_object1=-1;
	origin1=cvPoint(0,0);

///////////////////////////////

	int i, bin_w, c;
	//frame = cvQueryFrame( capture );

//
//	frame=cvCloneImage(image);
//        if( !frame )
//            return 0;
	if( image ){
		/* allocate all the buffers */
//		image = cvCreateImage( cvGetSize(frame), 8, 3 );
//		image->origin = frame->origin;
		hsv1 = cvCreateImage( cvGetSize(image), 8, 3 );
		h = cvCreateImage( cvGetSize(image), 8, 1 );
		s = cvCreateImage( cvGetSize(image), 8, 1 );
		v = cvCreateImage( cvGetSize(image), 8, 1);
		hue1 = cvCreateImage( cvGetSize(image), 8, 1 );
		mask1 = cvCreateImage( cvGetSize(image), 8, 1 );
		backproject1 = cvCreateImage( cvGetSize(image), 8, 1 );
		hist1= cvCreateHist( 1, &hdims1, CV_HIST_ARRAY, &hranges1, 1 );
		histimg1 = cvCreateImage( cvSize(320,200), 8, 3 );
		cvZero( histimg1 );
	}
	cvCvtColor( image, hsv1, CV_BGR2HSV );
	///////////////////Equalize v in hsv///////////
	cvSplit( hsv1, h, s, v, 0 );
	cvEqualizeHist(v,v);
	cvMerge(h,s,v,0,hsv1);
	///////////////////Equalize v in hsv///////////

	if( track_object1 !=0 ){
		int _vmin1 = vmin1, _vmax1 = vmax1;

		cvInRangeS( hsv1, cvScalar(0,smin1,MIN(_vmin1,_vmax1),0),
					cvScalar(180,256,MAX(_vmin1,_vmax1),0), mask1 );
		cvSplit( hsv1, hue1, 0, 0, 0 );

		if( track_object1 < 0 ){
			float max_val = 0.f;
			cvSetImageROI( hue1, selection );
			cvSetImageROI( mask1, selection );
			cvCalcHist( &hue1, hist1, 0, mask1 );
			cvGetMinMaxHistValue( hist1, 0, &max_val, 0, 0 );
			cvConvertScale( hist1->bins, hist1->bins, max_val ? 255. / max_val : 0., 0 );
			cvResetImageROI( hue1 );
			cvResetImageROI( mask1 );
			track_window1 = selection;
			track_object1 = 1;

			cvZero( histimg1 );
			bin_w = histimg1->width / hdims1;
			for( i = 0; i < hdims1; i++ )
			{
				int val = cvRound( cvGetReal1D(hist1->bins,i)*histimg1->height/255 );
				CvScalar color = camshift.hsvrgb(i*180.f/hdims1);
				cvRectangle( histimg1, cvPoint(i*bin_w,histimg1->height),
							 cvPoint((i+1)*bin_w,histimg1->height - val),
							 color, -1, 8, 0 );
			}
		}
		cvCalcBackProject( &hue1, backproject1, hist1);
		cvAnd( backproject1, mask1, backproject1, 0 );
		try{
		cvCamShift( backproject1, track_window1,
					cvTermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 ),
					&track_comp1, &track_box1 );
		}catch(...){
			cvReleaseImage(&hsv1);
			cvReleaseImage(&h);
			cvReleaseImage(&s);
			cvReleaseImage(&v);
			cvReleaseImage(&hue1);
			cvReleaseImage(&mask1);
			cvReleaseImage(&backproject1);
			cvReleaseHist(&hist1);
			cvReleaseImage(&histimg1);
		}
		track_window1 = track_comp1.rect;
		if( backproject1_mode )
			cvCvtColor( backproject1, image, CV_GRAY2BGR );
		if( !image->origin )
			track_box1.angle = -track_box1.angle;
		if(isIris)
			cvEllipseBox( image, track_box1, CV_RGB(255,0,0), 3, CV_AA, 0 );
	}
	cvShowImage( "CamShift Tracking", image );
	//cvShowImage( "Histogram", histimg1 );

//	c = cvWaitKey(10);
//	if( (char) c == 27 )
//		cout<<"esc pressed";
//		//return; //break;
//	switch( (char) c ){
//	case 'b':
//		backproject1_mode ^= 1;
//		break;
//	case 'c':
//		track_object1 = 0;
//		cvZero( histimg1 );
//		break;
//	case 'h':
//		show_hist1^= 1;
//		if( !show_hist1)
//			cvDestroyWindow( "Histogram" );
//		else
//			cvNamedWindow( "Histogram", 1 );
//		break;
//	default:
//		;
//	}

	//cvReleaseImage(&image);
	cvReleaseImage(&hsv1);
	cvReleaseImage(&h);
	cvReleaseImage(&s);
	cvReleaseImage(&v);
	cvReleaseImage(&hue1);
	cvReleaseImage(&mask1);
	cvReleaseImage(&backproject1);
	cvReleaseHist(&hist1);
	cvReleaseImage(&histimg1);

    return track_box1;
}
bool
cvFindExtrinsicCameraParams3( const CvMat* obj_points,
                  const CvMat* img_points, const CvMat* A,
                  const CvMat* dist_coeffs,
                  CvMat* r_vec, CvMat* t_vec )
{
    bool fGood = true;

    const int max_iter = 20;
    CvMat *_M = 0, *_Mxy = 0, *_m = 0, *_mn = 0, *_L = 0, *_J = 0;
    
    CV_FUNCNAME( "cvFindExtrinsicCameraParams3" );

    __BEGIN__;

    int i, j, count;
    double a[9], k[4] = { 0, 0, 0, 0 }, R[9], ifx, ify, cx, cy;
    double Mc[3] = {0, 0, 0}, MM[9], U[9], V[9], W[3];
    double JtJ[6*6], JtErr[6], JtJW[6], JtJV[6*6], delta[6], param[6];
    CvPoint3D64f* M = 0;
    CvPoint2D64f *m = 0, *mn = 0;
    CvMat _a = cvMat( 3, 3, CV_64F, a );
    CvMat _R = cvMat( 3, 3, CV_64F, R );
    CvMat _r = cvMat( 3, 1, CV_64F, param );
    CvMat _t = cvMat( 3, 1, CV_64F, param + 3 );
    CvMat _Mc = cvMat( 1, 3, CV_64F, Mc );
    CvMat _MM = cvMat( 3, 3, CV_64F, MM );
    CvMat _U = cvMat( 3, 3, CV_64F, U );
    CvMat _V = cvMat( 3, 3, CV_64F, V );
    CvMat _W = cvMat( 3, 1, CV_64F, W );
    CvMat _JtJ = cvMat( 6, 6, CV_64F, JtJ );
    CvMat _JtErr = cvMat( 6, 1, CV_64F, JtErr );
    CvMat _JtJW = cvMat( 6, 1, CV_64F, JtJW );
    CvMat _JtJV = cvMat( 6, 6, CV_64F, JtJV );
    CvMat _delta = cvMat( 6, 1, CV_64F, delta );
    CvMat _param = cvMat( 6, 1, CV_64F, param );
    CvMat _dpdr, _dpdt;

    if( !CV_IS_MAT(obj_points) || !CV_IS_MAT(img_points) ||
        !CV_IS_MAT(A) || !CV_IS_MAT(r_vec) || !CV_IS_MAT(t_vec) )
        CV_ERROR( CV_StsBadArg, "One of required arguments is not a valid matrix" );

    count = MAX(obj_points->cols, obj_points->rows);
    CV_CALL( _M = cvCreateMat( 1, count, CV_64FC3 ));
    CV_CALL( _Mxy = cvCreateMat( 1, count, CV_64FC2 ));
    CV_CALL( _m = cvCreateMat( 1, count, CV_64FC2 ));
    CV_CALL( _mn = cvCreateMat( 1, count, CV_64FC2 ));
    M = (CvPoint3D64f*)_M->data.db;
    m = (CvPoint2D64f*)_m->data.db;
    mn = (CvPoint2D64f*)_mn->data.db;

    CV_CALL( cvConvertPointsHomogenious( obj_points, _M ));
    CV_CALL( cvConvertPointsHomogenious( img_points, _m ));
    CV_CALL( cvConvert( A, &_a ));

    if( dist_coeffs )
    {
        CvMat _k;
        if( !CV_IS_MAT(dist_coeffs) ||
            CV_MAT_DEPTH(dist_coeffs->type) != CV_64F &&
            CV_MAT_DEPTH(dist_coeffs->type) != CV_32F ||
            dist_coeffs->rows != 1 && dist_coeffs->cols != 1 ||
            dist_coeffs->rows*dist_coeffs->cols*CV_MAT_CN(dist_coeffs->type) != 4 )
            CV_ERROR( CV_StsBadArg,
                "Distortion coefficients must be 1x4 or 4x1 floating-point vector" );

        _k = cvMat( dist_coeffs->rows, dist_coeffs->cols,
                    CV_MAKETYPE(CV_64F,CV_MAT_CN(dist_coeffs->type)), k );
        CV_CALL( cvConvert( dist_coeffs, &_k ));
    }

    if( CV_MAT_DEPTH(r_vec->type) != CV_64F && CV_MAT_DEPTH(r_vec->type) != CV_32F ||
        r_vec->rows != 1 && r_vec->cols != 1 ||
        r_vec->rows*r_vec->cols*CV_MAT_CN(r_vec->type) != 3 )
        CV_ERROR( CV_StsBadArg, "Rotation vector must be 1x3 or 3x1 floating-point vector" );

    if( CV_MAT_DEPTH(t_vec->type) != CV_64F && CV_MAT_DEPTH(t_vec->type) != CV_32F ||
        t_vec->rows != 1 && t_vec->cols != 1 ||
        t_vec->rows*t_vec->cols*CV_MAT_CN(t_vec->type) != 3 )
        CV_ERROR( CV_StsBadArg,
            "Translation vector must be 1x3 or 3x1 floating-point vector" );

    ifx = 1./a[0]; ify = 1./a[4];
    cx = a[2]; cy = a[5];

    // normalize image points
    // (unapply the intrinsic matrix transformation and distortion)
    for( i = 0; i < count; i++ )
    {
        double x = (m[i].x - cx)*ifx, y = (m[i].y - cy)*ify, x0 = x, y0 = y;

        // compensate distortion iteratively
        if( dist_coeffs )
            for( j = 0; j < 5; j++ )
            {
                double r2 = x*x + y*y;
                double icdist = 1./(1 + k[0]*r2 + k[1]*r2*r2);
                double delta_x = 2*k[2]*x*y + k[3]*(r2 + 2*x*x);
                double delta_y = k[2]*(r2 + 2*y*y) + 2*k[3]*x*y;
                x = (x0 - delta_x)*icdist;
                y = (y0 - delta_y)*icdist;
            }
        mn[i].x = x; mn[i].y = y;

        // calc mean(M)
        Mc[0] += M[i].x;
        Mc[1] += M[i].y;
        Mc[2] += M[i].z;
    }

    Mc[0] /= count;
    Mc[1] /= count;
    Mc[2] /= count;

    cvReshape( _M, _M, 1, count );
    cvMulTransposed( _M, &_MM, 1, &_Mc );
    cvSVD( &_MM, &_W, 0, &_V, CV_SVD_MODIFY_A + CV_SVD_V_T );

    // initialize extrinsic parameters
    if( W[2]/W[1] < 1e-3 || count < 4 )
    {
        // a planar structure case (all M's lie in the same plane)
        double tt[3], h[9], h1_norm, h2_norm;
        CvMat* R_transform = &_V;
        CvMat T_transform = cvMat( 3, 1, CV_64F, tt );
        CvMat _H = cvMat( 3, 3, CV_64F, h );
        CvMat _h1, _h2, _h3;

        if( V[2]*V[2] + V[5]*V[5] < 1e-10 )
            cvSetIdentity( R_transform );

        if( cvDet(R_transform) < 0 )
            cvScale( R_transform, R_transform, -1 );

        cvGEMM( R_transform, &_Mc, -1, 0, 0, &T_transform, CV_GEMM_B_T );

        for( i = 0; i < count; i++ )
        {
            const double* Rp = R_transform->data.db;
            const double* Tp = T_transform.data.db;
            const double* src = _M->data.db + i*3;
            double* dst = _Mxy->data.db + i*2;

            dst[0] = Rp[0]*src[0] + Rp[1]*src[1] + Rp[2]*src[2] + Tp[0];
            dst[1] = Rp[3]*src[0] + Rp[4]*src[1] + Rp[5]*src[2] + Tp[1];
        }

        cvFindHomography( _Mxy, _mn, &_H );

        cvGetCol( &_H, &_h1, 0 );
        _h2 = _h1; _h2.data.db++;
        _h3 = _h2; _h3.data.db++;
        h1_norm = sqrt(h[0]*h[0] + h[3]*h[3] + h[6]*h[6]);
        h2_norm = sqrt(h[1]*h[1] + h[4]*h[4] + h[7]*h[7]);

        cvScale( &_h1, &_h1, 1./h1_norm );
        cvScale( &_h2, &_h2, 1./h2_norm );
        cvScale( &_h3, &_t, 2./(h1_norm + h2_norm));
        cvCrossProduct( &_h1, &_h2, &_h3 );

        cvRodrigues2( &_H, &_r );
        cvRodrigues2( &_r, &_H );
        cvMatMulAdd( &_H, &T_transform, &_t, &_t );
        cvMatMul( &_H, R_transform, &_R );
        cvRodrigues2( &_R, &_r );
    }
    else
    {
        // non-planar structure. Use DLT method
        double* L;
        double LL[12*12], LW[12], LV[12*12], sc;
        CvMat _LL = cvMat( 12, 12, CV_64F, LL );
        CvMat _LW = cvMat( 12, 1, CV_64F, LW );
        CvMat _LV = cvMat( 12, 12, CV_64F, LV );
        CvMat _RRt, _RR, _tt;

        CV_CALL( _L = cvCreateMat( 2*count, 12, CV_64F ));
        L = _L->data.db;

        for( i = 0; i < count; i++, L += 24 )
        {
            double x = -mn[i].x, y = -mn[i].y;
            L[0] = L[16] = M[i].x;
            L[1] = L[17] = M[i].y;
            L[2] = L[18] = M[i].z;
            L[3] = L[19] = 1.;
            L[4] = L[5] = L[6] = L[7] = 0.;
            L[12] = L[13] = L[14] = L[15] = 0.;
            L[8] = x*M[i].x;
            L[9] = x*M[i].y;
            L[10] = x*M[i].z;
            L[11] = x;
            L[20] = y*M[i].x;
            L[21] = y*M[i].y;
            L[22] = y*M[i].z;
            L[23] = y;
        }

        cvMulTransposed( _L, &_LL, 1 );
        cvSVD( &_LL, &_LW, 0, &_LV, CV_SVD_MODIFY_A + CV_SVD_V_T );
        _RRt = cvMat( 3, 4, CV_64F, LV + 11*12 );
        cvGetCols( &_RRt, &_RR, 0, 3 );
        cvGetCol( &_RRt, &_tt, 3 );
        if( cvDet(&_RR) < 0 )
            cvScale( &_RRt, &_RRt, -1 );
        sc = cvNorm(&_RR);
        cvSVD( &_RR, &_W, &_U, &_V, CV_SVD_MODIFY_A + CV_SVD_U_T + CV_SVD_V_T );
        cvGEMM( &_U, &_V, 1, 0, 0, &_R, CV_GEMM_A_T );
        cvScale( &_tt, &_t, cvNorm(&_R)/sc );
        cvRodrigues2( &_R, &_r );
        cvReleaseMat( &_L );
    }

    //
    // Check if new r and t are good
    //
    if ( cvGetReal1D( r_vec, 0 ) ||
         cvGetReal1D( r_vec, 1 ) ||
         cvGetReal1D( r_vec, 2 ) ||
         cvGetReal1D( t_vec, 0 ) ||
         cvGetReal1D( t_vec, 1 ) ||
         cvGetReal1D( t_vec, 2 ) )
    {
        //
        // perfom this only when the old r and t exist.
        //
        CvMat * R_inv = cvCreateMat( 3, 3, CV_64FC1 );
        CvMat * r_old = cvCreateMat( 3, 1, CV_64FC1 );
        CvMat * R_old = cvCreateMat( 3, 3, CV_64FC1 );
        CvMat * t_old = cvCreateMat( 3, 1, CV_64FC1 );
        // get new center
        cvInvert( &_R, R_inv );
        double new_center[3];
        CvMat newCenter = cvMat( 3, 1, CV_64FC1, new_center );
        cvMatMul( R_inv, &_t, &newCenter );
        cvScale( &newCenter, &newCenter, -1 );
        // get old center
        cvConvert( r_vec, r_old );
        cvConvert( t_vec, t_old );
        cvRodrigues2( r_old, R_old );
        cvInvert( R_old, R_inv );
        double old_center[3];
        CvMat oldCenter = cvMat( 3, 1, CV_64FC1, old_center );
        cvMatMul( R_inv, t_old, &oldCenter );
        cvScale( &oldCenter, &oldCenter, -1 );
        // get difference
        double diff_center = 0;
        for ( int i = 0; i < 3 ; i ++ )
            diff_center += pow( new_center[i] - old_center[i], 2 );
        diff_center = sqrt( diff_center );
        if ( diff_center > 300 )
        {
            printf("diff_center = %.2f --> set initial r and t as same as  the previous\n", diff_center);
            cvConvert(r_vec, &_r);
            cvConvert(t_vec, &_t);
            fGood = false;
        }
//        else printf("diff_center = %.2f\n", diff_center );
        
        cvReleaseMat( &R_inv );
        cvReleaseMat( &r_old );
        cvReleaseMat( &R_old );
        cvReleaseMat( &t_old );
    }
    
    if ( fGood )
    {
        CV_CALL( _J = cvCreateMat( 2*count, 6, CV_64FC1 ));
        cvGetCols( _J, &_dpdr, 0, 3 );
        cvGetCols( _J, &_dpdt, 3, 6 );

        // refine extrinsic parameters using iterative algorithm
        for( i = 0; i < max_iter; i++ )
        {
            double n1, n2;
            cvReshape( _mn, _mn, 2, 1 );
            cvProjectPoints2( _M, &_r, &_t, &_a, dist_coeffs,
                              _mn, &_dpdr, &_dpdt, 0, 0, 0 );
            cvSub( _m, _mn, _mn );
            cvReshape( _mn, _mn, 1, 2*count );

            cvMulTransposed( _J, &_JtJ, 1 );
            cvGEMM( _J, _mn, 1, 0, 0, &_JtErr, CV_GEMM_A_T );
            cvSVD( &_JtJ, &_JtJW, 0, &_JtJV, CV_SVD_MODIFY_A + CV_SVD_V_T );
            if( JtJW[5]/JtJW[0] < 1e-12 )
                break;
            cvSVBkSb( &_JtJW, &_JtJV, &_JtJV, &_JtErr,
                      &_delta, CV_SVD_U_T + CV_SVD_V_T );
            cvAdd( &_delta, &_param, &_param );
            n1 = cvNorm( &_delta );
            n2 = cvNorm( &_param );
            if( n1/n2 < 1e-10 )
                break;
        }

        _r = cvMat( r_vec->rows, r_vec->cols,
            CV_MAKETYPE(CV_64F,CV_MAT_CN(r_vec->type)), param );
        _t = cvMat( t_vec->rows, t_vec->cols,
            CV_MAKETYPE(CV_64F,CV_MAT_CN(t_vec->type)), param + 3 );

        cvConvert( &_r, r_vec );
        cvConvert( &_t, t_vec );
    }

    __END__;

    cvReleaseMat( &_M );
    cvReleaseMat( &_Mxy );
    cvReleaseMat( &_m );
    cvReleaseMat( &_mn );
    cvReleaseMat( &_L );
    cvReleaseMat( &_J );

    return fGood;
}
Example #27
0
int main(int argc, char **argv) {
IplImage *inputImg;
trainImage sample;  
cvNamedWindow("With COM", CV_WINDOW_AUTOSIZE);
// CvCapture* capture = 0;
// capture = cvCreateCameraCapture(-1);
// if(!capture){
// return -1;
// }
// inputImg = cvQueryFrame(capture);
#include <opencv2/ml/ml.hpp>
float result;
initializeFaceProcessor();
CvMat* SampleMatrix;
CvMat* PjMatrix=(CvMat*)cvLoad("/home/umut/projects/fastTrainer/build/ProjectionMatrix.xml");
int newDimension=PjMatrix->cols;
// int newDimension;
CvMat* allFeatures;
 CvMat* LDAMatrix=cvCreateMat(newDimension,1,CV_32F);
// CvBoost booster;
// 
// booster.load("/home/umut/projects/fastTrainer/build/Booster.dat");
int trans=CV_GEMM_A_T;

CvSVM SVM;
SVM.load("/home/umut/projects/fastTrainer/build/SVM_CLASS.dat");
// Grab the next frame from the camera.
// while((inputImg = cvQueryFrame(capture))  != NULL ){
for (int i=1;i<argc;i++){
    inputImg=cvLoadImage(argv[i]);


  
      if(processFace(inputImg,  sample.FaceImage, sample.MouthImage, sample.NoseImage, sample.EyeImage, 0))  
      {
	 sample.LBPHF=LBP_HF(sample.FaceImage,sample.nonUniform,sample.complete); //Pass through the LBPHF
// 	  sample.EyeImage=filterGabor(sample.EyeImage);
// 	  sample.NoseImage=filterGabor(sample.NoseImage);
// 	  sample.MouthImage=filterGabor(sample.MouthImage);
	  mat2Col(&sample,0,1,0,SampleMatrix);
	//  newDimension=SampleMatrix->rows;
	  allFeatures=cvCreateMat(1,35+2+newDimension,CV_32F);
	  cvGEMM(PjMatrix,SampleMatrix,1,NULL,0,LDAMatrix,trans);
	   cvSetReal1D(allFeatures,0,sample.complete);
	  cvSetReal1D(allFeatures,1,sample.nonUniform);      
	  for (int j=0;j<35;j++)
	    cvSetReal1D(allFeatures,2+j,sample.LBPHF[j]);
	  for (int j=0;j
	    <newDimension;j++)
	//    cvSetReal1D(allFeatures,37+j,cvGetReal1D(SampleMatrix,j));
	  cvSetReal1D(allFeatures,37+j,cvGetReal1D(LDAMatrix,j));
	//  cout<< "feature Size: "<< allFeatures->cols << "\n";
	//  result=booster.predict(allFeatures,0,booster.get_weak_response());
	  result=SVM.predict(allFeatures);
	  if (result==0)
	  {
	    cvRectangle(sample.FaceImage,cvPoint(2,2),cvPoint(sample.FaceImage->width-2,sample.FaceImage->height-2),cvScalar(255,0,0),3);
	    printf("Result is male\n");
	  }
	  else
	  {
	    cvRectangle(sample.FaceImage,cvPoint(2,2),cvPoint(sample.FaceImage->width-2,sample.FaceImage->height-2),cvScalar(0,0,255),3);
	    printf("Result is female\n");
	  }
	
      cvShowImage("With COM",sample.FaceImage);
     char c=cvWaitKey(0);
//      char c=cvWaitKey(5);
//       if (c==27) break;
 }
}
if (strcmp(argv[1],"1"))
cvReleaseImage( &inputImg);
}
void CTWithWater::imageAndDepthToWorld(double u, double v, double d,
                                       double* x, double* y, double* z,
                                       bool undistort)
{
    double xx, yy, t;
    CvMat* r = cvCreateMat(3, 1, CV_32FC1);

    if(undistort)
    {
        CvMat* I = cvCreateMat(1, 1, CV_32FC2);
        CvMat* Io = cvCreateMat(1, 1, CV_32FC2);
        cvSet2D(I, 0, 0, cvScalar(u,v));
        
        cvUndistortPoints(I, Io, m_CameraMatrix, m_DistCoeffs, NULL, m_CameraMatrixNorm);
        CvScalar s = cvGet2D(Io, 0, 0);
        
        xx = s.val[0];//cvGetReal1D(Io, 0);
        yy = s.val[1];//cvGetReal1D(Io, 1);
        
        cvReleaseMat(&I);
        cvReleaseMat(&Io);            
    }
    else
    {
        xx = u;
        yy = v;
    }

    xx = (xx - cvGetReal2D(m_CameraMatrixNorm, 0, 2))/cvGetReal2D(m_CameraMatrixNorm, 0, 0);
    yy = (yy - cvGetReal2D(m_CameraMatrixNorm, 1, 2))/cvGetReal2D(m_CameraMatrixNorm, 1, 1);

    cvSetReal1D(r, 0, xx); 
    cvSetReal1D(r, 1, yy);
    cvSetReal1D(r, 2, 1.0);

    /* Rt_(3,:)*r = sum of third column of R times elements of r */
    t = xx*cvGetReal2D(m_R, 0, 2) + yy*cvGetReal2D(m_R, 1, 2) + cvGetReal2D(m_R, 2, 2);
    if(t == 0)
    {
        t = 1.0;
    }

    if(d <= 0)
    {
        /* d<= 0 => above water surface */
        t = (-m_dCameraHeightAboveWater-d)/t;

        /* r = t*R'*r + C */
        cvGEMM(m_R, r, t, m_CameraWorld, 1.0, r, CV_GEMM_A_T);
    }
    else
    {
        /* d > 0 => below water surface */

        t = -m_dCameraHeightAboveWater/t;
        
        /* S = t*R'*r */
        cvGEMM(m_R, r, t, NULL, 0, m_S, CV_GEMM_A_T);

        double Sx = cvGetReal1D(m_S, 0);
        double Sy = cvGetReal1D(m_S, 1);    
        double phi = atan2(Sy, Sx);
        double rS = sqrt(Sx*Sx + Sy*Sy);

        double rP = calculateRpFromRs(rS, d, m_dCameraHeightAboveWater);
        cvSetReal1D(r, 0, rP*cos(phi));
        cvSetReal1D(r, 1, rP*sin(phi));
        cvSetReal1D(r, 2, -m_dCameraHeightAboveWater-d);

        cvAdd(r, m_CameraWorld, r);
    }

    *x = cvGetReal1D(r, 0);
    *y = cvGetReal1D(r, 1);
    *z = cvGetReal1D(r, 2);    
                           
    cvReleaseMat(&r);

}
void CTWithWater::worldToImage(double x, double y, double z,
                               double* u, double* v,
                               bool distort)
{
    double d = m_dWaterDepth - z;

    /* d > 0 => point is below surface of water */
    if(d > 0)
    {
        double phi = atan2(y - cvGetReal1D(m_CameraWorld, 1),
                           x - cvGetReal1D(m_CameraWorld, 0));
        double rP = sqrt((x-cvGetReal1D(m_CameraWorld, 0))
                         *(x-cvGetReal1D(m_CameraWorld, 0)) + 
                         (y-cvGetReal1D(m_CameraWorld, 1))
                         *(y-cvGetReal1D(m_CameraWorld, 1)));
        double rS = solveRsFromRp(rP, d, m_dCameraHeightAboveWater);
        cvSetReal1D(m_S, 0, rS*cos(phi));
        cvSetReal1D(m_S, 1, rS*sin(phi));
        cvSetReal1D(m_S, 2, -m_dCameraHeightAboveWater);

        /* S = CW + S */
        cvAdd(m_S, m_CameraWorld, m_S);
    }
    else
    {
        /* point is above water surface and can be used directly */
        cvSetReal1D(m_S, 0, x);
        cvSetReal1D(m_S, 1, y);
        cvSetReal1D(m_S, 2, z);
    }

    /* S = R*S + T */
    cvGEMM(m_R, m_S, 1.0, m_T, 1.0, m_S);

    /* in camera coordinats */
    x = cvGetReal1D(m_S, 0);
    y = cvGetReal1D(m_S, 1);
    z = cvGetReal1D(m_S, 2);

    if(z == 0)
    {
        z = 1.0;
    }

    x = x/z;
    y = y/z;

    if(distort)
    {
        double xx, yy, r2, k1, k2, k3, p1, p2;
        r2 = (x*x + y*y);
        k1 = cvGetReal1D(m_DistCoeffs, 0);
        k2 = cvGetReal1D(m_DistCoeffs, 1);
        p1 = cvGetReal1D(m_DistCoeffs, 2);
        p2 = cvGetReal1D(m_DistCoeffs, 3);
        k3 = cvGetReal1D(m_DistCoeffs, 4);        
        xx = x*(1 + k1*r2 + k2*r2*r2 + k3*r2*r2*r2) +
            2.0*p1*x*y + p2*(r2 + 2.0*x*x);
        yy = y*(1 + k1*r2 + k2*r2*r2 + k3*r2*r2*r2) +
            p1*(r2 + 2.0*y*y) + 2.0*p2*x*y;
        *u = cvGetReal2D(m_CameraMatrix, 0, 0)*x + cvGetReal2D(m_CameraMatrix, 0, 2);
        *v = cvGetReal2D(m_CameraMatrix, 1, 1)*y + cvGetReal2D(m_CameraMatrix, 1, 2);
    }
    else
    {
        *u = cvGetReal2D(m_CameraMatrixNorm, 0, 0)*x
            + cvGetReal2D(m_CameraMatrixNorm, 0, 2);
        *v = cvGetReal2D(m_CameraMatrixNorm, 1, 1)*y
            + cvGetReal2D(m_CameraMatrixNorm, 1, 2);
    }

}
void CTWithWater::setWaterDepth(double water_depth)
{
    m_dWaterDepth = water_depth;
    m_dCameraZ = cvGetReal1D(m_CameraWorld, 2);
    m_dCameraHeightAboveWater = m_dCameraZ - m_dWaterDepth;
}