Mat initModify::histogramEqualization(Mat & sourceImage) {
	// change the Mat to IplImage, which can make whole process quicker.
	IplImage * src;
	src = &IplImage(sourceImage);
	IplImage * imgChannel[4] = { 0, 0, 0, 0 };
	IplImage * dist = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 3);

	if (src) {
		for (int i = 0; i < src->nChannels; i++) {
			imgChannel[i] = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
		}

		// split all the channels (R, G, B, A)
		cvSplit(src, imgChannel[0], imgChannel[1], imgChannel[2], imgChannel[3]);
		for (int i = 0; i < dist->nChannels; i++) {
			cvEqualizeHist(imgChannel[i], imgChannel[i]);
		}
		// merge all the channels
		cvMerge(imgChannel[0], imgChannel[1], imgChannel[2], imgChannel[3], dist);
		Mat resultImage = cvarrToMat(dist, true);
		cvReleaseImage(&dist);
		return resultImage;
	}
	else {
		return Mat();
	}
}
void pix_opencv_patreco :: loadDistMess (t_symbol *filename)
{
	CvMat* distortion_coeffs;
		
	if ( filename->s_name[0] == 0 ) {
		error("no filename passed to loadDist message");
		return;
	}
	if ( filename == NULL ) { error("NULL pointer passed to function loadDist"); return;}
	distortion_coeffs = (CvMat*)cvLoad(filename->s_name); // TODO crash when passing non-XML file
	
	if (distortion_coeffs == NULL) { 
		distortion_coeffs = 	cvCreateMat(5, 1, CV_32FC1);
		error("can't open file %s", filename->s_name);
		//~ resetCorrectionMatrix();
	}
	else if( distortion_coeffs->rows != 5 || distortion_coeffs->cols != 1 || CV_MAT_TYPE(distortion_coeffs->type) != CV_32FC1 ) {
		error("%s is not a valid distortions coeffs file", filename->s_name);
		cvReleaseMat(&distortion_coeffs);
		distortion_coeffs = 	cvCreateMat(3, 3, CV_32FC1);
		//~ resetCorrectionMatrix();
	}
	else post("load distortion coefficients from %s",filename->s_name);
	
	m_distortions = cvarrToMat(distortion_coeffs);

	t_atom dist_out[5];
	for ( int i = 0 ; i < 5 ; i++ ){
		SETFLOAT(&dist_out[i], CV_MAT_ELEM( *distortion_coeffs, float, i, 0));
	}		
	outlet_anything( m_dataout, gensym("distortion_coeffs"), 5, dist_out);
	 
}
void pix_opencv_patreco :: loadIntraMess (t_symbol *filename)
{
	CvMat* intrinsic_matrix;

	if ( filename->s_name[0] == 0 ) {
		error("no filename passed to loadIntra message");
		return;
	}
	if ( filename == NULL ) { error("%s is not a valid matrix", filename->s_name); return;}
	intrinsic_matrix = (CvMat*)cvLoad(filename->s_name, 0, 0, 0);// TODO crash when passing non-XML file
  
	if (intrinsic_matrix == NULL) {
		intrinsic_matrix = 	cvCreateMat(3, 3, CV_32FC1);
		error("can't open file %s", filename->s_name);
		//~ resetCorrectionMatrix();
	}
	else if ( intrinsic_matrix->rows != 3 || intrinsic_matrix->cols != 3 || CV_MAT_TYPE(intrinsic_matrix->type) != CV_32FC1 ) {
		error("%s is not a valid intrinsic matrix", filename->s_name);
		cvReleaseMat(&intrinsic_matrix);
		intrinsic_matrix = 	cvCreateMat(3, 3, CV_32FC1);
		//~ resetCorrectionMatrix();
	}
	else post("load transformation matrix from %s",filename->s_name);
	
	m_cameraMatrix = cvarrToMat(intrinsic_matrix);

	t_atom intra_out[9];
	for ( int i = 0 ; i < 9 ; i++ ){
		SETFLOAT(&intra_out[i], CV_MAT_ELEM( *intrinsic_matrix, float, i%3, i/3));
	}		
	outlet_anything( this->m_dataout, gensym("intrinsic_matrix"), 9, intra_out);
}
QImage convert_lpl_qimg(IplImage* frame)
{
    Mat img = cvarrToMat(frame);
    cv::cvtColor(img,img,CV_BGR2RGB); //Qt reads in RGB whereas CV in BGR
    QImage imdisplay((uchar*)img.data, img.cols, img.rows, img.step, QImage::Format_RGB888);
    img.release();
    return imdisplay;
}
Beispiel #5
0
Mat AAM::getTextureParamsFromC(Mat c)
{
    Mat ap = this->appearancePCA.backProject(c);
    CvMat apMat=ap;
    CvMat textureParams;
    cvGetCols(&apMat, &textureParams, shapeSet.cols, shapeSet.cols+this->b_g.cols);
    Mat texture=cvarrToMat(&textureParams);
    return texture;
}
Beispiel #6
0
Mat AAM::getShapeParamsFromC(Mat c)
{
    Mat ap = this->appearancePCA.backProject(c);
    CvMat apMat=ap;
    CvMat shapeParams;
    cvGetCols(&apMat, &shapeParams, 0, shapeSet.cols);
    Mat shape=cvarrToMat(&shapeParams);
    shape=shape/this->w;
    return shape;
}
Beispiel #7
0
void insertImageCOI(InputArray _ch, CvArr* arr, int coi)
{
    Mat ch = _ch.getMat(), mat = cvarrToMat(arr, false, true, 1);
    if(coi < 0)
    {
        CV_Assert( CV_IS_IMAGE(arr) );
        coi = cvGetImageCOI((const IplImage*)arr)-1;
    }
    CV_Assert(ch.size == mat.size && ch.depth() == mat.depth() && 0 <= coi && coi < mat.channels());
    int _pairs[] = { 0, coi };
    mixChannels( &ch, 1, &mat, 1, _pairs, 1 );
}
Beispiel #8
0
void extractImageCOI(const CvArr* arr, OutputArray _ch, int coi)
{
    Mat mat = cvarrToMat(arr, false, true, 1);
    _ch.create(mat.dims, mat.size, mat.depth());
    Mat ch = _ch.getMat();
    if(coi < 0)
    {
        CV_Assert( CV_IS_IMAGE(arr) );
        coi = cvGetImageCOI((const IplImage*)arr)-1;
    }
    CV_Assert(0 <= coi && coi < mat.channels());
    int _pairs[] = { coi, 0 };
    mixChannels( &mat, 1, &ch, 1, _pairs, 1 );
}
Beispiel #9
0
// iterate the EKF
double slamStep(Mat u, VideoCapture &cap, VideoWriter &record)
{
	double r_dp = u.at<double>(0);
	double r_dth = u.at<double>(1);

	Mat frame;
	cap >> frame;

	ml->UpdateMarkers(frame);
	Mat observations = slam->step(u);

	// find the closest visible marker
	double mindist = 99999;
	for(int i=0; i < observations.rows; i+=2) {
		if(observations.at<double>(i) < mindist)
			mindist = observations.at<double>(i);
	}
	#ifdef GUI
	Mat rmap, mcov;
	if(slam->getMap(rmap, mcov)) {
		for(int i=0; i < rmap.rows; i+=2) {
			double g_x = rmap.at<double>(i);
			double g_y = rmap.at<double>(i+1);

			disp->setGuess(g_x,g_y,i/2);
		}
	}

	Mat robot = slam->getRobot();
	disp->guessRobot(robot.at<double>(0), robot.at<double>(1), robot.at<double>(2));
	const IplImage* dispImage = disp->renderMap();

	// Make sure our mat can hold all the images
	int height = (frame.rows > dispImage->height) ? frame.rows : dispImage->height;
	if(!outFrame.data || outFrame.cols < dispImage->width + frame.cols + 100 || outFrame.rows < height) {
		outFrame = Mat::zeros(height, dispImage->width + frame.cols + 100, frame.type());
		Rect binRoi(100 + dispImage->width, 0, frame.cols, frame.rows);
		Rect markerRoi(0, 0, 100, height);
		ml->SetDrawMats(outFrame(binRoi), outFrame(markerRoi));
	}
	Rect iplRoi(100, 0, dispImage->width, dispImage->height);
	cvarrToMat(dispImage).copyTo(outFrame(iplRoi));
	imshow("EKFSlam", outFrame);
	g_outFrame = outFrame;
	record << outFrame;
	waitKey(10);
	#endif

	return mindist;
}
Beispiel #10
0
void EyeCameraFrameReaderThread::run(void)
{
    emit onCameraTextChanged("Loading...");
    int frontCamIndex = cameraIndex == 1 ? 2 : 1;
    EyeTracker *eyeTracker = new EyeTracker(cameraIndex, frontCamIndex);

    // If camera could not be opened
    if(!eyeTracker->isOpened())
    {
        qDebug() << "Error: Cannot connect to camera.";
        emit onCameraTextChanged("Cannot connect to camera");
        emit finished();
    }

    for( ; ; )
    {
        // Capture images
        eyeTracker->captureEyeFrame();
        eyeTracker->captureSceneFrame();

        Point2f p = eyeTracker->doTrack();

        // Set up x and y coordinates
        char str[50];
        sprintf(str, "%.1f", p.x);
        emit onEyeXChanged(str);

        sprintf(str, "%.1f", p.y);
        emit onEyeYChanged(str);

        // Draw squares to eye image
        eyeTracker->drawSquares(eyeTracker->findSquares());

        // Set eye pixmap
        emit onCameraPixmapChanged(QPixmap::fromImage(cvMatToQImage(cvarrToMat(eyeTracker->getEyeImage()))));

        // Release images
        eyeTracker->releaseGrayEyeImage();
        eyeTracker->releaseGraySceneImage();

        cvWaitKey(20);
    }
}
int _tmain(int argc, _TCHAR* argv[])
{
	IplImage* input_img = cvLoadImage("Carol_Niedermayer_0001.jpg",0);
	Mat input_mat=cvarrToMat(input_img,true);
	if (input_mat.cols == 0)
	{
		cout<<"can not load image"<<endl;
		return 1;
	}
	Rect face_box = face_detect(input_mat);
	cout<<"here"<<endl;
	Mat aligned_face(128,128,CV_8UC1);
	int flag = face_align(input_mat,&aligned_face,face_box);
	if(flag == 0)
	{
		cout<<"can not align the face image"<<endl;
		return 1;
	}
	imwrite("aligned_face.bmp",aligned_face);
	char* modelPath = "casia_144_ve_id_model_iter_1210000.bin";
	const int layer_index =14;
	const int len = 320*8*8;
	float* feature = (float*)malloc(320*sizeof(float));
	clock_t start, end;
	double time;
	start = clock();
	cnnFace cnn(modelPath,layer_index,len);
	if (cnn.cnnFaceInit() != 0) {
		return 1;
	}
	cnn.getFeature(aligned_face, feature);
	//cnn.getFeature(imgFace2, feat2);

	//float score = cnn.getScore(feat1, feat2);

	end = clock();
	time = (double)(end -  start) / CLOCKS_PER_SEC;

	cout << "The score is " << "\nTime is " << time << endl;
	cnn.~cnnFace();
	free(feature);
	return 0;
}
Beispiel #12
0
static void*
imdecode_( const Mat& buf, int flags, int hdrtype, Mat* mat=0 )
{
    CV_Assert(buf.data && buf.isContinuous());
    IplImage* image = 0;
    CvMat *matrix = 0;
    Mat temp, *data = &temp;
    string filename;

    ImageDecoder decoder = findDecoder(buf);
    if( decoder.empty() )
        return 0;

    if( !decoder->setSource(buf) )
    {
        filename = tempfile();
        FILE* f = fopen( filename.c_str(), "wb" );
        if( !f )
            return 0;
        size_t bufSize = buf.cols*buf.rows*buf.elemSize();
        fwrite( &buf.data[0], 1, bufSize, f );
        fclose(f);
        decoder->setSource(filename);
    }

    if( !decoder->readHeader() )
    {
        if( !filename.empty() )
            remove(filename.c_str());
        return 0;
    }

    CvSize size;
    size.width = decoder->width();
    size.height = decoder->height();

    int type = decoder->type();
    if( flags != -1 )
    {
        if( (flags & CV_LOAD_IMAGE_ANYDEPTH) == 0 )
            type = CV_MAKETYPE(CV_8U, CV_MAT_CN(type));

        if( (flags & CV_LOAD_IMAGE_COLOR) != 0 ||
                ((flags & CV_LOAD_IMAGE_ANYCOLOR) != 0 && CV_MAT_CN(type) > 1) )
            type = CV_MAKETYPE(CV_MAT_DEPTH(type), 3);
        else
            type = CV_MAKETYPE(CV_MAT_DEPTH(type), 1);
    }

    if( hdrtype == LOAD_CVMAT || hdrtype == LOAD_MAT )
    {
        if( hdrtype == LOAD_CVMAT )
        {
            matrix = cvCreateMat( size.height, size.width, type );
            temp = cvarrToMat(matrix);
        }
        else
        {
            mat->create( size.height, size.width, type );
            data = mat;
        }
    }
    else
    {
        image = cvCreateImage( size, cvIplDepth(type), CV_MAT_CN(type) );
        temp = cvarrToMat(image);
    }

    bool code = decoder->readData( *data );
    if( !filename.empty() )
        remove(filename.c_str());

    if( !code )
    {
        cvReleaseImage( &image );
        cvReleaseMat( &matrix );
        if( mat )
            mat->release();
        return 0;
    }

    return hdrtype == LOAD_CVMAT ? (void*)matrix :
           hdrtype == LOAD_IMAGE ? (void*)image : (void*)mat;
}
Beispiel #13
0
int ns__trainANN(struct soap *soap,
                char *inputMatFilename,
                char *neuralFile,
                char **OutputMatFilename)
{
    double start, end;
    start = omp_get_wtime();

	Mat src; //must be 3ch image
    if(!readMat(inputMatFilename, src))
    {
        cerr << "trainANN :: can not read bin file" << endl;
        soap->fault->faultstring = "trainANN :: can not read bin file";
        return SOAP_FAULT;
    }

    // convert src to CvMat to use an old-school function
    CvMat tmp = src;
    CV_Assert(tmp.cols == src.cols && tmp.rows == src.rows &&
        tmp.data.ptr == (uchar*)src.data && tmp.step == src.step);

    CvMat *input3Ch = cvCreateMat(src.rows, src.cols, CV_32FC3);
    cvConvert(&tmp, input3Ch);
    CvMat *output1Ch = cvCreateMat(src.rows, src.cols, CV_32FC1);

    CvANN_MLP* neuron = NULL ;
    if (neuron == NULL )
        neuron = new CvANN_MLP();
	else
        neuron->clear();

    if(!ByteArrayToANN(neuralFile, neuron)){
        cerr << "trainANN :: can not load byte array to neural" << endl;
        return soap_receiver_fault(soap, "trainANN :: can not load byte array to neural", NULL);
    }

    CvMat input_nn = cvMat(input3Ch->height*input3Ch->width, 3, CV_32FC1, input3Ch->data.fl);
    CvMat output_nn = cvMat(output1Ch->height*output1Ch->width, 1, CV_32FC1, output1Ch->data.fl);
    neuron->predict(&input_nn, &output_nn);

    Mat resultNN = cvarrToMat(output1Ch, false);

    /* generate output file name */
    *OutputMatFilename = (char*)soap_malloc(soap, FILENAME_SIZE);

    time_t now = time(0);
    strftime(*OutputMatFilename, sizeof(OutputMatFilename)*FILENAME_SIZE, "/home/lluu/dir/%Y%m%d_%H%M%S_trainANN", localtime(&now));

    /* save to bin */
    if(!saveMat(*OutputMatFilename, resultNN))
    {
        cerr << "trainANN :: save mat to binary file" << endl;
        return soap_receiver_fault(soap, "trainANN :: save mat to binary file", NULL);
    }

    src.release();
    resultNN.release();
    cvReleaseMat(&output1Ch);

    end = omp_get_wtime();
    cerr<<"ns__trainANN "<<"time elapsed "<<end-start<<endl;

    return SOAP_OK;
}
int main( int argc, char** argv )
{
	// Medida de tiempo inicial
	clock_t t0, t_load, t_loop, t_loop0;
	double dtime;
	t0 = clock();

	// Declaration of variables
	CascadeClassifier face_cascade;				// Cascade Classifier
	Mat captureFrame, grayscaleFrame;			// Captured and converted to gray Frames
	double x_face_pos, y_face_pos, area_face;	// Coordinates of the detected face
	vector< Rect_<int> > faces;					// Vector of faces
	#ifdef RASPBERRY
		RaspiCamCvCapture * captureDevice;		// Video input
	#else
		CvCapture * captureDevice;				// Video input
	#endif
	char sTmp[255];

#ifdef TRACE
	sprintf(sTmp,"\n Directorio de ejecucion del programa: ");
	trace (sTmp);
	cout << get_ProgramDirectory();
#endif

#ifdef RECOGNITION
	// Declaration of variables Face recognition
	string people[MAX_PEOPLE];					// Each person of the model of face recognition
	int im_width, im_height;					// heigh, witdh of 1st images of the model of face recognition
	int prediction_seuil;						// Prediction limit
	Ptr<FaceRecognizer> model;					// Model of face recognitio

	// Prediction limit depending on the device
	#ifdef EIGENFACES
		prediction_seuil = 10;
	#else
		prediction_seuil = 1000;
	#endif

	// Model of face recognition
	#ifdef EIGENFACES
		model = createEigenFaceRecognizer();
	#else
		model = createFisherFaceRecognizer();
	#endif

	// Read measures file
	read_measures_file(im_width, im_height, '=');

	// Read people file
	read_people_file(people, '(', ')', '=');

	// Load model
	load_model_recognition(model);
	t_load = clock();

	#ifdef DEBUG
		dtime = difftime(t_load,t0);
		sprintf(sTmp,"\n (Face Tracking) tiempo de carga del modelo = ");
		debug(sTmp);
		cout << print_time(dtime);
	#endif

#endif

	// Video input depending on the device
	#ifdef RASPBERRY
		captureDevice = raspiCamCvCreateCameraCapture(0); // Index doesn't really matter
	#else
		captureDevice = cvCreateCameraCapture(0);
	#endif

	// Load of Haar Cascade
	if (!load_haar_cascade(face_cascade)) {	return -1;}

	// Create new window
	SHOW cvNamedWindow("Face tracking", 1);

	do {
		t_loop0 = clock();

		#ifdef RASPBERRY
				IplImage* image = raspiCamCvQueryFrame(captureDevice);	// Get images from the video input
		#else
				IplImage* image = cvQueryFrame(captureDevice);			// Get images from the video input
		#endif
		captureFrame = cvarrToMat(image);			// Convert images to Mat
		cvtColor(captureFrame, grayscaleFrame, CV_RGB2GRAY);	// Convert the image to gray scale

		// Detection and Face Recognition
		face_detection(face_cascade, grayscaleFrame, captureFrame, &faces, x_face_pos, y_face_pos, area_face);

		#ifdef RECOGNITION
				// Detection and Face Recognition
				face_recognition(people, grayscaleFrame, captureFrame, &faces, im_width, im_height, model,
								prediction_seuil, x_face_pos, y_face_pos, area_face);
		#endif

		// Display results
		#ifdef SHOW
				imshow("Face tracking", captureFrame);
		#endif

		t_loop = clock();
		#ifdef DEBUG
			dtime = difftime(t_loop,t_loop0);
			sprintf(sTmp,"\n (Face Tracking) tiempo del bucle del reconocimiento de cara = ");
			debug(sTmp);
			cout << print_time(dtime);
		#endif
	} while(cvWaitKey(10) < 0);

	// Destroy window
	#ifdef SHOW
		cvDestroyWindow("Face tracking");
	#endif

	#ifdef TRACE
		trace ("\n");
	#endif

	#ifdef RASPBERRY
		raspiCamCvReleaseCapture(&captureDevice);
	#else
		cvReleaseCapture(&captureDevice);
	#endif

	return 0;
}
Beispiel #15
0
static void*
imread_( const string& filename, int flags, int hdrtype, Mat* mat=0 )
{
    IplImage* image = 0;
    CvMat *matrix = 0;
    Mat temp, *data = &temp;

    ImageDecoder decoder = findDecoder(filename);
    if( decoder.empty() )
        return 0;
    decoder->setSource(filename);
    if( !decoder->readHeader() )
        return 0;

    CvSize size;
    size.width = decoder->width();
    size.height = decoder->height();

    int type = decoder->type();
    if( flags != -1 )
    {
        if( (flags & CV_LOAD_IMAGE_ANYDEPTH) == 0 )
            type = CV_MAKETYPE(CV_8U, CV_MAT_CN(type));

        if( (flags & CV_LOAD_IMAGE_COLOR) != 0 ||
                ((flags & CV_LOAD_IMAGE_ANYCOLOR) != 0 && CV_MAT_CN(type) > 1) )
            type = CV_MAKETYPE(CV_MAT_DEPTH(type), 3);
        else
            type = CV_MAKETYPE(CV_MAT_DEPTH(type), 1);
    }

    if( hdrtype == LOAD_CVMAT || hdrtype == LOAD_MAT )
    {
        if( hdrtype == LOAD_CVMAT )
        {
            matrix = cvCreateMat( size.height, size.width, type );
            temp = cvarrToMat(matrix);
        }
        else
        {
            mat->create( size.height, size.width, type );
            data = mat;
        }
    }
    else
    {
        image = cvCreateImage( size, cvIplDepth(type), CV_MAT_CN(type) );
        temp = cvarrToMat(image);
    }

    if( !decoder->readData( *data ))
    {
        cvReleaseImage( &image );
        cvReleaseMat( &matrix );
        if( mat )
            mat->release();
        return 0;
    }

    return hdrtype == LOAD_CVMAT ? (void*)matrix :
           hdrtype == LOAD_IMAGE ? (void*)image : (void*)mat;
}
Beispiel #16
0
static void*
imdecode_( const Vector<uchar>& buf, int flags, int hdrtype, Mat* mat=0 )
{
    IplImage* image = 0;
    CvMat *matrix = 0;
    Mat temp, *data = &temp;
    char fnamebuf[L_tmpnam];
    const char* filename = 0;

    ImageDecoder decoder = findDecoder(buf);
    if( !decoder.obj )
        return 0;

    if( !decoder->setSource(buf) )
    {
        filename = tmpnam(fnamebuf);
        FILE* f = fopen( filename, "wb" );
        if( !f )
            return 0;
        fwrite( &buf[0], 1, buf.size(), f );
        fclose(f);
        decoder->setSource(filename);
    }

    if( !decoder->readHeader() )
    {
        if( filename )
            unlink(filename);
        return 0;
    }

    CvSize size;
    size.width = decoder->width();
    size.height = decoder->height();

    int type = decoder->type();
    if( flags != -1 )
    {
        if( (flags & CV_LOAD_IMAGE_ANYDEPTH) == 0 )
            type = CV_MAKETYPE(CV_8U, CV_MAT_CN(type));

        if( (flags & CV_LOAD_IMAGE_COLOR) != 0 ||
           ((flags & CV_LOAD_IMAGE_ANYCOLOR) != 0 && CV_MAT_CN(type) > 1) )
            type = CV_MAKETYPE(CV_MAT_DEPTH(type), 3);
        else
            type = CV_MAKETYPE(CV_MAT_DEPTH(type), 1);
    }

    if( hdrtype == LOAD_CVMAT || hdrtype == LOAD_MAT )
    {
        if( hdrtype == LOAD_CVMAT )
        {
            matrix = cvCreateMat( size.height, size.width, type );
            temp = cvarrToMat(matrix);
        }
        else
        {
            mat->create( size.height, size.width, type );
            data = mat;
        }
    }
    else
    {
        image = cvCreateImage( size, cvIplDepth(type), CV_MAT_CN(type) );
        temp = cvarrToMat(image);
    }

    bool code = decoder->readData( *data );
    if( filename )
        unlink(filename);

    if( !code )
    {
        cvReleaseImage( &image );
        cvReleaseMat( &matrix );
        if( mat )
            mat->release();
        return 0;
    }

    return hdrtype == LOAD_CVMAT ? (void*)matrix :
        hdrtype == LOAD_IMAGE ? (void*)image : (void*)mat;
}
//-----------------------------------------------------
void ofxSURFTracker::detect(unsigned char *pix, int inputWidth, int inputHeight) {

    /***
    code adapted from http://docs.opencv.org/doc/tutorials/features2d/feature_homography/feature_homography.html
     ***/

    if( inputWidth != inputImg.getWidth() || inputHeight != inputImg.getHeight()) {
        // this should only happen once
        inputImg.clear();
        inputImg.allocate(inputWidth, inputHeight);
        cout << "ofxSURFTracker : re-allocated the input image."<<endl;
    }

    // create the cvImage from the ofImage
    inputImg.setFromPixels(pix, inputWidth, inputHeight);
    inputImg.setROI( ofRectangle((inputWidth-width)/2,
                                 (inputHeight-height)/2,
                                 width,
                                 height
                                )
                   );

    // take out the piece that we want to use.
    croppedImg.setFromPixels(inputImg.getRoiPixels(), width, height);

    // make it into a trackable grayscale image
    trackImg = croppedImg;

    // do some fancy contrast stuff
    if(bContrast) {
        trackImg.contrastStretch();
    }

    // set up the feature detector
    detector =  SurfFeatureDetector(hessianThreshold,
                                    octaves,
                                    octaveLayers,
                                    bUpright);

    // clear existing keypoints from previous frame
    keypoints_scene.clear();

    // get the Mat to do the feature detection on
    Mat trackMat = cvarrToMat(trackImg.getCvImage());
    detector.detect( trackMat, keypoints_scene);

    // Calculate descriptors (feature vectors)
    extractor.compute( trackMat, keypoints_scene, descriptors_scene );

    // Matching descriptor vectors using FLANN matcher
    vector< DMatch > matches;
    if(!descriptors_object.empty() && !descriptors_scene.empty() ) {
        flannMatcher.match( descriptors_object, descriptors_scene, matches);
    }

    // Quick calculation of max and min distances between keypoints
    double max_dist = 0;
    double min_dist = 100;
    for( int i = 0; i < matches.size(); i++ ) {
        double dist = matches[i].distance;
        if( dist < min_dist ) min_dist = dist;
        if( dist > max_dist ) max_dist = dist;
    }

    // Filter matches upon quality : distance is between 0 and 1, lower is better
    good_matches.clear();
    for( int i = 0; i < matches.size(); i++ ) {
        if(matches[i].distance < 3 * min_dist && matches[i].distance < distanceThreshold) {
            good_matches.push_back( matches[i]);
        }
    }

    // find the homography
    // transform the bounding box for this scene
    vector <Point2f> scene_pts;
    vector <Point2f> object_pts;
    object_transformed.clear();
    if(good_matches.size() > minMatches) {
        for( int i = 0; i < good_matches.size(); i++ )
        {
            //-- Get the keypoints from the good matches
            object_pts.push_back( keypoints_object[ good_matches[i].queryIdx ].pt );
            scene_pts.push_back( keypoints_scene[ good_matches[i].trainIdx ].pt );
        }
        if( scene_pts.size() >5 && object_pts.size() > 5) {
            homography = findHomography( object_pts, scene_pts, CV_RANSAC);
            perspectiveTransform( object, object_transformed, homography);
        }
        // being here means we have found a decent match
        objectLifeTime += 0.05;

    } else {
        // we haven't found a decent match
        objectLifeTime -= 0.05;
    }
    if(objectLifeTime > 1) {
        objectLifeTime = 1;
    } else if( objectLifeTime < 0) {
        objectLifeTime = 0;
    }
}
Beispiel #18
0
void AAM::countA()
{
    Mat deltaC;
    Mat deltaI;
    for(int i=0; i<this->shapeSet.rows; i++)
    {

        for(int j=0; j<100; j++)
        {
            Mat ds=randomPointsMat(40, this->shapeSet.cols);
            cout<<"random ds: "<<ds<<endl;
            ds.convertTo(ds, this->shapeSet.type());
            Mat newShape=ds+this->shapeSet.row(i);
            newShape.convertTo(newShape, meanShape.type());
            Mat newShapeParams=newShape-meanShape;
            newShapeParams=newShapeParams*this->w;
            Mat image_grey;
            cvtColor(this->images[i], image_grey, COLOR_BGR2GRAY);
            Mat newTexture=this->getTetureInShape(image_grey, newShape);
            newTexture.convertTo(newTexture, meanTexture.type());
            Mat newDeltaTexture=newTexture - meanTexture;
            Mat newTextureParams=this->textureVariationPCA.project(newDeltaTexture);
            Mat newAp;
            newShapeParams.convertTo(newShapeParams, newTextureParams.type());
            hconcat(newShapeParams, newTextureParams, newAp);
            cout<<"new appearance: " <<newAp<<endl;
            Mat c=this->appearancePCA.project(newAp);
            cout<<"new c: "<<c<<endl;
            c.convertTo(c, cSet.type());
            Mat dc=this->cSet.row(i)-c;
            cout<<"dc: "<<dc<<endl;

            Mat ap = this->appearancePCA.backProject(c);
            CvMat apMat=ap;
            cout<<"appearance: "<<ap<<endl;
            CvMat shapeParams;
            cvGetCols(&apMat, &shapeParams, 0, shapeSet.cols);
            cout<<"shape: "<<endl;
            Mat shape=cvarrToMat(&shapeParams);
            cout<<"shapeParams: "<<shape<<endl;
            shape=shape/this->w;
            cout<<"shapeParams scaled: "<<shape<<endl;
            CvMat textureParams;
            cvGetCols(&apMat, &textureParams, shapeSet.cols, shapeSet.cols+this->b_g.cols);
            Mat texture=cvarrToMat(&textureParams);
            cout<<"texture Params: "<<texture<<endl;
            shape.convertTo(shape, meanShape.type());
            Mat modelShape=this->meanShape + shape;
            modelShape=this->cutToRange(modelShape, 0, this->ImageHeight);
            cout<<"modelShape: "<<modelShape<<endl;
            Mat backProjectShape=this->textureVariationPCA.backProject(texture);
            backProjectShape.convertTo(backProjectShape, meanTexture.type());
            Mat modelTexture=this->meanTexture + backProjectShape;
            modelTexture.convertTo(modelTexture, CV_8U);
            modelTexture.convertTo(modelTexture, CV_32S);
            //cout<<"modelTexture: "<<modelTexture<<endl;
            Mat warped=this->warpImage(this->images[i], modelShape);
            cvtColor(warped, image_grey, COLOR_BGR2GRAY);
            Mat convHull = this->createConvexHull(this->meanPoints);
            Mat realTexture=this->getTextureInsideHull(image_grey, convHull);
            Mat realTextureSampled=this->sampleMat(realTexture, sampling);
            normalize(realTextureSampled, realTextureSampled, 0, 255, NORM_MINMAX);
            cout<<realTexture.cols<<" "<<realTextureSampled.cols<<"  "<<modelTexture.cols<<endl;
            realTextureSampled.convertTo(realTextureSampled, modelTexture.type());
            Mat dI=realTextureSampled-modelTexture;
            deltaC.push_back(dc);
            deltaI.push_back(dI);
        }
    }
    cout<<"deltaI : " <<deltaI<<endl;
    cout<<"deltaC: "<<deltaC<<endl;
    this->A=this->findLinearAproximation(deltaI, deltaC);
    Mat deltaItest;
    deltaI.row(0).convertTo(deltaItest, A.type());
    cout<<"delta I row: "<<deltaItest.row(0)<<endl;
    cout<<"delta c row: "<<deltaC.row(0)<<endl;
    //cout<<this->A.type()<<endl;
    //cout<<deltaI.type()<<endl;
    Mat result=deltaItest.row(0)*A;
    cout<<result<<endl;
}
Beispiel #19
0
vector<Point> AAM::findPoints(Mat image)
{
    float k=10;
    int i=0;
    Mat convHull = this->createConvexHull(this->meanPoints);
    Mat points;
    cout<<"mean shape"<<this->meanShape<<endl;
    this->meanShape.copyTo(points);
    cout<<"initial shape"<<points<<endl;
    Mat deltaG;
    double normE0;
    Mat c;
    do
    {
        cout<<"iteration "<<i<<endl;
        cout<<"start Points: "<<points<<endl;
        k=1;
        //this->drawPoints(image, points, QString::number(i).toStdString());
        Mat textureSampled=this->getTetureInShape(image, points);
        textureSampled.convertTo(textureSampled, CV_32S);
        this->meanTexture.convertTo(meanTexture, CV_32S);
        Mat deltaT = textureSampled - this->meanTexture;
        points.convertTo(points, meanShape.type());
        Mat deltaPoint = points-meanShape;
       // cout<<"deltaPoint: "<<deltaPoint<<endl;
        deltaPoint=deltaPoint*this->w;
       // cout<<"deltaPoint: "<<deltaPoint<<endl;
        Mat b=this->textureVariationPCA.project(deltaT);
       // cout<<"b for texture: "<<b<<endl;
        Mat ap;
        deltaPoint.convertTo(deltaPoint, b.type());
        hconcat(deltaPoint, b, ap);
       // cout<<"appearance: "<<ap<<endl;
        c= this->appearancePCA.project(ap);
       // cout<<"c: "<<c<<endl;
        Mat modelAp=this->appearancePCA.backProject(c);
       // cout<<"model appearance: "<<modelAp<<endl;
        CvMat bModelMat;
        CvMat modelApMat=modelAp;
        cvGetCols(&modelApMat, &bModelMat, this->shapeSet.cols, this->shapeSet.cols + this->b_g.cols);
        Mat bModel=cvarrToMat(&bModelMat);
        bModel.convertTo(bModel, CV_32F);
       // cout<<"b model"<<bModel<<endl;
        Mat deltaGModel=this->textureVariationPCA.backProject(bModel);
       // cout<<"delta g model: "<<deltaGModel<<endl;
        deltaGModel.convertTo(deltaGModel, this->meanTexture.type());
        Mat textureModel=this->meanTexture + deltaGModel;
        textureModel.convertTo(textureModel, CV_8U);
        textureModel.convertTo(textureModel, CV_32S);
       // cout<<"texture model: "<<textureModel<<endl;
        Mat modelShape=this->getShapeParamsFromC(c);
        modelShape.convertTo(modelShape, this->meanShape.type());
        modelShape=meanShape + modelShape;
        modelShape=this->cutToRange(modelShape, 0, this->ImageHeight);
       // cout<<"model shape: "<<modelShape<<endl;
        Mat realTexture=this->getTetureInShape(image, modelShape);
        realTexture.convertTo(realTexture, textureModel.type());
       // cout<<"real texture"<<realTexture<<endl;
        Mat deltaI=realTexture-textureModel;
       // cout<<"deltaI: "<<deltaI<<endl;
        deltaI.convertTo(deltaI, CV_32F);
        Mat deltaC=deltaI*this->A;
        cout<<"delta c"<<deltaC<<endl;
        c.convertTo(c, deltaC.type());
        normE0=this->euqlidesNorm(deltaI);
        double normE=normE0;
        Mat newC;
        while(normE0<=normE && k>0.0001) //sprawdzić, czy deltaG jest mniejsze niż deltaG0
        {
           // cout<<"k "<<k<<endl;
            newC=c+k*deltaC;
            cout<<"newc: "<<newC<<endl;
            Mat shapeParams=this->getShapeParamsFromC(newC);
            Mat textureParams=this->getTextureParamsFromC(newC);
            Mat textureDelta=this->textureVariationPCA.backProject(textureParams);
            textureDelta.convertTo(textureDelta, meanTexture.type());
            Mat texture=meanTexture + textureDelta;
            shapeParams.convertTo(shapeParams, meanShape.type());
            points=meanShape + shapeParams;
            points=this->cutToRange(points, 0, this->ImageHeight);
            Mat realTexture=this->getTetureInShape(image, points);
            texture.convertTo(texture, realTexture.type());
            deltaI=realTexture - texture;
            normE=this->euqlidesNorm(deltaI);
            k=k/2;
        }
        i++;
        newC.copyTo(c);
        normE0=normE;
        cout<<"points: "<<points<<endl;
        cout<<"error: "<<normE0<<endl;
    }
    while(i<30); // sprwdzić, czy błąd deltaG jest mniejszy od maxymalnego błędu
    this->drawPoints(image, points, "final points");
    cout<<"final points: "<<points<<endl;
    cout<<"mean:         "<<this->meanShape<<endl;
    return this->convertMatToPoints(points);
}
Beispiel #20
0
/**
 * Read an image into memory and return the information
 *
 * @param[in] filename File to load
 * @param[in] flags Flags
 * @param[in] hdrtype { LOAD_CVMAT=0,
 *                      LOAD_IMAGE=1,
 *                      LOAD_MAT=2
 *                    }
 * @param[in] mat Reference to C++ Mat object (If LOAD_MAT)
 * @param[in] scale_denom Scale value
 *
*/
static void*
imread_( const String& filename, int flags, int hdrtype, Mat* mat=0 )
{
    IplImage* image = 0;
    CvMat *matrix = 0;
    Mat temp, *data = &temp;

    /// Search for the relevant decoder to handle the imagery
    ImageDecoder decoder;

#ifdef HAVE_GDAL
    if(flags != IMREAD_UNCHANGED && (flags & IMREAD_LOAD_GDAL) == IMREAD_LOAD_GDAL ){
        decoder = GdalDecoder().newDecoder();
    }else{
#endif
        decoder = findDecoder( filename );
#ifdef HAVE_GDAL
    }
#endif

    /// if no decoder was found, return nothing.
    if( !decoder ){
        return 0;
    }

    int scale_denom = 1;
    if( flags > IMREAD_LOAD_GDAL )
    {
    if( flags & IMREAD_REDUCED_GRAYSCALE_2 )
        scale_denom = 2;
    else if( flags & IMREAD_REDUCED_GRAYSCALE_4 )
        scale_denom = 4;
    else if( flags & IMREAD_REDUCED_GRAYSCALE_8 )
        scale_denom = 8;
    }

    /// set the scale_denom in the driver
    decoder->setScale( scale_denom );

    /// set the filename in the driver
    decoder->setSource( filename );

   // read the header to make sure it succeeds
   if( !decoder->readHeader() )
        return 0;

    // established the required input image size
    CvSize size;
    size.width = decoder->width();
    size.height = decoder->height();

    // grab the decoded type
    int type = decoder->type();
    if( (flags & IMREAD_LOAD_GDAL) != IMREAD_LOAD_GDAL && flags != IMREAD_UNCHANGED )
    {
        if( (flags & CV_LOAD_IMAGE_ANYDEPTH) == 0 )
            type = CV_MAKETYPE(CV_8U, CV_MAT_CN(type));

        if( (flags & CV_LOAD_IMAGE_COLOR) != 0 ||
           ((flags & CV_LOAD_IMAGE_ANYCOLOR) != 0 && CV_MAT_CN(type) > 1) )
            type = CV_MAKETYPE(CV_MAT_DEPTH(type), 3);
        else
            type = CV_MAKETYPE(CV_MAT_DEPTH(type), 1);
    }

    if( hdrtype == LOAD_CVMAT || hdrtype == LOAD_MAT )
    {
        if( hdrtype == LOAD_CVMAT )
        {
            matrix = cvCreateMat( size.height, size.width, type );
            temp = cvarrToMat( matrix );
        }
        else
        {
            mat->create( size.height, size.width, type );
            data = mat;
        }
    }
    else
    {
        image = cvCreateImage( size, cvIplDepth(type), CV_MAT_CN(type) );
        temp = cvarrToMat( image );
    }

    // read the image data
    if( !decoder->readData( *data ))
    {
        cvReleaseImage( &image );
        cvReleaseMat( &matrix );
        if( mat )
            mat->release();
        return 0;
    }

    if( decoder->setScale( scale_denom ) > 1 ) // if decoder is JpegDecoder then decoder->setScale always returns 1
    {
        resize( *mat, *mat, Size( size.width / scale_denom, size.height / scale_denom ) );
    }

    return hdrtype == LOAD_CVMAT ? (void*)matrix :
        hdrtype == LOAD_IMAGE ? (void*)image : (void*)mat;
}