Ejemplo n.º 1
0
Mat getMat(const VRmImage *p_source_img) {
	_VRmImageFormat format = p_source_img->m_image_format;
	VRmColorFormat color_format = format.m_color_format;
	CvSize cvSize = CvSize(format.m_width, format.m_height);
	int cvColor = toCvType(color_format);
	Mat openCVImg(cvSize, cvColor, (void*) p_source_img->mp_buffer, p_source_img->m_pitch);
	return openCVImg;
}
Ejemplo n.º 2
0
void CaptureThreadC::CaptureCamera() {

    bool colorFrameAdded = false;
    cv::VideoCapture capture(0);
    //keep getting last frame until told to stop
    int frame_count = 0;
    while (isRunning) {

        //time stuff
        struct timeb start;
        ftime(&start);

        capture.set(CV_CAP_PROP_FRAME_WIDTH, 320);
        capture.set(CV_CAP_PROP_FRAME_HEIGHT, 240);

        int frameheight = capture.get(CV_CAP_PROP_FRAME_HEIGHT);
        int framewidth = capture.get(CV_CAP_PROP_FRAME_WIDTH);

        cv::Mat bufferMat(frameheight, framewidth, CV_8UC3);

        capture >> bufferMat;
        cv::resize(bufferMat, bufferMat, CvSize(1200, 600));

        framesBuffer.enqueue(bufferMat);

        colorFrameAdded = true;
        if (colorFrameAdded == true)
        {
            cv::Mat  currentFrame = bufferMat.clone();
            cv::Mat gray_img;
            vector<cv::Point> points;	//to hold this frame face points

            int topLeftx = framewidth / 8;
            int topLefty = frameheight / 3;
            int bottomRightx = framewidth * 2;
            int bottomRighty = frameheight;

            samplingPoints.enqueue(points);

            cv::Rect ROI(cv::Point(topLeftx, topLefty), cv::Point(bottomRightx, bottomRighty));

            faceArea.enqueue(ROI);
        }
    }

    colorFrameAdded = false;

    //regulate fps
    struct timeb end;
    ftime(&end);

}
Ejemplo n.º 3
0
void ColorSeqDetector::input(IplImage *inputImg)
{
  if(!img)
  {
    img = cvCreateImage(cvGetSize(inputImg), IPL_DEPTH_32F, 1);
    cvZero(img);
  }
  if(!stateImg)
  {
    stateImg = cvCreateImage(cvGetSize(inputImg), IPL_DEPTH_8U, 3);
    cvZero(stateImg);
  }
  
  else if(img->width != inputImg->width
          || img->height != inputImg->height)
  {
    cvReleaseImage(&img);
    img = cvCreateImage(cvGetSize(inputImg), IPL_DEPTH_32F, 1);
    cvReleaseImage(&stateImg);
    stateImg = cvCreateImage(cvGetSize(inputImg), IPL_DEPTH_8U, 3);
    cvZero(stateImg);
    cvZero(img);
  }

  uchar *data;
  int step;
  CvSize(size);
  uchar *state_data;
  int state_step;
  float *float_data;
  int float_step;

  cvGetRawData(inputImg, (uchar**)&data, &step, &size);
  step /= sizeof(data[0]);
  cvGetRawData(img, (uchar**)&float_data, &float_step, &size);
  float_step /= sizeof(float_data[0]);
  cvGetRawData(stateImg, (uchar**)&state_data, &state_step, &size);
  state_step /= sizeof(state_data[0]);
  
  for(int y = 0; y < size.height;
      y++, data += step, state_data+=state_step, float_data+=float_step )
    for(int x = 0; x < size.width; x++ )
    {
      uchar h = data[3*x];
      uchar s = data[3*x+1];
      uchar v = data[3*x+2];
      uchar state = state_data[3*x];
      uchar sstate = state_data[3*x+1];
      uchar bstate = state_data[3*x+1];
      float score = float_data[x];
      
      switch(state){
      case 0: // invalid state
        if(isRed(h,s,v))
        {
          state = 1;
          sstate = 0;
          bstate = 0;
        }
        break;
      case 1: // red state
        if(isRed(h,s,v))
        {
          sstate++;
          if(sstate > period+2)
          {
            sstate = 0;
            state = 0;
          }
        }else if(sstate >= period-2 && isGreen(h,s,v))
        {
          state = 2;
          sstate = 0;
          bstate = 0;
        }else{
          bstate ++;
          if(bstate > 1)
          {
            state = 0;
            sstate = 0;
          }
        }
        break;
      case 2: // green state
        if(isGreen(h,s,v))
        {
          sstate++;
          if(sstate > period+2)
          {
            sstate = 0;
            state = 0;
          }
        }else if(sstate >= period - 2 && isBlue(h,s,v))
        {
          state = 3;
          sstate = 0;
          bstate = 0;
        }else{
          bstate ++;
          if(bstate > 1)
          {
            state = 0;
            sstate = 0;
          }
        }
        break;
      case 3: // blue state
        if(isBlue(h,s,v))
        {
          sstate++;
          if(sstate > period+2)
          {
            sstate = 0;
            state = 0;
          }
        }else if(sstate >= period-2 && isYellow(h,s,v))
        {
          state = 4;
          sstate = 0;
          bstate = 0;
        }else{
          bstate ++;
          if(bstate > 1)
          {
            state = 0;
            sstate = 0;
          }
        }
        break;
      case 4: // yellow state
        if(isYellow(h,s,v))
        {
          sstate++;
          if(sstate > period+2)
          {
            sstate = 0;
            state = 0;
          }
        }else if(sstate >= period-2 && isRed(h,s,v))
        {
          state = 1;
          sstate = 0;
          bstate = 0;

          score = 1.;
          //float_data[x]=1.;
        }else{
          bstate ++;
          if(bstate > 1)
          {
            state = 0;
            sstate = 0;
          }
        }
        break;
      default:
        state = 0;
        sstate = 0;
        break;
      }
      
      state_data[3*x] = state;
      state_data[3*x+1] = sstate;
      //float_data[x] = (float) state / 4.f;

      score -= 0.005;
      if(score < 0. )
      {
        score = 0.;
      }
      float_data[x] = score;
      
//       if(state == 0){
//         float_data[x] = 0.;
//       }
    }
  
  emitNamedEvent("output", img);
}
Ejemplo n.º 4
0
int calGLCM(Mat src,int angleDirection,double* featureVector)
{
    cvtColor(src,src,COLOR_BGR2GRAY);
    IplImage* bWavelet;
    bWavelet=cvCreateImage(CvSize(src.cols,src.rows),8,1);
    bWavelet->imageData=(char*)src.data;

    int i,j;
    int width,height;

    if(NULL == bWavelet)
        return 1;

    width = bWavelet->width;
    height = bWavelet->height;

    double * glcm = new double[GLCM_CLASS * GLCM_CLASS];
    int * histImage = new int[width * height];
    int maxnumber=0;
    if(NULL == glcm || NULL == histImage)
        return 2;

    //灰度等级化---分GLCM_CLASS个等级
    uchar *data =(uchar*) bWavelet->imageData;
    for(i = 0;i < height;i++){
        for(j = 0;j < width;j++){
            histImage[i * width + j] = (int)(data[bWavelet->widthStep * i + j] * GLCM_CLASS / 256);
        }
    }

    //初始化共生矩阵
    for (i = 0;i < GLCM_CLASS;i++)
        for (j = 0;j < GLCM_CLASS;j++)
            glcm[i * GLCM_CLASS + j] = 0;

    //计算灰度共生矩阵
    int w,k,l;
    //水平方向
    if(angleDirection == GLCM_ANGLE_HORIZATION)
    {
        for (i = 0;i < height;i++)
        {
            for (j = 0;j < width;j++)
            {
                l = histImage[i * width + j];
                if(j + GLCM_DIS >= 0 && j + GLCM_DIS < width)
                {
                    k = histImage[i * width + j + GLCM_DIS];
                    glcm[l * GLCM_CLASS + k]++;
                    maxnumber++;
                }
                if(j - GLCM_DIS >= 0 && j - GLCM_DIS < width)
                {
                    k = histImage[i * width + j - GLCM_DIS];
                    glcm[l * GLCM_CLASS + k]++;
                    maxnumber++;
                }
            }
        }
    }
    //垂直方向
    else if(angleDirection == GLCM_ANGLE_VERTICAL)
    {
        for (i = 0;i < height;i++)
        {
            for (j = 0;j < width;j++)
            {
                l = histImage[i * width + j];
                if(i + GLCM_DIS >= 0 && i + GLCM_DIS < height)
                {
                    k = histImage[(i + GLCM_DIS) * width + j];
                    glcm[l * GLCM_CLASS + k]++;
                    maxnumber++;
                }
                if(i - GLCM_DIS >= 0 && i - GLCM_DIS < height)
                {
                    k = histImage[(i - GLCM_DIS) * width + j];
                    glcm[l * GLCM_CLASS + k]++;
                    maxnumber++;
                }
            }
        }
    }
    //对角方向
    else if(angleDirection == GLCM_ANGLE_DIGONAL)
    {
        for (i = 0;i < height;i++)
        {
            for (j = 0;j < width;j++)
            {
                l = histImage[i * width + j];

                if(j + GLCM_DIS >= 0 && j + GLCM_DIS < width && i + GLCM_DIS >= 0 && i + GLCM_DIS < height)
                {
                    k = histImage[(i + GLCM_DIS) * width + j + GLCM_DIS];
                    glcm[l * GLCM_CLASS + k]++;
                    maxnumber++;
                }
                if(j - GLCM_DIS >= 0 && j - GLCM_DIS < width && i - GLCM_DIS >= 0 && i - GLCM_DIS < height)
                {
                    k = histImage[(i - GLCM_DIS) * width + j - GLCM_DIS];
                    glcm[l * GLCM_CLASS + k]++;
                    maxnumber++;
                }
            }
        }
    }
    //反对角方向
    else if(angleDirection == GLCM_ANGLE_AGAINST_D)
    {
        for (i = height-1;i >=0 ;i--)
        {
            for (j = 0;j < width;j++)
            {
                l = histImage[i * width + j];

                if(j + GLCM_DIS >= 0 && j + GLCM_DIS < width && i - GLCM_DIS >= 0 && i - GLCM_DIS < height)
                {
                    k = histImage[(i - GLCM_DIS) * width + j + GLCM_DIS];
                    glcm[l * GLCM_CLASS + k]++;
                    maxnumber++;
                }
                if(j - GLCM_DIS >= 0 && j - GLCM_DIS < width && i + GLCM_DIS >= 0 && i + GLCM_DIS < height)
                {
                    k = histImage[(i + GLCM_DIS) * width + j - GLCM_DIS];
                    glcm[l * GLCM_CLASS + k]++;
                    maxnumber++;
                }
            }
        }
    }
    // 归一化
    for (i = 0;i < GLCM_CLASS;i++)
    {
        for (j = 0;j < GLCM_CLASS;j++)
        {
            if(maxnumber>0)
            glcm[i * GLCM_CLASS + j] = glcm[i * GLCM_CLASS + j]/maxnumber;
        }
    }

    //计算自相关用到的参数Ux,Uy
    double Ux=0,Uy=0;
    double Sx=0,Sy=0;
    for (i=0;i<GLCM_CLASS;i++)
    {
        for(j=0;j<GLCM_CLASS;j++)
        {
            Ux +=i*glcm[i * GLCM_CLASS + j];
            Uy +=j*glcm[i * GLCM_CLASS + j];
        }
    }
    for (i=0;i<GLCM_CLASS;i++)
    {
        for(j=0;j<GLCM_CLASS;j++)
        {
            Sx +=glcm[i * GLCM_CLASS + j]*(i-Ux)*(i-Ux);
            Sy +=glcm[i * GLCM_CLASS + j]*(j-Uy)*(j-Uy);
        }
    }
    Sx=sqrt(Sx);
    Sy=sqrt(Sy);
    //计算特征值
    double entropy = 0,energy = 0,contrast = 0,correlation = 0;//homogenity = 0;
    for (i = 0;i < GLCM_CLASS;i++)
    {
        for (j = 0;j < GLCM_CLASS;j++)
        {
            //熵
            if(glcm[i * GLCM_CLASS + j] > 0)
                entropy -= glcm[i * GLCM_CLASS + j] * log10(double(glcm[i * GLCM_CLASS + j]));
            //能量
            energy += glcm[i * GLCM_CLASS + j] * glcm[i * GLCM_CLASS + j];
            //对比度
            contrast += (i - j) * (i - j) * glcm[i * GLCM_CLASS + j];
            //一致性
            //homogenity += 1.0 / (1 + (i - j) * (i - j)) * glcm[i * GLCM_CLASS + j];
            //自相关性
            correlation +=i*j*glcm[i * GLCM_CLASS + j];
        }
    }
    correlation = (correlation - Ux*Uy)/(Sx*Sy);
    //返回特征值
    i = 0;
    featureVector[i++] = entropy;
    featureVector[i++] = energy;
    featureVector[i++] = contrast;
    featureVector[i++] = correlation;

    delete[] glcm;
    delete[] histImage;
    cvReleaseImage(&bWavelet);//释放内存
    return 0;
}