Пример #1
0
int FaceDetect(const T *img, int width, int height, const char *classifier,
               int *out, int maxdetect)
{
    cv::Mat cvimg(width,height,CV_8UC1);
    double maxval = *std::max_element(img,img+width*height);
    double scale = (maxval==0)?1:255/maxval;
    /* 8bitデータへの変換 */
    for(int i=0; i<height; ++i) {
        for(int j=0; j<width; ++j) {
            *cvimg.ptr(i,j) = static_cast<unsigned char>(img[i*width+j] * scale);
        }
    }

    std::vector<cv::Rect> faces;
    // 顔検出器のロード
    {
        cv::CascadeClassifier cascade;
        if(cascade.load( classifier )) {
            // 顔検出の実行
            cascade.detectMultiScale( cvimg, faces, 1.1, 2,
                                      CV_HAAR_SCALE_IMAGE, cv::Size(30, 30) );
        }
    }
    int n = std::min<int>(maxdetect,faces.size());
    /* 顔位置データの引渡し (配列への変換) */
    for(int i=0; i<n; ++i) {
        out[4*i+0] = faces[i].x;
        out[4*i+1] = faces[i].y;
        out[4*i+2] = faces[i].width;
        out[4*i+3] = faces[i].height;
    }
    return n;
}
Пример #2
0
openMVG::image::Image<openMVG::image::RGBColor> OpenCVHelper::createThumbnail(
	const openMVG::image::Image<openMVG::image::RGBColor> &orig, int thWidth, int thHeight)
{
	const int width = orig.Width();
	const int height = orig.Height();
	const float image_aspect = static_cast<float>(width) / static_cast<float>(height);
	const float thumb_aspect = static_cast<float>(thWidth) / static_cast<float>(thHeight);

	int rescale_width, rescale_height;
	if(image_aspect > thumb_aspect)
	{
		rescale_width = std::ceil(thHeight * image_aspect);
		rescale_height = thHeight;
	}
	else
	{
		rescale_width = thWidth;
		rescale_height = std::ceil(thWidth / image_aspect);
	}

	// Convert image to OpenCV data
	cv::Mat cvimg(height, width, CV_8UC3);
	cv::Vec3b *cvPtr = cvimg.ptr<cv::Vec3b>(0);
	for(int y = 0; y < height; y++)
	{
		for(int x = 0; x < width; x++)
		{
			(*cvPtr)[2] = orig(y, x).r();	// OpenCV usually stores BGR, not RGB
			(*cvPtr)[1] = orig(y, x).g();
			(*cvPtr)[0] = orig(y, x).b();
			cvPtr++;
		}
	}

	// Resize
	cv::Mat cvoutimg;
	cv::resize(cvimg, cvoutimg, cv::Size(thWidth, thHeight), 0, 0, cv::INTER_AREA);

	// Convert back to openMVG::image
	cvPtr = cvoutimg.ptr<cv::Vec3b>(0);
	openMVG::image::Image<openMVG::image::RGBColor> outImg;
	outImg.resize(thWidth, thHeight);
	for(int y = 0; y < thHeight; y++)
	{
		for(int x = 0; x < thWidth; x++)
		{
			outImg(y, x).r() = (*cvPtr)[2];	// OpenCV usually stores BGR, not RGB
			outImg(y, x).g() = (*cvPtr)[1];
			outImg(y, x).b() = (*cvPtr)[0];
			cvPtr++;
		}
	}

	return outImg;
}
Пример #3
0
int GoodFeaturesToTrack(const T *img, int width, int height, int maxpos,
 double *pos, double quality, double mindist, bool bHarris, bool bSubPix){
  cv::Mat cvimg(width,height,CV_8UC1);
  double maxval = *std::max_element(img,img+width*height);
  double scale = (maxval==0)?1:255/maxval;
  /* 8bitデータへの変換 */
  for(int i=0;i<height;++i){
    for(int j=0;j<width;++j){
      *cvimg.ptr(i,j) = static_cast<unsigned char>(img[i*width+j] * scale);
    }}
  std::vector<cv::Point2f> cvpnts;

  cv::goodFeaturesToTrack(cvimg,cvpnts,maxpos,quality,mindist,cv::Mat(),3,bHarris);
  if(bSubPix){
    cv::TermCriteria criteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 20, 0.03);
    cv::cornerSubPix(cvimg,cvpnts,cv::Size(10,10), cv::Size(-1,-1), criteria);
  }
  for(int i=0;i<cvpnts.size();++i){
    pos[2*i  ] = cvpnts[i].x;
    pos[2*i+1] = cvpnts[i].y;
  }
  return cvpnts.size();
}
bool DominantColorSegmentation::getHSIColorHistogram(QImage *img)
{
    //Create Mat images
    int w = img->width(), h = img->height();
    int bl = img->bytesPerLine();
    uchar *fin_p = img->bits();

    cv::Mat cvimg(h,w,CV_8UC4);
    uchar *cvpointer = cvimg.data;

    memcpy(cvpointer, fin_p, h*bl);

    cv::Mat BGRmat(h,w,CV_8UC3);
    uchar *BGRpointer = BGRmat.data;
    int bplbgr = BGRmat.step;
    cv::Mat HSVmat(h,w,CV_8UC3);
    cv::cvtColor(cvimg, BGRmat, COLOR_BGRA2BGR);
    cv::cvtColor(BGRmat, HSVmat, COLOR_BGR2HSV);
    uchar *HSVpointer = HSVmat.data;
    int blpHSV = HSVmat.step;

    //Histograms
    int H_Hist[255];
    int S_Hist[255];
    int I_Hist[255];
    int Hpeak = 0;
    int Speak = 0;
    int Ipeak = 0;
    int ihpeak;
    int ispeak;
    int iipeak;


    for(int i=0; i<256; i++)
    {
        H_Hist[i] = 0;
        S_Hist[i] = 0;
        I_Hist[i] = 0;
    }

    //Haciendo los Histogramas
    for (int i = 0; i < w; ++i) {
        for (int j = 0; j < h; ++j)
        {
            int B = cvpointer[i*4 + j*bl + 0];
            int G = cvpointer[i*4 + j*bl + 1];
            int R = cvpointer[i*4 + j*bl + 2];
//            int A = cvpointer[i*4 + j*bl + 3];
            int H = HSVpointer[i*3 + j*blpHSV + 0];
            int S = HSVpointer[i*3 + j*blpHSV + 1];
//            int V = HSVpointer[i*3 + j*blpHSV + 2];
            int I = (R+G+B)/3;
//            std::cout << H << "\t" << S << "\t" << I << "\t";
//            std::cout << R << "\t" << G << "\t" << B << "\n";
//            cvpointer[i*4 + j*bl + 0] = H;
//            cvpointer[i*4 + j*bl + 2] = H;
//            cvpointer[i*4 + j*bl + 1] = H;
            H_Hist[H]++;
            S_Hist[S]++;
            I_Hist[I]++;

            //Obtain max peaks of each histograms
            if(H_Hist[H] > Hpeak)
            {
                ihpeak = H;
                Hpeak = H_Hist[H];
            }
            if(S_Hist[S] > Speak && S<255)
            {
                ispeak = S;
                Speak = S_Hist[S];
            }
            if(I_Hist[H] > Ipeak)
            {
                iipeak = I;
                Ipeak = I_Hist[I];
            }
        }
    }

    /**Finding Histogram mean**/
    Hmean = find_histogram_mean(H_Hist, Hpeak, ihpeak);
    Smean = find_histogram_mean(S_Hist, Speak, ispeak);
    Imean = find_histogram_mean(I_Hist, Ipeak, iipeak);

    cv::Mat fg2(h,w,CV_8UC1);
    uchar *fg2bits = fg2.data;
    int fg2bpl = fg2.step;

    uchar *fgbits = m_data->fgImage->bits();
    int fgbpl = m_data->fgImage->bytesPerLine();

    for (int i = 0; i < w; ++i) {
        for (int j = 0; j < h; ++j)
        {
            //MASK
            if(j > (148-0.049*i) && j < (844-0.085*i))
            {
                int B = cvpointer[i*4 + j*bl + 0];
                int G = cvpointer[i*4 + j*bl + 1];
                int R = cvpointer[i*4 + j*bl + 2];
                int H = HSVpointer[i*3 + j*blpHSV + 0];
                int S = HSVpointer[i*3 + j*blpHSV + 1];
                int I = (R+G+B)/3;

                double cylindricaldist = get_cylindrical_dist(H,S,I);
                if(cylindricaldist > Tcolor)
                    fg2bits[i + j*fg2bpl + 0] = 255;
                else
                    fg2bits[i + j*fg2bpl + 0] = 0;
            }
            else
                fg2bits[i + j*fg2bpl + 0] = 0;
        }
    }

    Mat element = cv::getStructuringElement( cv::MORPH_ELLIPSE,
                                 cv::Size( 3, 3 ),
                                 cv::Point( 1, 1 ) );

    //EROSION
    cv::erode(fg2, fg2, element, cv::Point(-1,-1), 1);
    cv::dilate(fg2, fg2, element, cv::Point(-1,-1), 1);

    //DILATACION
    cv::dilate(fg2, fg2, element, cv::Point(-1,-1), 1);
    cv::erode(fg2, fg2, element, cv::Point(-1,-1), 1);

    memcpy(fgbits, fg2bits, h*fgbpl);
    /**EXTRA**/
//    cv::Mat blurimg(h,w,CV_8UC4);
//    blur( cvimg, blurimg, Size( 15, 15));
//    uchar *blurpointer = blurimg.data;
//    for (int i = 0; i < w; ++i) {
//        for (int j = 0; j < h; ++j)
//        {
//            int B = blurpointer[i*4 + j*bl + 0];
//            int G = blurpointer[i*4 + j*bl + 1];
//            int R = blurpointer[i*4 + j*bl + 2];
//            int I = (R+G+B)/3;

//            if(I+j*upperpenalty<IntensityThreshold)
//            {
//                cvpointer[i*4 + j*bl + 0] = 0;
//                cvpointer[i*4 + j*bl + 1] = 0;
//                cvpointer[i*4 + j*bl + 2] = 0;
//            }
//            else
//            {
//                cvpointer[i*4 + j*bl + 0] = BGRpointer[i*3 + j*bplbgr + 0];
//                cvpointer[i*4 + j*bl + 1] = BGRpointer[i*3 + j*bplbgr + 1];
//                cvpointer[i*4 + j*bl + 2] = BGRpointer[i*3 + j*bplbgr + 2];
//            }

//        }
//    }

//    memcpy(fin_p, cvpointer, h*bl);
    return true;
}
Пример #5
0
void BallClasificationModule::dominantcolor()
{
    //Parametros
    HistogramPeakThreshold = 0.2;
    Tcolor = 120;
    upperpenalty = 0.5;
    IntensityThreshold = 70;

    //CREANDO IMAGEN EN MAT
    QImage *img = m_data->currentImage;
    int w = img->width(), h = img->height();
    int bl = img->bytesPerLine();
    uchar *fin_p = img->bits();
    cv::Mat cvimg(h,w,CV_8UC4);
    uchar *cvpointer = cvimg.data;
    memcpy(cvpointer, fin_p, h*bl);

    //CAMBIANDO DIMENCIONES DE COLOR
    cv::Mat BGRmat(h,w,CV_8UC3);
    cv::Mat HSVmat(h,w,CV_8UC3);
    cv::cvtColor(cvimg, BGRmat, COLOR_BGRA2BGR);
    cv::cvtColor(BGRmat, HSVmat, COLOR_BGR2HSV);
    uchar *HSVpointer = HSVmat.data;
    int blpHSV = HSVmat.step;

    //Histograms
    int H_Hist[255];
    int S_Hist[255];
    int I_Hist[255];
    int Hpeak = 0;
    int Speak = 0;
    int Ipeak = 0;
    int ihpeak;
    int ispeak;
    int iipeak;


    for(int i=0; i<256; i++)
    {
        H_Hist[i] = 0;
        S_Hist[i] = 0;
        I_Hist[i] = 0;
    }

    //Haciendo los Histogramas
    for (int i = 0; i < w; ++i) {
        for (int j = 0; j < h; ++j)
        {
            int B = cvpointer[i*4 + j*bl + 0];
            int G = cvpointer[i*4 + j*bl + 1];
            int R = cvpointer[i*4 + j*bl + 2];
//            int A = cvpointer[i*4 + j*bl + 3];
            int H = HSVpointer[i*3 + j*blpHSV + 0];
            int S = HSVpointer[i*3 + j*blpHSV + 1];
//            int V = HSVpointer[i*3 + j*blpHSV + 2];
            int I = (R+G+B)/3;
//            std::cout << H << "\t" << S << "\t" << I << "\t";
//            std::cout << R << "\t" << G << "\t" << B << "\n";
//            cvpointer[i*4 + j*bl + 0] = H;
//            cvpointer[i*4 + j*bl + 2] = H;
//            cvpointer[i*4 + j*bl + 1] = H;
            H_Hist[H]++;
            S_Hist[S]++;
            I_Hist[I]++;

            //Obtain max peaks of each histograms
            if(H_Hist[H] > Hpeak)
            {
                ihpeak = H;
                Hpeak = H_Hist[H];
            }
            if(S_Hist[S] > Speak && S<255)
            {
                ispeak = S;
                Speak = S_Hist[S];
            }
            if(I_Hist[H] > Ipeak)
            {
                iipeak = I;
                Ipeak = I_Hist[I];
            }

//            /**TEST HISTOGRAMS**/
//            if(H>=48 && H<=53 )
//            {
//                cvpointer[i*4 + j*bl + 0] = 255;
//                cvpointer[i*4 + j*bl + 1] = 0;
//                cvpointer[i*4 + j*bl + 2] = 0;
//            }
        }
    }

    /**Finding Histogram mean**/
    Hmean = find_histogram_mean(H_Hist, Hpeak, ihpeak);
    Smean = find_histogram_mean(S_Hist, Speak, ispeak);
    Imean = find_histogram_mean(I_Hist, Ipeak, iipeak);

    /**Comprobando segmentacion**/
    for (int i = 0; i < w; ++i) {
        for (int j = 0; j < h; ++j)
        {
            int B = cvpointer[i*4 + j*bl + 0];
            int G = cvpointer[i*4 + j*bl + 1];
            int R = cvpointer[i*4 + j*bl + 2];
            int H = HSVpointer[i*3 + j*blpHSV + 0];
            int S = HSVpointer[i*3 + j*blpHSV + 1];
            int I = (R+G+B)/3;

            double cylindricaldist = get_cylindrical_dist(H,S,I);

            if(cylindricaldist > Tcolor)
            {
                cvpointer[i*4 + j*bl + 0] = 255;
                cvpointer[i*4 + j*bl + 1] = 0;
                cvpointer[i*4 + j*bl + 2] = 0;
            }
        }
    }

    namedWindow("asdasd",WINDOW_NORMAL);
    imshow("asdasd", cvimg);

    return;
}