// -------------------------------------------------------------------------- //
//
double ScalarDistributionData::calculate_chi2(const std::vector<double> & distribution) const
{
    // Subtract the target from the scalar distribution.
    std::vector<double> tmp_distribution = distribution - target_;

    // Square the difference.
    vsquare(tmp_distribution);

    // Multiply with the elementwize factors.
    tmp_distribution = tmp_distribution * factor_;

    // Sum.
    double chi2 = vsum(tmp_distribution);

    // Divide by sigma2.
    chi2 *= one_over_sigma2_;

    // All done.
    return chi2;
}
Exemplo n.º 2
0
void ImageProcessor::filterAverage(RectArea rect, QImage& img) {

  std::vector<std::vector<kaverage> > vsquare (rect.width,std::vector<kaverage>(
                                               rect.height));
  int average;

  for(int i=0;i<rect.width;i++) {
    for(int j=0; j<rect.height;j++) {
      vsquare[i][j].Gray=qGray(img.pixel(i+rect.x,j+rect.y));
      vsquare[i][j].k=0;
      average+=vsquare[i][j].Gray;
    }
  }
  average=average/(rect.width*rect.height);
  
  std::vector<int> arr;

  for(int i=2;i<rect.width-2;i++) {
    for(int j=2; j<rect.height-2;j++) {
      arr.clear();
      for (int k=-2; k<=2; k++) {
        for (int l=-2; l<=2; l++) {
          if(abs(k*k+l*l)<2)
            arr.push_back(vsquare[i+k][j+l].Gray);
        }
      }
      int avrg=0;
      for(int m=0;(static_cast<unsigned int>(m))<arr.size();m++) {
        avrg+=arr[m];
      }
      avrg/=arr.size();
      img.setPixel(i+rect.x,j+rect.y,avrg*0x010101);
        //qApp->processEvents();
    }
  }
  if (DebugMode) {
    sendImage(*image);
    writeLog(QString(tr("AverageFilter")));
  }

}
Exemplo n.º 3
0
void ImageProcessor::filterMedian(RectArea rect, QImage& img) {

  std::vector<std::vector<kaverage> > vsquare (rect.width,std::vector<kaverage>(
                                               rect.height));
  int average;

  for(int i=0;i<rect.width;i++) {
    for(int j=0; j<rect.height;j++) {
      vsquare[i][j].Gray=qGray(img.pixel(i+rect.x,j+rect.y));
      vsquare[i][j].k=0;
      average+=vsquare[i][j].Gray;
    }
  }
  average=average/(rect.width*rect.height);
  
  std::vector<int> arr;

  for(int i=2;i<rect.width-2;i++) {
    for(int j=2; j<rect.height-2;j++) {
      arr.clear();
      for (int k=-2; k<=2; k++) {
        for (int l=-2; l<=2; l++) {
          if(abs(k*k+l*l)<2)
            arr.push_back(vsquare[i+k][j+l].Gray);
        }
      }
      std::sort(&arr[0], &arr[arr.size()]);
      img.setPixel(i+rect.x,j+rect.y,arr[
           // 0
          arr.size()/2
              ]*0x010101);
        //qApp->processEvents();
    }
  }
  if (DebugMode) {
    sendImage(*image);
    writeLog(QString(tr("MedianFilter")));
  }

}
Exemplo n.º 4
0
void ImageProcessor::filterDilation(RectArea rect, QImage& img) {

  std::vector<std::vector<kaverage> > vsquare (rect.width,std::vector<kaverage>(
                                               rect.height));
  int average;

  for(int i=0;i<rect.width;i++) {
    for(int j=0; j<rect.height;j++) {
      vsquare[i][j].Gray=qGray(img.pixel(i+rect.x,j+rect.y));
      vsquare[i][j].k=0;
      average+=vsquare[i][j].Gray;
    }
  }
  average=average/(rect.width*rect.height);

  for(int i=2;i<rect.width-2;i++) {
    for(int j=2; j<rect.height-2;j++) {
      for (int k=-1; k<=1; k++) {
        for (int l=-1; l<=1; l++) {
        if ((abs(k)+abs(l))<2) 
          if (vsquare[i+k][j+l].Gray) {
            img.setPixel(i+rect.x,j+rect.y,0xffffff);
            break;
          }
          if (vsquare[i+k][j+l].Gray) break;
        }
      }
    //  img.setPixel(i+rect.x,j+rect.y,arr[
           // 0
      //    arr.size()/2
     //         ]*0x010101);
    }
  }
  if (DebugMode) {
    sendImage(*image);
    writeLog(QString(tr("Dilation")));
  }
}
Exemplo n.º 5
0
// -------------------------------------------------------------------------- //
//
void Test_Mathutils::testElementwiseVectorDoubleSquare()
{
    // Setup.
    std::vector<double> vec0(4);
    vec0[0] =  1.1;
    vec0[1] =  2.2;
    vec0[2] =  3.3;
    vec0[3] = -7.7;

    // Take a reference.
    const std::vector<double> ref = vec0;

    // Call.
    vsquare(vec0);

    // Check.
    CPPUNIT_ASSERT_EQUAL(static_cast<int>(vec0.size()), static_cast<int>(ref.size()));
    CPPUNIT_ASSERT_DOUBLES_EQUAL( vec0[0], ref[0]*ref[0], EPS );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( vec0[1], ref[1]*ref[1], EPS );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( vec0[2], ref[2]*ref[2], EPS );
    CPPUNIT_ASSERT_DOUBLES_EQUAL( vec0[3], ref[3]*ref[3], EPS );

}
Exemplo n.º 6
0
void ImageProcessor::recognizePiece(RectArea rect, QImage& img, PieceSquare &ChessDeskPiece) {
  

  std::vector<std::vector<kaverage> > vsquare (rect.width,std::vector<kaverage>(
                                               rect.height));
  int average;
  int NumOfPixels=0;

  int HasPiece=0;
  for(int i=1;i<rect.width-1;i++) {
    for(int j=1; j<rect.height-1;j++) {
      vsquare[i][j].Gray=qGray(img.pixel(i+rect.x,j+rect.y));
      if (vsquare[i][j].Gray) NumOfPixels++;
      if (vsquare[i][j].Gray && !HasPiece) HasPiece=1;
      vsquare[i][j].k=0;
      average+=vsquare[i][j].Gray;
    }
  }
  average=average/(rect.width*rect.height);

  if (HasPiece) {
    double Elongation=0;
    double Compaction=0;

    int Perimeter=0;
    int VolumeArea=0;

    for(int i=1;i<rect.width-1;i++) {
      for(int j=1; j<rect.height-1;j++) {
        if (vsquare[i][j].Gray) {
          VolumeArea++;
          if (!vsquare[i+1][j+1].Gray ||
              !vsquare[i+1][j].Gray ||
              !vsquare[i+1][j-1].Gray ||
              !vsquare[i][j+1].Gray ||
              !vsquare[i][j-1].Gray ||
              !vsquare[i-1][j+1].Gray ||
              !vsquare[i-1][j].Gray ||
              !vsquare[i-1][j-1].Gray) {
            Perimeter++;
              }
        }
      }
    }
    
    Compaction=static_cast<double>(Perimeter*Perimeter)/VolumeArea;
    //writeLog(QString("Perimeter %1 \n Square %2").arg(Perimeter).arg(VolumeArea));

    writeLog(QString("Compact %3").arg(Compaction));

    int XCenterMass=0;
    int YCenterMass=0;
    
    for(int i=1;i<rect.width-1;i++) {
      for(int j=1; j<rect.height-1;j++) {
        if (vsquare[i][j].Gray) {
          XCenterMass+=i;
          YCenterMass+=j;
        }
      }
    }
    XCenterMass/=NumOfPixels;
    YCenterMass/=NumOfPixels;
    //writeLog(QString("XCenterMass %1 YCenterMass %2").arg(XCenterMass).arg(YCenterMass));

    double m02=0;
    double m20=0;
    double m11=0;
    
    for(int i=1;i<rect.width-1;i++) {
      for(int j=1; j<rect.height-1;j++) {
        if (vsquare[i][j].Gray) {
          m02+=(YCenterMass-j)*(YCenterMass -j);
          m20+=(XCenterMass-i)*(XCenterMass -i);
          m11+=(i-XCenterMass)*(j-YCenterMass);
        }
      }
    }
    Elongation=(m20+m02+sqrt((m20-m02)*(m20-m02)+4*m11*m11))/
               (m20+m02-sqrt((m20-m02)*(m20-m02)+4*m11*m11));
    writeLog(QString("Elongation %1 \n").arg(Elongation));

    if (Elongation>2) {
      ChessDeskPiece.Type=3; // Pawn
    } else {
        if (Compaction>50) {
          ChessDeskPiece.Type=2; //Queen
      } else {
          ChessDeskPiece.Type=1; //King
      }
    }


  } else {
    ChessDeskPiece.Type=0;
  }
  if (DebugMode) {
    sendImage(*image);
    writeLog(QString(tr("Piece recognition")));
  }
}
Exemplo n.º 7
0
void ImageProcessor::filterFigure(RectArea rect, QImage& img) {


  std::vector<std::vector<kaverage> > vsquare (rect.width,std::vector<kaverage>(
                                               rect.height));
  int average;

  for(int i=0;i<rect.width;i++) {
    for(int j=0; j<rect.height;j++) {
      vsquare[i][j].Gray=qGray(img.pixel(i+rect.x,j+rect.y));
      vsquare[i][j].k=0;
      average+=vsquare[i][j].Gray;
    }
  }
  average=average/(rect.width*rect.height);

  int CurrentLabelNumber=0;

  for(int i=1;i<rect.width-1;i++) {
    for(int j=1; j<rect.height-1;j++) {

      if (vsquare[i][j].Gray) {
        if(!vsquare[i-1][j].k && !vsquare[i][j-1].k) {
          CurrentLabelNumber++;
          vsquare[i][j].k=CurrentLabelNumber;
        }

        else if ((!vsquare[i-1][j].k && vsquare[i][j-1].k) ||
                  (vsquare[i-1][j].k && !vsquare[i][j-1].k)) {
          if (vsquare[i-1][j].k) {vsquare[i][j].k=vsquare[i-1][j].k;}
          if (vsquare[i][j-1].k) {vsquare[i][j].k=vsquare[i][j-1].k;}
        }

        else if (vsquare[i-1][j].k && vsquare[i][j-1].k) {
          if (vsquare[i-1][j].k == vsquare[i][j-1].k){
            vsquare[i][j].k=vsquare[i][j-1].k;
          }
          else {
            int tmpLabel=vsquare[i-1][j].k;
            vsquare[i][j].k=vsquare[i][j-1].k;
            for(int m=1;m<rect.width-1;m++) {
              for(int l=1; l<rect.height-1;l++) {
                if (vsquare[m][l].k==tmpLabel) {
                  vsquare[m][l].k=vsquare[i][j-1].k;
                }
              }
            }
          }
        }
      }

    }
  }

  std::vector<int> GkArea(CurrentLabelNumber+1,0);

  for(int i=1;i<rect.width-1;i++) {
    for(int j=1; j<rect.height-1;j++) {
      if (vsquare[i][j].k!=0) GkArea[vsquare[i][j].k]++;
    }
  }

  int MaxK=0;
  int ThisK=0;

  for (int i=1; (static_cast<unsigned int>(i))<GkArea.size(); i++) {
     if (GkArea[i]>MaxK) {
       MaxK=GkArea[i];
       ThisK=i;
     }
  }

  for(int i=1;i<rect.width-1;i++) {
    for(int j=1; j<rect.height-1;j++) {
      if (vsquare[i][j].k!=ThisK) {vsquare[i][j].k=0;}
            else
            if (GkArea[vsquare[i][j].k]<100) {vsquare[i][j].k=0;}
    }
  }

  for(int i=1;i<rect.width-1;i++) {
    for(int j=1; j<rect.height-1;j++) {
      img.setPixel(rect.x+i,rect.y+j,vsquare[i][j].k?0xffffff:0x000000
                   //0xffffff*vsquare[i][j].k/(CurrentLabelNumber+2)
                  );
    }
  }
  if (DebugMode) {
    sendImage(*image);
    writeLog(QString(tr("Finding compact spaces.")));
  }
}