Example #1
0
int main(int argc, char** argv) {
    const int RESIZE_FACTOR = 2;
    const string DATA_PATH = "/Users/Mike/Documents/Eclipse Workspace/SCB3/data/";
    const string OUTPUT_PATH = "/Users/Mike/Documents/Eclipse Workspace/SCB3/output/";

    // 0, 1, 5, 6, I, O and S are never used
    const string ALLOWED_CHARS = "234789ABCDEFGHJKLMNPQRTUVWXYZ@&%";

    // Images
    Mat sourceImage, finalImage, histogramImage;
    Mat histogram;

    // Initialize character counters
    map<string, int> counter;
    for(int i = 0; i < ALLOWED_CHARS.length(); i++) {
        string letter(1, ALLOWED_CHARS[i]);
        counter[letter] = 0;
    }

    // Check if data folder exists
    fs::path folder(DATA_PATH);
    if(!exists(folder))
        return -1;

    // Create output folder structure
    if(!createFolderStructure(OUTPUT_PATH, ALLOWED_CHARS))
        return -1;

    fs::directory_iterator endItr;
    for(fs::directory_iterator itr(folder); itr != endItr; itr++) {
        string fullPath = itr->path().string();
        string fileName = itr->path().filename().string();

        // Skip all dot files
        if(fileName[0] == '.')
            continue;

        // Retrieve captcha string
        string captchaCode = boost::replace_all_copy(fileName, ".png", "");
        captchaCode = aliasToSpecialChar(captchaCode);

        // Load our base image
        sourceImage = imread(fullPath, CV_LOAD_IMAGE_GRAYSCALE);

        // Is it loaded?
        if(!sourceImage.data)
            return -1;

        // Resize the image by resize factor
        resize(sourceImage, sourceImage, Size(sourceImage.cols * RESIZE_FACTOR, sourceImage.rows * RESIZE_FACTOR));

        // Define our final image
        finalImage = sourceImage.clone();

        // Apply adaptive threshold
        adaptiveThreshold(finalImage, finalImage, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 3, 1);

        // Use the thresholded image as a mask
        Mat tmp;
        sourceImage.copyTo(tmp, finalImage);
        tmp.copyTo(finalImage);
        tmp.release();

        // Let's calculate histogram for our image
        histogram = createHistogram(finalImage);

        // Calculate final threshold value
        int thresholdValue = getIdealThreshold(histogram);

        // Draw histogram image
        histogramImage = drawHistogram(histogram, thresholdValue);

        // Apply binary threshold
        threshold(finalImage, finalImage, thresholdValue, 255, THRESH_BINARY);

        // Morphological closing
        Mat element = getStructuringElement(MORPH_ELLIPSE, Size(3, 3));
        dilate(finalImage, finalImage, element);
        erode(finalImage, finalImage, element);

        // Segments
        int* segH = horizontalSegments(finalImage);
        int* segV = verticalSegments(finalImage);

        Mat segHImage = drawHorizontalSegments(segH, finalImage.rows, finalImage.cols);
        Mat segVImage = drawVerticalSegments(segV, finalImage.rows, finalImage.cols);

        // Create pairs
        vector<pair<int, int> > verticalPairs = filterVerticalPairs(createSegmentPairs(segV, finalImage.rows));
        vector<pair<int, int> > horizontalPairs = splitLarge(filterHorizontalPairs(createSegmentPairs(segH, finalImage.cols), finalImage.cols));

        // Get segment squares
        vector<Rectangle> squares = takeRectangles(shrinkRectangles(finalImage, getRectangles(verticalPairs, horizontalPairs)), 6);

        // Save the squares
        saveRectangles(sourceImage, squares, OUTPUT_PATH, captchaCode, counter);

        // Let's draw the rectangles
        drawRectangles(finalImage, squares);
        drawRectangles(sourceImage, squares);

        /*
        // Display the images if necessary

        imshow("Final image", finalImage);
        imshow("Source image", sourceImage);
        imshow("HSeg", segHImage);
        imshow("VSeg", segVImage);
        imshow("Histogram", histogramImage);
        waitKey();
        */

        sourceImage.release();
        finalImage.release();
    }

    Mat trainingData, classLabels;

    // Create training data
#if SIMPLE_DESCRIPTORS == 1
    getSimpleTrainingData(trainingData, classLabels, OUTPUT_PATH, "G", "Y", 25);
#else
    getHOGTrainingData(trainingData, classLabels, OUTPUT_PATH, "G", "Y", 25);
#endif

    // This part is highly simplified and was mostly done just to test
    // classification based on two characters with the highest frequency
    CvSVMParams params;
    params.svm_type = CvSVM::C_SVC;
    params.kernel_type = CvSVM::LINEAR;
    params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);

    CvSVM SVM;
    SVM.train(trainingData, classLabels, Mat(), Mat(), params);

    int success = 0;
    for(int i = 25; i < 29; i++) {
        Mat letterImage = imread(OUTPUT_PATH + "G/" + to_string(i) + ".png", CV_LOAD_IMAGE_GRAYSCALE);
        float result = classify(SVM, letterImage);

        if(result == 1) {
            cout << "Letter G classified as: G" << endl;
            success++;
        } else
            cout << "Letter G classified as: Y" << endl;

        letterImage.release();

        letterImage = imread(OUTPUT_PATH + "Y/" + to_string(i) + ".png", CV_LOAD_IMAGE_GRAYSCALE);
        result = classify(SVM, letterImage);

        if(result == -1) {
            cout << "Letter Y classified as: Y" << endl;
            success++;
        } else
            cout << "Letter Y classified as: G" << endl;

        letterImage.release();
    }

    trainingData.release();
    classLabels.release();

    cout << "Success rate: " << success << "/8" << endl;

    return 0;
}
Example #2
0
int main(int argc, char *argv[]){
        
        //hue values for blue: 105 120
        //hue values for red: 160 180
        //hue values for green: 70 100

        //values to filter the Hue value
        int minHue = 70, maxHue = 100;
       
        //main Mat object
	Mat m;

        for(int i = 0; i < argc ; i++){

                //argument for the hue values when doing an inRange function
                if((string(argv[i])) == "-h"){
                        minHue = atoi(string(argv[i + 1]).c_str());
                        maxHue = atoi(string(argv[i + 2]).c_str());                       
                }

                //argument for getting the image from feed or command line argument
                 if((string(argv[i])) == "-c"){
                         //code to get the picture from camera feed
                         VideoCapture cap("rtsp://192.168.0.100/axis-media/media.amp");
                        camerafeed = true;
                         if(!cap.isOpened()){
                                wcout << "Unable to get camera";
                                return -1;    
                         }
        
                        m.release();
                        cap.read(m);
                }
                 
                 //argument to get the picture from a file
                 else if((string(argv[i])) == "-f"){
                        m = imread(argv[i + 1]);
                }
                       
        }
        if(runtimeFeed){
                thread runtimeFeedThread(displayFeedContinuously);
        
                runtimeFeedThread.join();
        }
        
        //check what properties it needs in order to be a mask
        //I think it has to be a binary image
        Mat greenFiltered;

        //HSV Filtering and histogram generation
        HSVFilter hFilter(m);
        
        hFilter.hueHistogram();

        Mat HSVImage = m.clone();

        Mat binImage = m.clone();
        
        //convert the color arrangement from RGB to HSV format
        cvtColor(HSVImage, HSVImage, CV_BGR2HSV);        
         
        //filter the image Hue              normally: 200
        inRange(HSVImage, Scalar(minHue, 0, 50), Scalar(maxHue, 255, 255), binImage);
        
        //save this code for general purposes
        Mat kernel = (Mat_<double>(5, 5) <<         
                        1, 1, 1, 1, 1,
                        1, 1, 1, 1, 1,
                        1, 1, 1, 1, 1,
                        1, 1, 1, 1, 1
                        );
              
        
        //make sure that it is the right channel.
        kernel.convertTo(kernel, CV_8U);
            
        //reduces the noise
        erode(binImage, binImage, kernel);
        dilate(binImage, binImage, kernel);

        namedWindow("Original", 1);
        namedWindow("Ranged", 1);

        imshow( "Original", m);
        if(camerafeed){
                imwrite("FRC_CAMERA_IMAGE.png", m);
        }
	imshow("Ranged", binImage);
	waitKey(0);
        
        return 0;
}
Example #3
0
	/*
	This function is to remove all rows and cols which have no black pixels.
	If input is RGB or GRAY, it will be thresholded to Binary for checking the no. of black pixels.
	This thresholding is done using THRESH_OTSU by default, but can be done at different level by passing it as "thresh"
	Even if thresholding is done, the output will still be the same format as input.

	Inputs
	Word input - The Word containing the Image to be processed.
	int thresh - If input is RGB/GRAY, this value is used for thresholding. Default value = OTSU.
	CropType   - Enum Specifying the type of Cropping.
	*/
	static Mat crop(Mat inputImage, int thresh = 0, CropType ct = CROP_EXTERNAL)
	{
		//Mat inputImage = input.getImage(); //Input image passed thorugh Word Object.
		Mat threshImage; //Thresholded Image used for checking no of black pixels.
		Mat tempImage; //Temp Image used in between eliminating empty rows and empty columns
		Mat outputImage; //Output Image that will be Returned as Word Object
		switch (ct)
		{
		case CROP_EXTERNAL:


			//Copy the inputImage into threshImage and tempImage
			threshImage = inputImage.clone();
			tempImage = inputImage.clone();

			//If its a 3 channel image, convert to single channel image
			if (threshImage.channels() == 3)
				cvtColor(threshImage, threshImage, CV_RGB2GRAY);

			//Threshold the single channel image
			threshold(threshImage, threshImage, thresh, 255, THRESH_BINARY | THRESH_OTSU);

			//Start from i=0.
			//If a row has a black pixel, add all rows after that row to tempImage
			for (int i = 0; i<threshImage.rows; i++)
			{
				if (threshImage.cols - (sum(threshImage.row(i))(0) / 255) > 0)
				{
					for (int j = i; j<threshImage.rows; j++)
						outputImage.push_back(Mat(tempImage.row(j)));
					break;
				}
			}

			//Copy the outputImage into threshImage and tempImage
			threshImage = outputImage.clone();
			tempImage = outputImage.clone();
			//Destroy the outputImage
			outputImage.release();
			//If its a 3 channel image, convert to single channel image
			if (threshImage.channels() == 3)
				cvtColor(threshImage, threshImage, CV_RGB2GRAY);
			//Threshold the single channel image
			threshold(threshImage, threshImage, thresh, 255, THRESH_BINARY | THRESH_OTSU);

			if (!threshImage.data)
				return Mat();
			if ((threshImage.rows == 0) || (threshImage.cols == 0))
				return Mat();


			//Start from i=threshImage.rows-1.
			//If a row has a black pixel, add all rows after that row to tempImage
			for (int i = threshImage.rows - 1; i >= 0; i--)
			{
				if (threshImage.cols - (sum(threshImage.row(i))(0) / 255) > 0)
				{
					for (int j = 0; j <= i; j++)
						outputImage.push_back(Mat(tempImage.row(j)));
					break;
				}
			}

			//Transpose, so that cols become rows. Then we can check for white columns by checking for white rows.
			outputImage = outputImage.t(); //Transpose the Matrix

			//Copy the outputImage into threshImage and tempImage
			threshImage = outputImage.clone();
			tempImage = outputImage.clone();

			//Destroy the outputImage
			outputImage.release();

			//If its a 3 channel image, convert to single channel image
			if (threshImage.channels() == 3)
				cvtColor(threshImage, threshImage, CV_RGB2GRAY);
			//Threshold the single channel image
			threshold(threshImage, threshImage, thresh, 255, THRESH_BINARY | THRESH_OTSU);

			//Start from i=0.
			//If a row has a black pixel, add all rows after that row to tempImage
			for (int i = 0; i<threshImage.rows; i++)
			{
				if (threshImage.cols - (sum(threshImage.row(i))(0) / 255) > 0)
				{
					for (int j = i; j<threshImage.rows; j++)
						outputImage.push_back(Mat(tempImage.row(j)));
					break;
				}
			}
			//Copy the outputImage into threshImage and tempImage
			threshImage = outputImage.clone();
			tempImage = outputImage.clone();
			//Destroy the outputImage
			outputImage.release();
			//If its a 3 channel image, convert to single channel image
			if (threshImage.channels() == 3)
				cvtColor(threshImage, threshImage, CV_RGB2GRAY);
			//Threshold the single channel image
			threshold(threshImage, threshImage, thresh, 255, THRESH_BINARY | THRESH_OTSU);
			//Start from i=threshImage.rows-1.
			//If a row has a black pixel, add all rows after that row to tempImage
			for (int i = threshImage.rows - 1; i >= 0; i--)
			{
				if (threshImage.cols - (sum(threshImage.row(i))(0) / 255) > 0)
				{
					for (int j = 0; j <= i; j++)
						outputImage.push_back(Mat(tempImage.row(j)));
					break;
				}
			}

			//Transpose, to get back the Image in proper orientation.
			outputImage = outputImage.t();

			//Return the output as a Word object
			return outputImage;



		}
	}
	~FlashDriveFinderNode() {
		cv::destroyWindow("Image");
		imgColor.release();
		imgGray.release();
	}
void CV_StereoMatchingTest::run(int)
{
    string dataPath = ts->get_data_path();
    string algorithmName = name;
    assert( !algorithmName.empty() );
    if( dataPath.empty() )
    {
        ts->printf( cvtest::TS::LOG, "dataPath is empty" );
        ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ARG_CHECK );
        return;
    }

    FileStorage datasetsFS( dataPath + DATASETS_DIR + DATASETS_FILE, FileStorage::READ );
    int code = readDatasetsParams( datasetsFS );
    if( code != cvtest::TS::OK )
    {
        ts->set_failed_test_info( code );
        return;
    }
    FileStorage runParamsFS( dataPath + ALGORITHMS_DIR + algorithmName + RUN_PARAMS_FILE, FileStorage::READ );
    code = readRunParams( runParamsFS );
    if( code != cvtest::TS::OK )
    {
        ts->set_failed_test_info( code );
        return;
    }
    
    string fullResultFilename = dataPath + ALGORITHMS_DIR + algorithmName + RESULT_FILE;
    FileStorage resFS( fullResultFilename, FileStorage::READ );
    bool isWrite = true; // write or compare results
    if( resFS.isOpened() )
        isWrite = false;
    else
    {
        resFS.open( fullResultFilename, FileStorage::WRITE );
        if( !resFS.isOpened() )
        {
            ts->printf( cvtest::TS::LOG, "file %s can not be read or written\n", fullResultFilename.c_str() );
            ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ARG_CHECK );
            return;
        }
        resFS << "stereo_matching" << "{";
    }

    int progress = 0, caseCount = (int)caseNames.size();
    for( int ci = 0; ci < caseCount; ci++)
    {
        progress = update_progress( progress, ci, caseCount, 0 );
        printf("progress: %d%%\n", progress);
        fflush(stdout);
        string datasetName = caseDatasets[ci];
        string datasetFullDirName = dataPath + DATASETS_DIR + datasetName + "/";
        Mat leftImg = imread(datasetFullDirName + LEFT_IMG_NAME);
        Mat rightImg = imread(datasetFullDirName + RIGHT_IMG_NAME);
        Mat trueLeftDisp = imread(datasetFullDirName + TRUE_LEFT_DISP_NAME, 0);
        Mat trueRightDisp = imread(datasetFullDirName + TRUE_RIGHT_DISP_NAME, 0);

        if( leftImg.empty() || rightImg.empty() || trueLeftDisp.empty() )
        {
            ts->printf( cvtest::TS::LOG, "images or left ground-truth disparities of dataset %s can not be read", datasetName.c_str() );
            code = cvtest::TS::FAIL_INVALID_TEST_DATA;
            continue;
        }
        int dispScaleFactor = datasetsParams[datasetName].dispScaleFactor;
        Mat tmp; trueLeftDisp.convertTo( tmp, CV_32FC1, 1.f/dispScaleFactor ); trueLeftDisp = tmp; tmp.release();
        if( !trueRightDisp.empty() )
            trueRightDisp.convertTo( tmp, CV_32FC1, 1.f/dispScaleFactor ); trueRightDisp = tmp; tmp.release();

        Mat leftDisp, rightDisp;
        int ignBorder = max(runStereoMatchingAlgorithm(leftImg, rightImg, leftDisp, rightDisp, ci), EVAL_IGNORE_BORDER);
        leftDisp.convertTo( tmp, CV_32FC1 ); leftDisp = tmp; tmp.release();
        rightDisp.convertTo( tmp, CV_32FC1 ); rightDisp = tmp; tmp.release();

        int tempCode = processStereoMatchingResults( resFS, ci, isWrite,
                   leftImg, rightImg, trueLeftDisp, trueRightDisp, leftDisp, rightDisp, QualityEvalParams(ignBorder));
        code = tempCode==cvtest::TS::OK ? code : tempCode;
    }

    if( isWrite )
        resFS << "}"; // "stereo_matching"

    ts->set_failed_test_info( code );
}
static void build3dmodel( const Ptr<FeatureDetector>& detector,
                          const Ptr<DescriptorExtractor>& descriptorExtractor,
                          const vector<Point3f>& /*modelBox*/,
                          const vector<string>& imageList,
                          const vector<Rect>& roiList,
                          const vector<Vec6f>& poseList,
                          const Mat& cameraMatrix,
                          PointModel& model )
{
    int progressBarSize = 10;

    const double Feps = 5;
    const double DescriptorRatio = 0.7;

    vector<vector<KeyPoint> > allkeypoints;
    vector<int> dstart;
    vector<float> alldescriptorsVec;
    vector<Vec2i> pairwiseMatches;
    vector<Mat> Rs, ts;
    int descriptorSize = 0;
    Mat descriptorbuf;
    Set2i pairs, keypointsIdxMap;

    model.points.clear();
    model.didx.clear();

    dstart.push_back(0);

    size_t nimages = imageList.size();
    size_t nimagePairs = (nimages - 1)*nimages/2 - nimages;

    printf("\nComputing descriptors ");

    // 1. find all the keypoints and all the descriptors
    for( size_t i = 0; i < nimages; i++ )
    {
        Mat img = imread(imageList[i], 1), gray;
        cvtColor(img, gray, COLOR_BGR2GRAY);

        vector<KeyPoint> keypoints;
        detector->detect(gray, keypoints);
        descriptorExtractor->compute(gray, keypoints, descriptorbuf);
        Point2f roiofs = roiList[i].tl();
        for( size_t k = 0; k < keypoints.size(); k++ )
            keypoints[k].pt += roiofs;
        allkeypoints.push_back(keypoints);

        Mat buf = descriptorbuf;
        if( !buf.isContinuous() || buf.type() != CV_32F )
        {
            buf.release();
            descriptorbuf.convertTo(buf, CV_32F);
        }
        descriptorSize = buf.cols;

        size_t prev = alldescriptorsVec.size();
        size_t delta = buf.rows*buf.cols;
        alldescriptorsVec.resize(prev + delta);
        std::copy(buf.ptr<float>(), buf.ptr<float>() + delta,
                  alldescriptorsVec.begin() + prev);
        dstart.push_back(dstart.back() + (int)keypoints.size());

        Mat R, t;
        unpackPose(poseList[i], R, t);
        Rs.push_back(R);
        ts.push_back(t);

        if( (i+1)*progressBarSize/nimages > i*progressBarSize/nimages )
        {
            putchar('.');
            fflush(stdout);
        }
    }

    Mat alldescriptors((int)alldescriptorsVec.size()/descriptorSize, descriptorSize, CV_32F,
                       &alldescriptorsVec[0]);

    printf("\nOk. total images = %d. total keypoints = %d\n",
           (int)nimages, alldescriptors.rows);

    printf("\nFinding correspondences ");

    int pairsFound = 0;

    vector<Point2f> pts_k(2);
    vector<Mat> Rs_k(2), ts_k(2);
    //namedWindow("img1", 1);
    //namedWindow("img2", 1);

    // 2. find pairwise correspondences
    for( size_t i = 0; i < nimages; i++ )
        for( size_t j = i+1; j < nimages; j++ )
        {
            const vector<KeyPoint>& keypoints1 = allkeypoints[i];
            const vector<KeyPoint>& keypoints2 = allkeypoints[j];
            Mat descriptors1 = alldescriptors.rowRange(dstart[i], dstart[i+1]);
            Mat descriptors2 = alldescriptors.rowRange(dstart[j], dstart[j+1]);

            Mat F = getFundamentalMat(Rs[i], ts[i], Rs[j], ts[j], cameraMatrix);

            findConstrainedCorrespondences( F, keypoints1, keypoints2,
                                            descriptors1, descriptors2,
                                            pairwiseMatches, Feps, DescriptorRatio );

            //pairsFound += (int)pairwiseMatches.size();

            //Mat img1 = imread(format("%s/frame%04d.jpg", model.name.c_str(), (int)i), 1);
            //Mat img2 = imread(format("%s/frame%04d.jpg", model.name.c_str(), (int)j), 1);

            //double avg_err = 0;
            for( size_t k = 0; k < pairwiseMatches.size(); k++ )
            {
                int i1 = pairwiseMatches[k][0], i2 = pairwiseMatches[k][1];

                pts_k[0] = keypoints1[i1].pt;
                pts_k[1] = keypoints2[i2].pt;
                Rs_k[0] = Rs[i]; Rs_k[1] = Rs[j];
                ts_k[0] = ts[i]; ts_k[1] = ts[j];
                Point3f objpt = triangulatePoint(pts_k, Rs_k, ts_k, cameraMatrix);

                vector<Point3f> objpts;
                objpts.push_back(objpt);
                vector<Point2f> imgpts1, imgpts2;
                projectPoints(Mat(objpts), Rs_k[0], ts_k[0], cameraMatrix, Mat(), imgpts1);
                projectPoints(Mat(objpts), Rs_k[1], ts_k[1], cameraMatrix, Mat(), imgpts2);

                double e1 = norm(imgpts1[0] - keypoints1[i1].pt);
                double e2 = norm(imgpts2[0] - keypoints2[i2].pt);
                if( e1 + e2 > 5 )
                    continue;

                pairsFound++;

                //model.points.push_back(objpt);
                pairs[Pair2i(i1+dstart[i], i2+dstart[j])] = 1;
                pairs[Pair2i(i2+dstart[j], i1+dstart[i])] = 1;
                keypointsIdxMap[Pair2i((int)i,i1)] = 1;
                keypointsIdxMap[Pair2i((int)j,i2)] = 1;
                //CV_Assert(e1 < 5 && e2 < 5);
                //Scalar color(rand()%256,rand()%256, rand()%256);
                //circle(img1, keypoints1[i1].pt, 2, color, -1, CV_AA);
                //circle(img2, keypoints2[i2].pt, 2, color, -1, CV_AA);
            }
            //printf("avg err = %g\n", pairwiseMatches.size() ? avg_err/(2*pairwiseMatches.size()) : 0.);
            //imshow("img1", img1);
            //imshow("img2", img2);
            //waitKey();

            if( (i+1)*progressBarSize/nimagePairs > i*progressBarSize/nimagePairs )
            {
                putchar('.');
                fflush(stdout);
            }
        }

    printf("\nOk. Total pairs = %d\n", pairsFound );

    // 3. build the keypoint clusters
    vector<Pair2i> keypointsIdx;
    Set2i::iterator kpidx_it = keypointsIdxMap.begin(), kpidx_end = keypointsIdxMap.end();

    for( ; kpidx_it != kpidx_end; ++kpidx_it )
        keypointsIdx.push_back(kpidx_it->first);

    printf("\nClustering correspondences ");

    vector<int> labels;
    int nclasses = partition( keypointsIdx, labels, EqKeypoints(&dstart, &pairs) );

    printf("\nOk. Total classes (i.e. 3d points) = %d\n", nclasses );

    model.descriptors.create((int)keypointsIdx.size(), descriptorSize, CV_32F);
    model.didx.resize(nclasses);
    model.points.resize(nclasses);

    vector<vector<Pair2i> > clusters(nclasses);
    for( size_t i = 0; i < keypointsIdx.size(); i++ )
        clusters[labels[i]].push_back(keypointsIdx[i]);

    // 4. now compute 3D points corresponding to each cluster and fill in the model data
    printf("\nComputing 3D coordinates ");

    int globalDIdx = 0;
    for( int k = 0; k < nclasses; k++ )
    {
        int i, n = (int)clusters[k].size();
        pts_k.resize(n);
        Rs_k.resize(n);
        ts_k.resize(n);
        model.didx[k].resize(n);
        for( i = 0; i < n; i++ )
        {
            int imgidx = clusters[k][i].first, ptidx = clusters[k][i].second;
            Mat dstrow = model.descriptors.row(globalDIdx);
            alldescriptors.row(dstart[imgidx] + ptidx).copyTo(dstrow);

            model.didx[k][i] = globalDIdx++;
            pts_k[i] = allkeypoints[imgidx][ptidx].pt;
            Rs_k[i] = Rs[imgidx];
            ts_k[i] = ts[imgidx];
        }
        Point3f objpt = triangulatePoint(pts_k, Rs_k, ts_k, cameraMatrix);
        model.points[k] = objpt;

        if( (i+1)*progressBarSize/nclasses > i*progressBarSize/nclasses )
        {
            putchar('.');
            fflush(stdout);
        }
    }

    Mat img(768, 1024, CV_8UC3);
    vector<Point2f> imagePoints;
    namedWindow("Test", 1);

    // visualize the cloud
    for( size_t i = 0; i < nimages; i++ )
    {
        img = imread(format("%s/frame%04d.jpg", model.name.c_str(), (int)i), 1);
        projectPoints(Mat(model.points), Rs[i], ts[i], cameraMatrix, Mat(), imagePoints);

        for( int k = 0; k < (int)imagePoints.size(); k++ )
            circle(img, imagePoints[k], 2, Scalar(0,255,0), -1, CV_AA, 0);

        imshow("Test", img);
        int c = waitKey();
        if( c == 'q' || c == 'Q' )
            break;
    }
}
Example #7
0
int CHuman::HumanDetectRun(Mat &displayframe)
{
	//int time_use=0;
	//struct timeval start;
 //struct timeval end;

  //gettimeofday(&start,NULL);

	Mat tmpframe;
	//Mat blobdealFrame;
	vector< vector<Point> >  contours;
	Rect contoursRect;

	alarm =0;

	displayframe.copyTo(tmpframe);
	//displayframe.copyTo(blobdealFrame);

	vector<blobnode>().swap(humanlistpro);

	 m_zoomRows  =   tmpframe.rows  /m_rowsZoomRate;
	 m_zoomCols  =   tmpframe.cols   /m_colsZoomRate;

	 w_Rate = (float)tmpframe.cols / m_zoomCols;
	 h_Rate = (float)tmpframe.rows / m_zoomRows;


	Mat morph = Mat(tmpframe.rows ,tmpframe.cols,CV_8UC1);

	mog(tmpframe,foregrondframe,0.001);   // 0.001

	frameindex++;
	if(frameindex<250) return 2;
	if(frameindex >= 250) frameindex =250;
	foregrondframe.copyTo(mask);
	threshold(mask, mask, 200, 255, THRESH_BINARY);

	cv::erode(mask, mask, cv::Mat());

	cv::dilate(mask, mask, cv::Mat());

	algorithmMorphology_Operations(mask, mask);

	findContours(mask, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

	mask.release(); //nikola
	m_BlobRects.clear();
	for(int i=0;i<contours.size();i++)
	{
		contoursRect = boundingRect(contours[i]);
		if(fabs(contourArea(contours[i])) > 600.0)
		{
			//rectangle(displayframe, contoursRect,color_rect, 2, 8, 0);
			m_BlobRects.push_back(contoursRect);
		}
	}

	if((m_Flag & 0x02)  == 0x02){
		for(int i=0;i<DirectionLines.size();i++)
		{
			line(displayframe,DirectionLines[i].Start,DirectionLines[i].End,Scalar(255));
		}

	}


	if((m_Flag & 0x01)  == 1){

		for(int ii=0;ii<MonitorZoneRects.size();ii++)
		{
			rectangle(displayframe, MonitorZoneRects[ii], Scalar( 255, 0, 0 ), 2, 8, 0);//��
		}
	}

	human_detect(morph,displayframe);

	if((m_Flag & 0x01)  == 1){
		census(displayframe);// for human statistics
	}

	if((m_Flag & 0x02)  == 0x02){
		blobdeal(displayframe);
	}

        if(humanstatis.numAll<(humanstatis.doorin[0]+humanstatis.doorin[1]-humanstatis.doorout[0]-humanstatis.doorout[1]))
        		humanstatis.numAll = humanstatis.doorin[0]+humanstatis.doorin[1]-humanstatis.doorout[0]-humanstatis.doorout[1];

	 //dbgprint("door1:in=%d,out=%d  door2:in=%d,out=%d\n",humanstatis.doorin[0],humanstatis.doorout[0],humanstatis.doorin[1],humanstatis.doorout[1]);

	if(humanstatis.numAll >= MaxNum){
				//printf("humanstatis.numAll is %d\n",humanstatis.numAll);
				alarm =1;
	}

	char dstr[100];
	sprintf(dstr, "door1:in=%d,out=%d  door2:in=%d,out=%d",humanstatis.doorin[0],humanstatis.doorout[0],humanstatis.doorin[1],humanstatis.doorout[1]);
	putText(displayframe,dstr,cvPoint(200,25),CV_FONT_HERSHEY_COMPLEX, 0.5, cvScalar(0,0,255));


	//printf("humanstatis Num:%d, humanstatisIn:%d,humanstatisOut:%d\n",humanstatis.numAll,humanstatis.inAll,humanstatis.outAll);


	//char dstr[100];
  //sprintf(dstr,  "in=%d,out=%d",humanstatis.doorin,humanstatis.doorout);
  //putText(displayframe,dstr,cvPoint(25,25),CV_FONT_HERSHEY_COMPLEX, 1, cvScalar(0,0,255));
	//printf("doorin=%d,doorout=%d\n",humanstatis.doorin,humanstatis.doorout);

	tmpframe.release(); //nikola

	morph.release();
	vector<Point>().swap(object); //vector<Point>
	vector<blobnode>().swap(humanlist);

	//gettimeofday(&end,NULL);
	//time_use=(end.tv_sec-start.tv_sec)*1000+(end.tv_usec-start.tv_usec)/1000;//΢��
	//printf("time_use is %d\n",time_use);
	foregrondframe.release();

	return 0;
}
Example #8
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;
}
Example #9
0
void StereoVar::operator ()( const Mat& left, const Mat& right, Mat& disp )
{
    CV_Assert(left.size() == right.size() && left.type() == right.type());
    CvSize imgSize = left.size();
    int MaxD = MAX(labs(minDisp), labs(maxDisp));
    int SignD = 1; if (MIN(minDisp, maxDisp) < 0) SignD = -1;
    if (minDisp >= maxDisp) {MaxD = 256; SignD = 1;}

    Mat u;
    if ((flags & USE_INITIAL_DISPARITY) && (!disp.empty())) {
        CV_Assert(disp.size() == left.size() && disp.type() == CV_8UC1);
        disp.convertTo(u, CV_32FC1, static_cast<double>(SignD * MaxD) / 256);
    } else {
        u.create(imgSize, CV_32FC1);
        u.setTo(0);
    }

    // Preprocessing
    Mat leftgray, rightgray;
    if (left.type() != CV_8UC1) {
        cvtColor(left, leftgray, CV_BGR2GRAY);
        cvtColor(right, rightgray, CV_BGR2GRAY);
    } else {
        left.copyTo(leftgray);
        right.copyTo(rightgray);
    }
    if (flags & USE_EQUALIZE_HIST) {
        equalizeHist(leftgray, leftgray);
        equalizeHist(rightgray, rightgray);
    }
    if (poly_sigma > 0.0001) {
        GaussianBlur(leftgray, leftgray, cvSize(poly_n, poly_n), poly_sigma);
        GaussianBlur(rightgray, rightgray, cvSize(poly_n, poly_n), poly_sigma);
    }

    if (flags & USE_AUTO_PARAMS) {
        penalization = PENALIZATION_TICHONOV;
        autoParams();
    }

    Mat I1, I2;
    leftgray.convertTo(I1, CV_32FC1);
    rightgray.convertTo(I2, CV_32FC1);
    leftgray.release();
    rightgray.release();

    Mat I2x = diffX(I2);

    FMG(I1, I2, I2x, u, levels - 1);

    I1.release();
    I2.release();
    I2x.release();


    disp.create( left.size(), CV_8UC1 );
    u = abs(u);
    u.convertTo(disp, disp.type(), 256 / MaxD, 0);

    u.release();
}
Example #10
0
vector<detection> HogDetector::detect( ClassifierParams& params )
{
	Mat frame = params.frame;
	vector<Rect> rectDetections;
	vector<detection> detectionsToReturn;

	int detectionId = 0;
	Mat grayClone;
	cv::cvtColor(frame, grayClone, CV_BGR2GRAY);
	gpuMat.upload(grayClone);
	hogGpuDetector.detectMultiScale(gpuMat,rectDetections);
	gpuMat.release();
	grayClone.release();

	/*auto cl = frame.clone();
	for_each(begin(rectDetections), end(rectDetections), [&](Rect b)
	{
		Rect rect = b;
		Scalar color = Scalar( 0,255,0 );
		rectangle( cl, rect.tl(), rect.br(), color);
	});
	PngSaver::save("falseDetections", cl);
	PngSaver::incrementCount();
	cl.release();*/

	int size = rectDetections.size();
	int* status = new int[size]; 	
	memset(status, KEEP, size*sizeof(int));

	for(int i = 0; i < size - 1; i++)
	{		
		for(int j = i+1; j < size; j++)
		{
			if(status[i] == KEEP && status[j] == KEEP)
			{
				int min;
				if(rectDetections[i].area() < rectDetections[j].area())
					min = i;
				else
					min = j;

				Rect intersection = rectDetections[i] & rectDetections[j];
				if(intersection.area() > 0.4*rectDetections[min].area())
				{
					status[min] = DISCARD;
				}
			}			
		}
	}

	for(int i = 0; i < size; i++)	
		if(status[i] == KEEP)
		{
			detection det = {detectionId++, rectDetections[i]}; 
			detectionsToReturn.push_back(det);
		}
	
	delete[] status;	
	
	return detectionsToReturn;
}
Example #11
0
void StereoVar::VariationalSolver(Mat &I1, Mat &I2, Mat &I2x, Mat &u, int level)
{
    register int n, x, y;
    float gl = 1, gr = 1, gu = 1, gd = 1, gc = 4;
    Mat g_c, g_p;
    Mat U;
    u.copyTo(U);

    int     N = nIt;
    float   l = lambda;
    float   Fi = fi;


    if (flags & USE_SMART_ID) {
        double scale = pow(pyrScale, (double) level) * (1 + pyrScale);
        N = (int) (N / scale);
    }

    double scale = pow(pyrScale, (double) level);
    Fi /= (float) scale;
    l *= (float) scale;

    int width   = u.cols - 1;
    int height  = u.rows - 1;
    for (n = 0; n < N; n++) {
        if (penalization != PENALIZATION_TICHONOV) {
            Mat gradient = getGradient(U);
            switch (penalization) {
                case PENALIZATION_CHARBONNIER:  g_c = getG_c(gradient, l); break;
                case PENALIZATION_PERONA_MALIK: g_p = getG_p(gradient, l); break;
            }
            gradient.release();
        }
        for (y = 1 ; y < height; y++) {
            float *pU   = U.ptr<float>(y);
            float *pUu  = U.ptr<float>(y + 1);
            float *pUd  = U.ptr<float>(y - 1);
            float *pu   = u.ptr<float>(y);
            float *pI1  = I1.ptr<float>(y);
            float *pI2  = I2.ptr<float>(y);
            float *pI2x = I2x.ptr<float>(y);
            float *pG_c = NULL, *pG_cu = NULL, *pG_cd = NULL;
            float *pG_p = NULL, *pG_pu = NULL, *pG_pd = NULL;
            switch (penalization) {
                case PENALIZATION_CHARBONNIER:
                    pG_c    = g_c.ptr<float>(y);
                    pG_cu   = g_c.ptr<float>(y + 1);
                    pG_cd   = g_c.ptr<float>(y - 1);
                    break;
                case PENALIZATION_PERONA_MALIK:
                    pG_p    = g_p.ptr<float>(y);
                    pG_pu   = g_p.ptr<float>(y + 1);
                    pG_pd   = g_p.ptr<float>(y - 1);
                    break;
            }
            for (x = 1; x < width; x++) {
                switch (penalization) {
                    case PENALIZATION_CHARBONNIER:
                        gc = pG_c[x];
                        gl = gc + pG_c[x - 1];
                        gr = gc + pG_c[x + 1];
                        gu = gc + pG_cu[x];
                        gd = gc + pG_cd[x];
                        gc = gl + gr + gu + gd;
                        break;
                    case PENALIZATION_PERONA_MALIK:
                        gc = pG_p[x];
                        gl = gc + pG_p[x - 1];
                        gr = gc + pG_p[x + 1];
                        gu = gc + pG_pu[x];
                        gd = gc + pG_pd[x];
                        gc = gl + gr + gu + gd;
                        break;
                }

                float _fi = Fi;
                if (maxDisp > minDisp) {
                    if (pU[x] > maxDisp * scale) {_fi *= 1000; pU[x] = static_cast<float>(maxDisp * scale);}
                    if (pU[x] < minDisp * scale) {_fi *= 1000; pU[x] = static_cast<float>(minDisp * scale);}
                }

                int A = static_cast<int>(pU[x]);
                int neg = 0; if (pU[x] <= 0) neg = -1;

                if (x + A > width)
                    pu[x] = pU[width - A];
                else if (x + A + neg < 0)
                    pu[x] = pU[- A + 2];
                else {
                    pu[x] = A + (pI2x[x + A + neg] * (pI1[x] - pI2[x + A])
                              + _fi * (gr * pU[x + 1] + gl * pU[x - 1] + gu * pUu[x] + gd * pUd[x] - gc * A))
                              / (pI2x[x + A + neg] * pI2x[x + A + neg] + gc * _fi) ;
                }
            }// x
            pu[0] = pu[1];
            pu[width] = pu[width - 1];
        }// y
        for (x = 0; x <= width; x++) {
            u.at<float>(0, x) = u.at<float>(1, x);
            u.at<float>(height, x) = u.at<float>(height - 1, x);
        }
        u.copyTo(U);
        if (!g_c.empty()) g_c.release();
        if (!g_p.empty()) g_p.release();
    }//n
}
int main(int argc, char** argv) {
	/* the input and output dir */
	string input_dir = "/home/user/ccv/data/seq04-img-left";

	/* initialize the ccv states */
	ccv_enable_default_cache();
	ccv_dpm_mixture_model_t* model = ccv_dpm_read_mixture_model(argv[1]);

	/* set the pedestrian detection parameters */
	ccv_dpm_param_t myparameters;
	myparameters.threshold = 0.4;
	myparameters.interval = 8;
	myparameters.min_neighbors = 1;
	myparameters.flags = 0;

	/* debug */
	string source = "/home/user/ccv/demo1.avi";
	VideoCapture inputVideo(source);              // Open input
	if (!inputVideo.isOpened()) {
		cout  << "Could not open the input video: " << source << endl;
		return -1;
	}
	int ex = static_cast<int>(inputVideo.get(CV_CAP_PROP_FOURCC));     // Get Codec Type- Int form
	cout<<"The coding is "<<ex<<endl;
	cout<<"The fps is "<<inputVideo.get(CV_CAP_PROP_FPS)<<endl;

	/* initialize the video writer */
	Mat getSize = imread(input_dir + "/image_00000100_0.png");
	Size videoSize = getSize.size();
	getSize.release();
	VideoWriter outputVideo;
	outputVideo.open("/home/user/ccv/data/output/no_red_eth1_reg_0.2.avi", ex, fps, videoSize, true);
	if (!outputVideo.isOpened()) {
		cout<<"Could not open the output video"<<endl;
		return false;
	}


	/* process one by one */
	for (int iImage = imageStart; iImage <= imageEnd; iImage++) {

		/* read the image, ccv_image for detection, and opencv Mat for recording */
		string imageTail;
		if (iImage < 10) imageTail = "0000000" + patch::to_string(iImage);
		else if (iImage < 100) imageTail = "000000" + patch::to_string(iImage);
		else imageTail = "00000" + patch::to_string(iImage);
		string image_name = input_dir + "/image_" + imageTail + "_0.png";

		ccv_dense_matrix_t* image = 0;
		ccv_read(image_name.c_str(), &image, CCV_IO_ANY_FILE);
		Mat plot_result = imread(image_name);
		if (image == 0) cerr<<"The reading of dataset failed!"<<endl;
		cout<<"Image succussfully read"<<endl;

		/* processing the image one by one */
		unsigned int elapsed_time = get_current_time();
		ccv_array_t* seq = ccv_dpm_detect_objects(image, &model, 1, myparameters);
		elapsed_time = get_current_time() - elapsed_time;
		cout<<"Using "<<elapsed_time<<"ms on detecting the "<<iImage<<"th image"<<endl;

		if (seq != NULL) { 
			/* the detection has something to say */
			for (int i = 0; i < seq->rnum; i++) {
				ccv_root_comp_t* comp = (ccv_root_comp_t*)ccv_array_get(seq, i); /* get the ith number */
				/* a simple regression trick */
				float predHeight = ((float)videoSize.height / 2 - comp->rect.y) * 2;
				float diff = predHeight - comp->rect.height;
				if (comp->rect.height > videoSize.height / 2.2 && comp->rect.y + comp->rect.height < videoSize.height / 2 + 50) {
					rectangle(plot_result, 
							cv::Point(int(comp->rect.x), int(comp->rect.y)),
							cv::Point(int(comp->rect.x + comp->rect.width), int(comp->rect.y + comp->rect.height)),
							cvScalar(0, 0, 255), 2, 8, 0);
				} else{
					rectangle(plot_result, 
							cv::Point(int(comp->rect.x), int(comp->rect.y)),
							cv::Point(int(comp->rect.x + comp->rect.width), int(comp->rect.y + comp->rect.height)),
							cvScalar(0, 255, 0), 2, 8, 0);
				}
			}
			ccv_array_free(seq); /* release the sequence */
		}
		outputVideo << plot_result;

		/* free the images */
		ccv_matrix_free(image);
		plot_result.release();
	}


	outputVideo.release();
	ccv_drain_cache();
	ccv_dpm_mixture_model_free(model);
	return 0;
}
Example #13
0
/** @function thresh_callback */
void regionSelect()
{


      Mat canny_output;
      vector<vector<Point> > contours;
      vector<Vec4i> hierarchy;
      bool flag=true;


      Canny( src_gray, canny_output, thresh, thresh*2, 3 );

      findContours( canny_output, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_TC89_L1, Point(0, 0) );
    //    imshow("tests",canny_output);

      OCV::remove_contours(contours,1500,12000);
        if(contours.size()==0)
            return;
      RotatedRect rect;

      Mat M, rotated, roi,correctedImage;
      Mat mask=Mat::zeros( canny_output.size(), CV_8UC1);
      vector<Point> approx;
      vector<RotatedRect> selectedPattern;
      vector<Point2f> scale;
      for( auto i = 0; i<contours.size(); i++ )
      {
    //      cout/*<<rect_size.height<<" - "<<rect_size.width<<" - "*/<<i;
          rect = minAreaRect( Mat(contours[i]));
          Point2f vertices[4];
          rect.points(vertices);
          float angle = rect.angle;
          Size rect_size = rect.size;

          if (rect.angle < -45.) {
              angle += 90.0;
              swap(rect_size.width, rect_size.height);
          }

          M = getRotationMatrix2D(rect.center, angle, 1.0);

          warpAffine(canny_output, canny_output, M, src.size(), INTER_CUBIC);
          warpAffine(src, rotated, M, src.size(), INTER_CUBIC);

//          imshow("Srcs",src);
          auto rec=rect.center;
          int heightRect=rect_size.height;
          int widthRect=rect_size.width;
          Rect rc((int)rec.x-widthRect/2,(int)rec.y-heightRect/2,widthRect,heightRect);

          cout<<"Contour : "<<rect.center<<" Area : "<<contourArea(contours[i])<<" Corners : "<<rect.size<<endl;
          getRectSubPix(rotated, rect_size, rect.center, roi);
          imshow("sss",roi);
//          waitKey();
          if(containsCheck(rc,10,270)){
    ////          cout<<"hit"<<endl;
    //          Point points[4];
    //          get_rect_points(rc,points);
    //          line(rotated, {points[0].x, points[0].y}, {points[1].x, points[1].y}, Scalar(0,0,255), 2);
    //          line(rotated, {points[1].x, points[1].y}, {points[2].x, points[2].y}, Scalar(0,0,255), 2);
    //          line(rotated, {points[2].x, points[2].y}, {points[3].x, points[3].y}, Scalar(0,0,255), 2);
    //          line(rotated, {points[3].x, points[3].y}, {points[0].x, points[0].y}, Scalar(0,0,255), 2);
              for (int i = 0; i < 4; i++)
                  line(rotated, vertices[i], vertices[(i+1)%4], Scalar(0,255,0));
//              imshow( "Source", rotated );
//              waitKey();
              if(flag){
                  flag=false;
                  correctedImage=rotated.clone();
              }
              scale.push_back(Point2f(widthRect/27.0,heightRect/10.0));
              selectedPattern.push_back(rect);
//              cout<<" x - "<<rc.x<<" y- "<<rc.y<<endl;
          }

    }
      if(selectedPattern.size()<2)
          return;
{
      Point2f rctP[2][4];
      int i = 0 ;
      for (RotatedRect rct:selectedPattern) {
        if(i<2){
            vector<Point2f> pointVector;
            sort_rect_corners(rct,rctP[i]);
            rct.points(rctP[i]);
            for(auto rectPoint:rctP[i])
                pointVector.push_back(rectPoint);

            int j=0;
            std::ostringstream name;
            for(Point2f point:pointVector){
                name<<++j;
                if(rct.angle<-45.)
                    name<<"-";
//                putText( src,name.str(), Point(int(point.x+5.0),int(point.y)),  FONT_HERSHEY_SIMPLEX,1,Scalar(0,255,0),2);
//                circle(src,Point(int(point.x),int(point.y)),3,Scalar(255,0,0),2);
                name.str("");
            }
            cout<<" Pattern Number : "<<i<<" Center : "<<rct.center<<endl;

        }
        i++;

      }
      line(src,rctP[0][0],rctP[1][1],Scalar(255,255,0));
      line(src,rctP[0][0],rctP[0][3],Scalar(255,255,0));
      line(src,rctP[1][2],rctP[1][1],Scalar(255,255,0));
      line(src,rctP[1][2],rctP[0][3],Scalar(255,255,0));
      vector<Point2f> oPoints
      {
          rctP[0][0],rctP[0][3],rctP[1][2],rctP[1][1]
      };
      Mat target(108,96,src.type());
      std::vector<cv::Point2f> target_points
      {
       {0, 0}, {target.cols - 1, 0},
       {target.cols - 1, target.rows - 1},
       {0, target.rows - 1}
      };
      Mat const trans_mat = cv::getPerspectiveTransform(oPoints, target_points);
      warpPerspective(src, target, trans_mat, target.size());

//      imwrite("region.png",src);
      imshow("Region",src);
      waitKey();
}
      contours.clear();
      src_out.release();
}
Example #14
0
int main(int argc, char* argv[])
{
	// Loading Configuration file
	opencctv::util::Config* pConfig;
	try {
		pConfig = opencctv::util::Config::getInstance();
	} catch (opencctv::Exception &e) {
		std::string sErrMsg = "Failed to load Configuration file. ";
		sErrMsg.append(e.what());
		opencctv::util::log::Loggers::getDefaultLogger()->error(sErrMsg);
		return -1;
	}
	opencctv::util::log::Loggers::getDefaultLogger()->info(
			"Loading Configuration file done.");

	// Creating MQ
	zmq::socket_t* pSocket = NULL;
	try {
		pSocket = mq::MqUtil::createNewMq(pConfig->get(util::PROPERTY_MQ_PORT), ZMQ_PULL);
	} catch (std::runtime_error &e) {
		std::string sErrMsg =
				"Failed to create MQ. ";
		sErrMsg.append(e.what());
		util::log::Loggers::getDefaultLogger()->error(sErrMsg);
		return -1;
	}
	opencctv::util::log::Loggers::getDefaultLogger()->info(
				"MQ created.");

	while(pSocket)
	{
		std::string sRequest;
		try {
			mq::MqUtil::readFromSocket(pSocket, sRequest);
		} catch (std::runtime_error &e) {
			std::string sErrMsg =
					"Failed to read request from the MQ. ";
			sErrMsg.append(e.what());
			util::log::Loggers::getDefaultLogger()->error(sErrMsg);
		}

		unsigned int iId = 0;
		string sTimestamp, sCameraId, sDstDirPath;
		try
		{
			parseMessage(sRequest, iId, sCameraId, sDstDirPath);
		}
		catch(std::exception &e)
		{
			std::string sErrMsg = "Failed to parse request message. ";
			sErrMsg.append(e.what());
			util::log::Loggers::getDefaultLogger()->error(sErrMsg);
		}

		if(iId > 0 && !sCameraId.empty() && !sDstDirPath.empty())
		{
			// Get Result from Results DB
			opencctv::db::AnalyticResultGateway gateway;
			AnalyticResult result;
			unsigned int iAnalyticInstanceId;
			gateway.getResult(iId, result, iAnalyticInstanceId);
			sTimestamp = result.getTimestamp();

			time_t time = toTime(sTimestamp);
			std::vector<Rect> vBoundingBoxes;
			try
			{
				getRects(result.getCustomText(), vBoundingBoxes);
			}
			catch(std::exception &e)
			{
				std::string sErrMsg = "Failed to parse XML result text. ";
				sErrMsg.append(e.what());
				util::log::Loggers::getDefaultLogger()->error(sErrMsg);
			}

			stringstream ssRtspUri;
			ssRtspUri << "rtsp://"
					<< pConfig->get(util::PROPERTY_SERVER_USERNAME) << ":";
			ssRtspUri << pConfig->get(util::PROPERTY_SERVER_PASSWORD) << "@";
			ssRtspUri << pConfig->get(util::PROPERTY_SERVER_IP)
					<< ":554/ISAPI/Streaming/tracks/";
			ssRtspUri << sCameraId;

			int iTimeDiff = lexical_cast<int>(pConfig->get(util::PROPERTY_TIME_DIFF));
			int iSkips = lexical_cast<int>(pConfig->get(util::PROPERTY_NUMB_SKIPS));
			++iSkips;

			ssRtspUri << "?starttime=" << toTimestamp((time + iTimeDiff));

			VideoCapture cap(ssRtspUri.str());
			if(cap.isOpened())
			{
				Mat matImage;
				for (int i = 0; i < iSkips; ++i) {
					cap >> matImage;
				}
				if (!matImage.empty()) {
					for (size_t i = 0; i < vBoundingBoxes.size(); ++i) {
						Mat mati = matImage(vBoundingBoxes[i]);
						stringstream ss;
						ss << sDstDirPath << sTimestamp << "_" << i
								<< ".jpg";
						cv::imwrite(ss.str(), mati);
						std::string sMsg = "Image: ";
						sMsg.append(ss.str());
						sMsg.append(" created.");
						util::log::Loggers::getDefaultLogger()->debug(sMsg);
					}
				}
				matImage.release();
			}
			else
			{
dynamicArray2D<p_offset> GeneralizedPatchMatch::matchImage (const cv::Mat &img_src, const cv::Mat &img_dst) {
    
    Mat src = img_src.clone(), dst = img_dst.clone();
    
    if ( src.type() != CV_64FC3 ) {
        src.convertTo(src, CV_64FC3);
        src /= 255.0;
    }
    
    if ( dst.type() != CV_64FC3 ) {
        dst.convertTo(dst, CV_64FC3);
        dst /= 255.0;
    }
    
    
    dynamicArray2D<p_offset> NearNei = dynamicArray2D<p_offset>(dst.rows, dst.cols, NEAREST_K);
    
    Mat temp = src.clone();
    
    // random initialization of Nearest Neighbor Field
    for (int i = 0; i < NearNei.rows; i ++) {
        for (int j = 0; j < NearNei.cols; j ++) {
            
            
            for (int k = 0; k < NearNei.at(i, j).size(); k ++) {
                
                Point temp_pt = Point(genRandom.uniform(0, src.cols - 1) - j, genRandom.uniform(0, src.rows - 1) - i);
                double temp_er = patchTool.SSD(src, temp_pt + Point(j, i), dst, Point(j, i));
                
                NearNei.at(i, j).push(temp_pt, temp_er);
                
            }

        }
    }
    

    
    
    r_time time;
    time.start();
    
        NearNei = match(src, dst, NearNei);
    
    time.end();
    time.printTime("sequential");
    
//    for (int k = 0; k < NEAREST_K; k ++) {
//
//        reconstDisplay(src, NearNei, k, 1);
//        reconstErrorDisplay(src, dst, NearNei, k, 1);
//        distanceDisplay(NearNei, k, 1);
//        showMat(src, "s", 0);
//        
//    }
    
    RepetendDisplay(src, NearNei);


    
    
    //    time.start();
    //
    //        dynamicArray2D<Point> mulNearNei = multilevelMatch(src, dst);
    //
    //    time.end();
    //    time.printTime("multiscale");
    //
    //    cout<<"TEST = "<<convergenceTest(NearNei, mulNearNei);
    
    src.release();
    dst.release();

    
    return NearNei;
}
Example #16
0
double cv::findTransformECC(InputArray templateImage,
                            InputArray inputImage,
                            InputOutputArray warpMatrix,
                            int motionType,
                            TermCriteria criteria)
{


    Mat src = templateImage.getMat();//template iamge
    Mat dst = inputImage.getMat(); //input image (to be warped)
    Mat map = warpMatrix.getMat(); //warp (transformation)

    CV_Assert(!src.empty());
    CV_Assert(!dst.empty());


    if( ! (src.type()==dst.type()))
        CV_Error( CV_StsUnmatchedFormats, "Both input images must have the same data type" );

    //accept only 1-channel images
    if( src.type() != CV_8UC1 && src.type()!= CV_32FC1)
        CV_Error( CV_StsUnsupportedFormat, "Images must have 8uC1 or 32fC1 type");

    if( map.type() != CV_32FC1)
        CV_Error( CV_StsUnsupportedFormat, "warpMatrix must be single-channel floating-point matrix");

    CV_Assert (map.cols == 3);
    CV_Assert (map.rows == 2 || map.rows ==3);

    CV_Assert (motionType == MOTION_AFFINE || motionType == MOTION_HOMOGRAPHY ||
        motionType == MOTION_EUCLIDEAN || motionType == MOTION_TRANSLATION);

    if (motionType == MOTION_HOMOGRAPHY){
        CV_Assert (map.rows ==3);
    }

    CV_Assert (criteria.type & TermCriteria::COUNT || criteria.type & TermCriteria::EPS);
    const int    numberOfIterations = (criteria.type & TermCriteria::COUNT) ? criteria.maxCount : 200;
    const double termination_eps    = (criteria.type & TermCriteria::EPS)   ? criteria.epsilon  :  -1;

    int paramTemp = 6;//default: affine
    switch (motionType){
      case MOTION_TRANSLATION:
          paramTemp = 2;
          break;
      case MOTION_EUCLIDEAN:
          paramTemp = 3;
          break;
      case MOTION_HOMOGRAPHY:
          paramTemp = 8;
          break;
    }


    const int numberOfParameters = paramTemp;

    const int ws = src.cols;
    const int hs = src.rows;
    const int wd = dst.cols;
    const int hd = dst.rows;

    Mat Xcoord = Mat(1, ws, CV_32F);
    Mat Ycoord = Mat(hs, 1, CV_32F);
    Mat Xgrid = Mat(hs, ws, CV_32F);
    Mat Ygrid = Mat(hs, ws, CV_32F);

    float* XcoPtr = Xcoord.ptr<float>(0);
    float* YcoPtr = Ycoord.ptr<float>(0);
    int j;
    for (j=0; j<ws; j++)
        XcoPtr[j] = (float) j;
    for (j=0; j<hs; j++)
        YcoPtr[j] = (float) j;

    repeat(Xcoord, hs, 1, Xgrid);
    repeat(Ycoord, 1, ws, Ygrid);

    Xcoord.release();
    Ycoord.release();

    Mat templateZM    = Mat(hs, ws, CV_32F);// to store the (smoothed)zero-mean version of template
    Mat templateFloat = Mat(hs, ws, CV_32F);// to store the (smoothed) template
    Mat imageFloat    = Mat(hd, wd, CV_32F);// to store the (smoothed) input image
    Mat imageWarped   = Mat(hs, ws, CV_32F);// to store the warped zero-mean input image
    Mat allOnes		= Mat::ones(hd, wd, CV_8U); //to use it for mask warping
    Mat imageMask		= Mat(hs, ws, CV_8U); //to store the final mask

    //gaussian filtering is optional
    src.convertTo(templateFloat, templateFloat.type());
    GaussianBlur(templateFloat, templateFloat, Size(5, 5), 0, 0);//is in-place filtering slower?

    dst.convertTo(imageFloat, imageFloat.type());
    GaussianBlur(imageFloat, imageFloat, Size(5, 5), 0, 0);

    // needed matrices for gradients and warped gradients
    Mat gradientX = Mat::zeros(hd, wd, CV_32FC1);
    Mat gradientY = Mat::zeros(hd, wd, CV_32FC1);
    Mat gradientXWarped = Mat(hs, ws, CV_32FC1);
    Mat gradientYWarped = Mat(hs, ws, CV_32FC1);


    // calculate first order image derivatives
    Matx13f dx(-0.5f, 0.0f, 0.5f);

    filter2D(imageFloat, gradientX, -1, dx);
    filter2D(imageFloat, gradientY, -1, dx.t());


    // matrices needed for solving linear equation system for maximizing ECC
    Mat jacobian                = Mat(hs, ws*numberOfParameters, CV_32F);
    Mat hessian                 = Mat(numberOfParameters, numberOfParameters, CV_32F);
    Mat hessianInv              = Mat(numberOfParameters, numberOfParameters, CV_32F);
    Mat imageProjection         = Mat(numberOfParameters, 1, CV_32F);
    Mat templateProjection      = Mat(numberOfParameters, 1, CV_32F);
    Mat imageProjectionHessian  = Mat(numberOfParameters, 1, CV_32F);
    Mat errorProjection         = Mat(numberOfParameters, 1, CV_32F);

    Mat deltaP = Mat(numberOfParameters, 1, CV_32F);//transformation parameter correction
    Mat error = Mat(hs, ws, CV_32F);//error as 2D matrix

    const int imageFlags = CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS+CV_WARP_INVERSE_MAP;
    const int maskFlags  = CV_INTER_NN+CV_WARP_FILL_OUTLIERS+CV_WARP_INVERSE_MAP;


    // iteratively update map_matrix
    double rho      = -1;
    double last_rho = - termination_eps;
    for (int i = 1; (i <= numberOfIterations) && (fabs(rho-last_rho)>= termination_eps); i++)
    {

        // warp-back portion of the inputImage and gradients to the coordinate space of the templateImage
        if (motionType != MOTION_HOMOGRAPHY)
        {
            warpAffine(imageFloat, imageWarped,     map, imageWarped.size(),     imageFlags);
            warpAffine(gradientX,  gradientXWarped, map, gradientXWarped.size(), imageFlags);
            warpAffine(gradientY,  gradientYWarped, map, gradientYWarped.size(), imageFlags);
            warpAffine(allOnes,    imageMask,       map, imageMask.size(),       maskFlags);
        }
        else
        {
            warpPerspective(imageFloat, imageWarped,     map, imageWarped.size(),     imageFlags);
            warpPerspective(gradientX,  gradientXWarped, map, gradientXWarped.size(), imageFlags);
            warpPerspective(gradientY,  gradientYWarped, map, gradientYWarped.size(), imageFlags);
            warpPerspective(allOnes,    imageMask,       map, imageMask.size(),       maskFlags);
        }


        Scalar imgMean, imgStd, tmpMean, tmpStd;
        meanStdDev(imageWarped,   imgMean, imgStd, imageMask);
        meanStdDev(templateFloat, tmpMean, tmpStd, imageMask);

        subtract(imageWarped,   imgMean, imageWarped, imageMask);//zero-mean input
        subtract(templateFloat, tmpMean, templateZM,  imageMask);//zero-mean template

        const double tmpNorm = std::sqrt(countNonZero(imageMask)*(tmpStd.val[0])*(tmpStd.val[0]));
        const double imgNorm = std::sqrt(countNonZero(imageMask)*(imgStd.val[0])*(imgStd.val[0]));

        // calculate jacobian of image wrt parameters
        switch (motionType){
            case MOTION_AFFINE:
                image_jacobian_affine_ECC(gradientXWarped, gradientYWarped, Xgrid, Ygrid, jacobian);
                break;
            case MOTION_HOMOGRAPHY:
                image_jacobian_homo_ECC(gradientXWarped, gradientYWarped, Xgrid, Ygrid, map, jacobian);
                break;
            case MOTION_TRANSLATION:
                image_jacobian_translation_ECC(gradientXWarped, gradientYWarped, jacobian);
                break;
            case MOTION_EUCLIDEAN:
                image_jacobian_euclidean_ECC(gradientXWarped, gradientYWarped, Xgrid, Ygrid, map, jacobian);
                break;
        }

        // calculate Hessian and its inverse
        project_onto_jacobian_ECC(jacobian, jacobian, hessian);

        hessianInv = hessian.inv();

        const double correlation = templateZM.dot(imageWarped);

        // calculate enhanced correlation coefficiont (ECC)->rho
        last_rho = rho;
        rho = correlation/(imgNorm*tmpNorm);

        // project images into jacobian
        project_onto_jacobian_ECC( jacobian, imageWarped, imageProjection);
        project_onto_jacobian_ECC(jacobian, templateZM, templateProjection);


        // calculate the parameter lambda to account for illumination variation
        imageProjectionHessian = hessianInv*imageProjection;
        const double lambda_n = (imgNorm*imgNorm) - imageProjection.dot(imageProjectionHessian);
        const double lambda_d = correlation - templateProjection.dot(imageProjectionHessian);
        if (lambda_d <= 0.0)
        {
            rho = -1;
            CV_Error(CV_StsNoConv, "The algorithm stopped before its convergence. The correlation is going to be minimized. Images may be uncorrelated or non-overlapped");

        }
        const double lambda = (lambda_n/lambda_d);

        // estimate the update step delta_p
        error = lambda*templateZM - imageWarped;
        project_onto_jacobian_ECC(jacobian, error, errorProjection);
        deltaP = hessianInv * errorProjection;

        // update warping matrix
        update_warping_matrix_ECC( map, deltaP, motionType);


    }

    // return final correlation coefficient
    return rho;
}
Example #17
0
int main()
{
	cout<< "Begin My Stitcher" << endl;

	vector<string> img_names;
	if(!ReadImg_names(img_names)){return -1;}

	// Check if have enough images
	int num_images = static_cast<int>(img_names.size());
	if (num_images < 2)
    {
        cout<< "Need more images" << endl;
        return -1;
    }

	int64 t = getTickCount();

	// find features
	cout<< "START_ Find features " << endl;

	Ptr<FeaturesFinder> finder;
#if defined(HAVE_OPENCV_NONFREE) && defined(HAVE_OPENCV_GPU)
        if (try_gpu && gpu::getCudaEnabledDeviceCount() > 0)
            finder = new SurfFeaturesFinderGpu();
        else
#endif
            finder = new SurfFeaturesFinder();

	// Load images
	// step 1 : find features
	Mat img;
	vector<ImageFeatures> features(num_images);
    vector<Mat> images(num_images);
    vector<Size> img_sizes(num_images);

	for(int i = 0; i < num_images; i++)
	{
		img = imread(img_names[i]);
		images[i] = img.clone();
		img_sizes[i] = img.size();

		if(img.empty())
		{
			cout<< "Connot open image " << img_names[i] << endl;
			return -1;
		}

		(*finder)(img, features[i]);
		features[i].img_idx = i;
		cout<< "Features in image # " << i << " : " << features[i].keypoints.size() << endl; 
	}

	finder->collectGarbage();
	img.release();

	cout<< "END_ Find all features, time:" << (getTickCount()-t)/getTickFrequency() << "sec" << endl;

	// step 2 : match features
	cout<< "START_ Partwise match" << endl;

	vector<MatchesInfo> pairwise_matches;
	BestOf2NearestMatcher matcher(try_gpu, match_conf);
	matcher(features, pairwise_matches);
	matcher.collectGarbage();

	 ofstream f("graph_match.txt");
	  f << matchesGraphAsString(img_names, pairwise_matches, conf_thresh);


	cout<< "END_ Pairwise matching, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec" << endl;

	 

	// step 3 : Homography
	cout<< "START_ Homography" << endl;

	vector<Mat> HA;


	// step 4 : wrap
	/*Ptr<WarperCreator> warper_creator;
	warper_creator = new cv::FisheyeWarper();*/





	return 0;
}
Example #18
0
//this is a sample for foreground detection functions
int main(int argc, const char** argv)
{


	help();

	CommandLineParser parser(argc, argv, keys);
	bool useCamera = parser.has("camera");
	string file = parser.get<string>("file_name");



	/*****************************************************************************/
	//Blob detector initialization
	SimpleBlobDetector::Params pDefaultBLOB;
	// This is default parameters for SimpleBlobDetector
	pDefaultBLOB.thresholdStep = 10;
	pDefaultBLOB.minThreshold = 10;
	pDefaultBLOB.maxThreshold = 220;
	pDefaultBLOB.minRepeatability = 2;
	pDefaultBLOB.minDistBetweenBlobs = 10;
	pDefaultBLOB.filterByColor = false;
	pDefaultBLOB.blobColor = 0;
	pDefaultBLOB.filterByArea = true;  // Region variation filtering by area
	pDefaultBLOB.minArea = 10;
	pDefaultBLOB.maxArea = 15500;
	pDefaultBLOB.filterByCircularity = false;
	pDefaultBLOB.minCircularity = 0.9f;
	pDefaultBLOB.maxCircularity = (float)1e37;
	pDefaultBLOB.filterByInertia = false;
	pDefaultBLOB.minInertiaRatio = 0.01f;
	pDefaultBLOB.maxInertiaRatio = (float)1e37;
	pDefaultBLOB.filterByConvexity = false;
	pDefaultBLOB.minConvexity = 0.95f;
	pDefaultBLOB.maxConvexity = (float)1e37;
	// Descriptor array for BLOB
	vector<String> typeDesc;
	// Param array for BLOB
	vector<SimpleBlobDetector::Params> pBLOB;
	vector<SimpleBlobDetector::Params>::iterator itBLOB;
	// Color palette
	vector< Vec3b >  palette;
	for (int i = 0; i < 65536; i++)
	{
		palette.push_back(Vec3b((uchar)rand(), (uchar)rand(), (uchar)rand()));
	}

	// This descriptor are going to be detect and compute BLOBS with 6 differents params
	// Param for first BLOB detector we want all
	typeDesc.push_back("BLOB");    // see http://docs.opencv.org/trunk/d0/d7a/classcv_1_1SimpleBlobDetector.html
	pBLOB.push_back(pDefaultBLOB);
	
	
	itBLOB = pBLOB.begin();
	vector<double> desMethCmp;
	Ptr<Feature2D> b;
	String label;

	// Descriptor loop
	vector<String>::iterator itDesc;
	for (itDesc = typeDesc.begin(); itDesc != typeDesc.end(); itDesc++)
	{
		vector<KeyPoint> keyImg1;
		if (*itDesc == "BLOB")
		{
			b = SimpleBlobDetector::create(*itBLOB);
			label = Legende(*itBLOB);
			itBLOB++;
		}
	}
	vector<KeyPoint>  keyImg;
	vector<Rect>  zone;
	vector<vector <Point> >  region;
	Ptr<SimpleBlobDetector> sbd = b.dynamicCast<SimpleBlobDetector>();

	//Blob initialization end
	/*****************************************************************************/

	VideoCapture cap;
	bool update_bg_model = false;

	if (useCamera)
		cap.open(0);
	else
		cap.open(file.c_str());



	if (!cap.isOpened())
	{
		printf("can not open camera or video file\n");
		return -1;
	}

	namedWindow("image", WINDOW_NORMAL);
	namedWindow("foreground image", WINDOW_NORMAL);

	// CUDA background Substractor initialization
	GpuMat d_frame;
	GpuMat d_fgmask;
	GpuMat d_fgimg;
	GpuMat d_bgimg;

	Ptr<BackgroundSubtractor> bg_model = cuda::createBackgroundSubtractorMOG2();
	
	//Ptr<BackgroundSubtractor> bg_model = method == "mog" ?
	//	cuda::createBackgroundSubtractorMOG() :
	//	cuda::createBackgroundSubtractorMOG2();

	Mat img0, img, fgmask, fgimg;
	Mat bgimg;

	//vector<Point2f> points[2];

	TermCriteria termcrit(TermCriteria::COUNT | TermCriteria::EPS, 20, 0.03);
	Size subPixWinSize(10, 10), winSize(31, 31);

	const int MAX_COUNT = 500;
	bool needToInit = true;
	bool nightMode = false;
	Point2f point;
	bool addRemovePt = false;


	Mat vstatus_mat, verror_mat;

	// MOVEMENT DETECTION LOOP
	for (;;)
	{
		try {


			cap >> img0;

			if (img0.empty())
				break;

			resize(img0, img, Size(640, 640 * img0.rows / img0.cols), INTER_LINEAR);

			if (fgimg.empty())
				fgimg.create(img.size(), img.type());

			//update the model
			
			d_frame.upload(img);
			bg_model->apply(d_frame, d_fgmask);


			
			d_fgmask.download(fgmask);
		

			//apply medianblur filter
			//to be optimized using CUDA
			Mat mask;

			int MAX_KERNEL_LENGTH = 5;
			for (int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2)
			{
				medianBlur(fgmask, mask, i);
				//if (display_dst(DELAY_BLUR) != 0) { return 0; }
			}
			fgmask = mask;

			// We can detect keypoint with blob detect method

			//Mat     desc, result(fgmask.rows, fgmask.cols, CV_8UC3);
			if (b.dynamicCast<SimpleBlobDetector>() != NULL)
			{

				sbd->detect(fgmask, keyImg, Mat());
				//drawKeypoints(fgmask, keyImg, result);


				int i = 0;
				for (vector<KeyPoint>::iterator k = keyImg.begin(); k != keyImg.end(); k++, i++)
				{
					circle(fgmask, k->pt, (int)k->size, palette[i], 3);
					circle(img, k->pt, (int)k->size, palette[i], 3);
					//cout << "object cooordenates : " << k->pt.x << ";" << k->pt.y << "\n";

					int fontFace = CV_FONT_HERSHEY_COMPLEX;
					double fontScale = 0.4;
					int thickness = 1;

					putText(fgimg, to_string(i + 1), k->pt, fontFace, fontScale,
						palette[i], thickness, 8);
					putText(img, to_string(i + 1), k->pt, fontFace, fontScale,
						palette[i], thickness, 8);

					//putText(fgimg, "object " + to_string(i) + ": " + to_string((int)k->pt.x) + ";" + to_string((int)k->pt.y), Point(15, 10 + 15 * i), fontFace, fontScale,
					//	palette[i], thickness, 8);
				}

				keyImg.clear();
			}



			//namedWindow(*itDesc + label, WINDOW_AUTOSIZE);
			//imshow(*itDesc + label, result);
			//imshow("Original", img);
			//waitKey();

			//imshow("image result", result);

			imshow("image", img);

			//imshow("foreground mask", fgmask);
			imshow("foreground image", fgmask);


			char k = (char)waitKey(1);
			if (k == 27) break;
			if (k == ' ')
			{
				update_bg_model = !update_bg_model;
				if (update_bg_model)
					printf("Background update is on\n");
				else
					printf("Background update is off\n");
			}

		}
		catch (const std::exception&)
		{


			std::cout << "Error, check input file/device" << std::endl;



		}
	}


	cv::destroyAllWindows();
	img0.release();
	img.release();
	fgimg.release();
	bgimg.release();
	return EXIT_SUCCESS;


}
Example #19
0
int
main(int argc, char** argv)
{
    int iKey = 0;
    string input_file;
    string output_file;
    ifstream input;
    ofstream output;
    int num = 0;

    if (argc != 3)
    {
        cerr << argv[0] << " input_info.txt output_info.txt" << endl;
        return -1;
    }

    input_file = argv[1];
    output_file = argv[2];

    cerr << "Opening input_file with names of images" << endl;
    input.open(input_file.c_str());
    cerr << "Done." << endl;

    output.open(output_file.c_str());

    namedWindow(window_name, 1);

    if (input.is_open())
    {
        string line;

        getline(input, line);
        while (!input.eof())
        {

            cout << "Linha: " << line << endl;
            string s;
            vector<string> strings;
            istringstream iss(line);
            while (getline(iss, s, ' '))
            {
                cout << "Leitura: " << s << endl;
                strings.push_back(s);
            }
            numOfRec = 0;

            cerr << "Loading image :" << strings.at(0) << endl;

            image = imread(strings.at(0), 1);

            if (strings.at(0) != old_string)
            {
                old_string = strings.at(0);
                output << old_string << endl;
            }

            if (!image.empty())
            {

                cout << "x1 " << strings.at(1) << endl;
                int x1 = atoi(strings.at(1).c_str());
                cout << "y1 " << strings.at(2) << endl;
                int y1 = atoi(strings.at(2).c_str());
                cout << "x2 " << strings.at(3) << endl;
                int x2 = atoi(strings.at(3).c_str());
                cout << "y2 " << strings.at(4) << endl;
                int y2 = atoi(strings.at(4).c_str());

                Rect myROI(cvPoint(x1, y1), cvPoint(x2, y2));
                cv::Mat croppedImage;

                cv::Mat(image, myROI).copyTo(croppedImage);
                num++;
                ostringstream myStream; //creates an ostringstream object
                myStream << num << flush;
                imwrite("positives/image" + myStream.str() + ".png", croppedImage);

                rectangle(image, cvPoint(x1, y1), cvPoint(x2, y2), CV_RGB(255, 0, 255), 1);
                imshow(window_name, image);


                iKey = waitKey(5);

                if (iKey == 27)
                {
                    image.release();
                    destroyWindow(window_name);
                    return EXIT_SUCCESS;
                }
            }
            getline(input, line);
            image.~Mat();
        }
    }


    else
    {
        cerr << "Failed to open: " << input_file << endl;
    }


    image.~Mat();
    image2.~Mat();
    input.close();
    output.close();
    destroyWindow(window_name);

    return EXIT_SUCCESS;
}
Example #20
0
// Mean Shift Algorithm
void meanShift(Mat& img1, Mat& img2, Ptr<DescriptorMatcher>& descriptorMatcher, int matcherFilterType, vector<KeyPoint>& tpkeypoints,
               Mat& tpdescriptors, vector<KeyPoint>& keypoints2, Mat& descriptors2, Mat& clusters1, Point2f &cp, int& flag,
               vector<Point2f>& MP1, Mat& img2ROI, vector<KeyPoint>& bkeypoints, Mat& bdescriptors, Mat& temp,
               int& FG_mp, int&FG, int& BG_mp, int& BG, int& FG_BG, int& msI)
{
    size_t i,j;
    // Mat temp=img2.clone();
    int converged = 0, semiConverged = 0;
    Mat img1ROI, labels1_;
    Point2f lastCenter(box.x+box.width/2,box.y+box.height/2);
    float scale = 1;

    img1ROI = img1(boxOrg);
    vector<Point2f> points1, bmp;
    KeyPoint::convert(tpkeypoints, points1);
    searchBin(tpdescriptors, clusters1, labels1_); // clustering based on Kmeans centers obatined earlier

//vector<Point2f> np;
//    Mat newimg = img1ROI.clone();
//     KeyPoint::convert(tpkeypoints, np);
//    for(size_t i=0;i<np.size();i++)
//        circle(newimg, np[i], 2, Scalar(255,0,255),2);

//     imshow( "msimg", newimg );
//    np.clear();

//    waitKey(0);


    vector<float> prevPDF(NOC); // pdf of source
    weightedPDF( points1, boxOrg, labels1_, NOC, prevPDF); // Making histogram/pdf by normalizing the data and applying weights based on positions

    // Iterations for finding the object

    Point2f zBCmax(0,0); // center corspndng to the iteration with max BC
    float BC, BCmax=0, BCprev=0, BCnow=0; // Bhattachrya coefficient, max val for an image, previous and current val for an iteration
    int stopCount=0; // MS iterations must converege for stopCount < ManualSetThreshold
    vector<Point2f> matchedPoints1, matchedPoints2;
    while ( !converged )
    {
       // ofstream tempF;
        //tempF.open("tempF.txt", ios::out);
        matchedPoints1.clear(); matchedPoints2.clear();

        Mat matchedDesc1, matchedDesc2;
        vector<int> queryIdxs, trainIdxs;

        cv::Rect expandedBox;

#ifdef DEBUG
        cout << "iteration in while = \t" << ++iter << endl;
#endif

        if (EXPANDED_BOX)
        {
            expandedBox = Rect(box.x-25, box.y-25, box.width+50, box.height+50);
            boundaryCheckRect(expandedBox);
            img2ROI = img2(expandedBox);
        }
        else
        {
          //  cout << box.br() << "\t" << box.tl() << "\t" << img2.cols << endl;
            img2ROI = img2(box);
        }

        vector<KeyPoint> tempkey;
       // Mat pointsTransed21;
        MP1.clear();

        doIteration(img1ROI, img2ROI, queryIdxs, trainIdxs,descriptorMatcher, matcherFilterType, tpkeypoints,tpdescriptors,
                    keypoints2,descriptors2, matchedDesc1, matchedDesc2,  matchedPoints1, matchedPoints2, MP1,tempkey);

        if(matchedPoints2.size() < 1)
        {
            FG=0; BG=0;FG_mp=0;BG_mp=0;FG_BG=0; msI=0;
            break;

        }
        //  mdescriptors = matchedDesc2;

        //   KeyPoint::convert(keypoints2, points2);


        if (EXPANDED_BOX)
            shiftPoints(matchedPoints2, expandedBox);
        else
            shiftPoints(matchedPoints2, box);


        // shiftPoints(matchedPoints1,boxOrg);
        vector<float> predPDF(NOC,0);

        Mat labels2, labels2_; // depending on PDF_OF_WHOLE
        Point2f z(0,0);


        //==================== Edited at 8th april =======================//
        bmp.clear();
        Mat tmatchedDesc2, tmatchedDesc1;
        vector<Point2f> tmatchedPoints2;
        msI = stopCount;
        FG_mp = matchedPoints2.size();

        vector<KeyPoint> tempbk;
        Mat tempBd;
        vector<DMatch> filteredMatches;
        crossCheckMatching( descriptorMatcher, bdescriptors, descriptors2, filteredMatches, 1 );
        trainIdxs.clear();    queryIdxs.clear();

        for( i = 0; i < filteredMatches.size(); i++ )
        {
            queryIdxs.push_back(filteredMatches[i].queryIdx);
            trainIdxs.push_back(filteredMatches[i].trainIdx);
        }


        vector<Point2f> points1; KeyPoint::convert(bkeypoints, points1, queryIdxs);
        vector<Point2f> points2;   KeyPoint::convert(keypoints2, points2, trainIdxs);
        vector<char> matchesMask( filteredMatches.size(), 0 );
        /////
        Mat H12;
        Mat drawImg;
        if (points2.size() < 4 )
        {
            cout << "backpoints less than 4, hence prev ROI is retained" << endl;
            return;
            for(i=0;i<points2.size();i++)
            {
                bmp.push_back( points2[i]);
                tempBd.push_back(bdescriptors.row(queryIdxs[i]));
                tempbk.push_back(keypoints2[trainIdxs[i]]);
                tempBd.push_back(descriptors2.row(trainIdxs[i]));
                tempbk.push_back(keypoints2[trainIdxs[i]]);
            }
        }
        else
        {
            H12 = findHomography( Mat(points1), Mat(points2), CV_RANSAC, RANSAC_THREHOLD );

            if( !H12.empty() )
            {
                Mat points1t; perspectiveTransform(Mat(points1), points1t, H12);

                for( size_t i1 = 0; i1 < points1.size(); i1++ )
                {
                    double diff = norm(points2[i1] - points1t.at<Point2f>((int)i1,0));
                    if(diff  <= 20)
                    {
                        matchesMask[i1]=1;
                        bmp.push_back( points2[i1]);
                        tempBd.push_back(bdescriptors.row(queryIdxs[i1]));
                        tempbk.push_back(keypoints2[trainIdxs[i1]]);
                        tempBd.push_back(descriptors2.row(trainIdxs[i1]));
                        tempbk.push_back(keypoints2[trainIdxs[i1]]);

                    }
                }

                drawMatches( img1ROI, bkeypoints, img2ROI, keypoints2, filteredMatches, drawImg, CV_RGB(0, 255, 0), CV_RGB(0, 0, 255), matchesMask
             #if DRAW_RICH_KEYPOINTS_MODE
                             , DrawMatchesFlags::DRAW_RICH_KEYPOINTS
             #endif
                             );

            }
        }
        imshow("bm",drawImg);

        //============edit part ====
        shiftPoints(bmp, box);
        vector<int> bflag(bmp.size(),0);

        for(i=0;i<bmp.size();i++)
            bflag[i]=0;

        vector<int> ft(matchedPoints2.size(),0);

        for(i=0;i<matchedPoints2.size();i++)
        {
            ft[i]=0;
            for(j=0; j< bmp.size(); j++)
            {
                double diff = norm (matchedPoints2[i] - bmp[j]);
                // cout << diff << endl;
                if(diff < 0.5)
                {
                    bflag[j]=1;
                    ft[i]=1;
                    break;
                }
            }
            if(ft[i]==0)
            {
                tmatchedPoints2.push_back(matchedPoints2[i]);
                tmatchedDesc1.push_back(matchedDesc1.row(i));
                tmatchedDesc2.push_back(matchedDesc2.row(i));
            }

        }




        //=================================================================//


        // allot descriptors to the clusters to make histogram
        searchBin(tmatchedDesc1, clusters1, labels1_);
        searchBin( tmatchedDesc2, clusters1, labels2);
        if (PDF_OF_WHOLE)
            searchBin( descriptors2, clusters1, labels2_);

        // find the PDF for the above histogram as per weights
        if (PDF_OF_WHOLE)
            weightedPDF( points2, box, labels2_, NOC, predPDF);
        else
            weightedPDF( tmatchedPoints2, box, labels2, NOC, predPDF);

        // find weights for each IPoint as per the values of weighted PDFs
        vector<float> weights(labels2.rows,0);
        Mat imgTemp = img2.clone();
        findWeights( prevPDF, predPDF, labels1_, labels2, weights, queryIdxs, tmatchedDesc1, tmatchedDesc2);

        // find new ROI center as per above weights
        findNewCenter(tmatchedPoints2, weights, box, z);

        lastCenter = Point2f (box.x+box.width/2, box.y+box.height/2);

        // if current BC is less than previous BC, then take mean of the prev and current centers
        BCnow = findBC(predPDF,prevPDF);
        if (BCnow < BCprev)
            z = 0.5*(z+lastCenter);
        BCprev = BCnow;

        // check if ROI centers converge to same pixel
        if ( (norm(z - lastCenter) < 3))
        {
            semiConverged = 1;
            if (!SHOW_FINAL_ROI)
                rectangle(temp, box, Scalar(0,0,255),2);
        }
        else
        {
            // keep iterating
            stopCount++;

            if (stopCount >= MAX_MS_ITER)
            {
                semiConverged = 1;
                flag = 0;
                if (!SHOW_FINAL_ROI)
                    rectangle(temp, box, Scalar(0,0,255),2);
                z = zBCmax;
            }

            box.x = z.x - box.width/2;
            box.y = z.y - box.height/2;
            boundaryCheckRect(box);

            if (stopCount < MAX_MS_ITER)
                if (!SHOW_FINAL_ROI)
                    ;// rectangle(temp, box, Scalar(0,255,0), 2);
        }

        // store values of max BC and corresponding center z
        if ( BCnow > BCmax)
        {
            BCmax = BC;
            zBCmax = z;
        }

        if (semiConverged)
        {
            converged = 1;

            //   FG_mp, FG, BG_mp, BG, FG_BG, msI ;
            //==========edited on 5april ========
            bdescriptors.release();
            bkeypoints.clear();
            for(i=0;i<tempBd.rows;i++)
            {
                bdescriptors.push_back(tempBd.row(i));
                bkeypoints.push_back(tempbk[i]);

            }


            tpdescriptors.release();
            tpkeypoints.clear();
            //============================================//

            for(i=0;i<matchedPoints2.size();i++)
            {
                if(ft[i]==0)
                {
                    tpdescriptors.push_back(matchedDesc1.row(i));
                    tpkeypoints.push_back(tempkey[i]);

                    tpdescriptors.push_back(matchedDesc2.row(i));
                    tpkeypoints.push_back(tempkey[i]);

                }
            }


//=================================
            box.x = z.x - box.width/2;
            box.y = z.y - box.height/2;

           // imgTemp.release();

            trajectory << z.x << "\t" << z.y << "\t" << box.width << "\t" << box.height << endl;

            cp =z;


            cv::circle(temp, z, 3, Scalar(0,255,255), 3);
            cv::rectangle(temp, box, Scalar(255,0,0),2);

            cout << "MP1 \t" << MP1.size() <<"\t" << "bmp \t"  <<bmp.size() << endl;

            for(size_t i=0;i<MP1.size();i++)
            {//circle(temp, MP1[i], 3, Scalar(255,255,255),3);
              circle(temp, matchedPoints2[i], 3, Scalar(255,0,255),3);
            }

            // shiftPoints(bmp,box);
           for(size_t i=0;i<bmp.size();i++)
             { circle(temp, bmp[i], 2, Scalar(0,0,0),2);
              // cout << bmp[i] << endl;
           }

        }

        char c = (char)waitKey(10);
        if( c == '\x1b' ) // esc
        {
            cout << "Exiting from while iterator..." << endl;
            break;
        }
    }




    cv::imshow("Iter", temp);
  //  waitKey(0);
    eachIter.close();

}
Example #21
0
int main (int argc, char **argv){
	if(argc!= 6){
		printf("Usage: ./imreconTest <numImages> <numFirstPasses> <connectivity(4,8)> <marker> <mask>");
		exit(1);
	}
	gpu::setDevice(0);
	// Used to get store timestamp and calc. exec. times
	uint64_t t1, t2;

	// OpenCV Stream to used by our functions
	Stream stream;

	// Objects to store the maker and mask images in the GPU
	GpuMat g_marker, g_mask;

	// Store the reconstructed image in the GPU
	GpuMat g_recon;

	// Store the reconstructed image for the CPU case
	Mat recon;

	// "Get" input parameters
	int num_images = atoi(argv[1]);
	int numFirstPasses = atoi(argv[2]);
	int connectivity=atoi(argv[3]);
	
	// Read marker and mask images	
	Mat marker = imread(argv[4], -1);
	Mat mask = imread(argv[5], -1);

	// Asser the reading operations worked fine
	assert(marker.cols != 0  && marker.rows != 0 && mask.cols == marker.cols && mask.rows == marker.rows);

	// When this value is higher than 1, the input image is "replicated" and the actually image use is 
	// increased (Zoom in) by the factor chosen 
	int zoomFactor = 1;
	cout << "Zoom = "<< zoomFactor<<endl;

	cout << "Marker.type = "<< marker.type() << " cols="<< marker.cols << " rows="<< marker.rows <<endl;

	if(zoomFactor > 1){
		Mat bigMarker( marker.rows * zoomFactor, marker.cols * zoomFactor, marker.type());
		for(int x = 0; x < zoomFactor; x++){
			for(int y = 0; y < zoomFactor; y++){
				Mat roi = cv::Mat(bigMarker, cv::Rect(x*marker.cols, y*marker.rows,marker.cols, marker.rows));
				marker.copyTo(roi);
			}
		}

		Mat bigMask( marker.rows * zoomFactor, marker.cols * zoomFactor, marker.type());
		for(int x = 0; x < zoomFactor; x++){
			for(int y = 0; y < zoomFactor; y++){
				Mat roi = cv::Mat(bigMask, cv::Rect(x*marker.cols, y*marker.rows,marker.cols, marker.rows));
				mask.copyTo(roi);
			}
		}

		marker.release();
		marker = bigMarker;

		mask.release();
		mask = bigMask;
	}


	stream.enqueueUpload(marker, g_marker);
	stream.enqueueUpload(mask, g_mask);
	stream.waitForCompletion();
	std::cout << "finished uploading" << std::endl;
////	//	int connectivity = 4;
		for(int numPasses=1; numPasses < numFirstPasses; numPasses+=1){
			Mat recon2;
			// 4 connectivity
			t1 = cci::common::event::timestampInUS();
			g_recon = nscale::gpu::imreconstructQueueSpeedup<unsigned char>(g_marker, g_mask, connectivity, numPasses,stream,
32);
//			g_recon = nscale::gpu::imreconstructQueueSpeedup<unsigned char>(g_marker, g_mask, connectivity, numPasses,stream);
			stream.waitForCompletion();
			t2 = cci::common::event::timestampInUS();
			g_recon.download(recon2);
			imwrite("test/out-gpu-queueu.ppm", recon2);
			recon2.release();
			cout << "gpu queue_recon"<< connectivity<< " passes "<< numPasses <<" took " << t2-t1<< " ms"<<endl;
			cout << "gpu queue_recon"<< connectivity<< " passes "<< numFirstPasses<<" took " << t2-t1<< " ms"<<endl;
			g_recon.release();
		}
//		int maxBlocks=48;
//		for(int numBlocks=1; numBlocks < maxBlocks; numBlocks+=1){
//			// 4 connectivity
//			t1 = cci::common::event::timestampInUS();
//
//			g_recon = nscale::gpu::imreconstructQueueSpeedup<unsigned char>(g_marker, g_mask, connectivity, numFirstPasses,stream,numBlocks);
//			stream.waitForCompletion();
//			t2 = cci::common::event::timestampInUS();
//			cout << "gpu queue_recon"<< connectivity<< " nBlocks "<< numBlocks<<" took " << t2-t1<< " ms"<<endl;
//			g_recon.release();
//		}
//
//

//		cout << "Connectivity="<<connectivity<<endl;
//		t1 = cci::common::event::timestampInUS();
//		g_recon = nscale::gpu::imreconstruct<unsigned char>(g_marker, g_mask, connectivity, stream);
//		stream.waitForCompletion();
//		t2 = cci::common::event::timestampInUS();
//		std::cout << "gpu recon"<< connectivity <<" took " << t2-t1 << " ms" << std::endl;
//		g_recon.release();

//		t1 = cci::common::event::timestampInUS();
//		recon = nscale::imreconstruct<unsigned char>(marker, mask, connectivity);
//		t2 = cci::common::event::timestampInUS();
//		std::cout << "recon"<< connectivity <<" took " << t2-t1 << "ms" << std::endl;
//		imwrite("test/out-recon8.ppm", recon);
//		recon.release();


	g_marker.release();
	g_mask.release();
	marker.release();
	mask.release();

	return 0;
}
Example #22
0
int main(int argc, char** argv)
{
	String folder = "images";
	String source = "img01.png";

	cout << "Speckle movement generator\n";

	Size dstSize = Size(512, 512); //size of images to generate
	int moves = 100; //number of images (moves) to generate
	Point2f direction = Point2f(10, 10); //starting direction
	Point2f maxDev = Point2f(2, 2); //maximum deviation from direction
	double angle = 0;
	double angleVar = 0;
	double noise = 0;
	double precision = 0.1;

	/**
	* USER INTERFACE
	*
	*/
	if (argc > 2) //arguments from command window
	{
		if (argc > 1)  source = argv[1];
		if (argc > 2)  folder = argv[2];
		if (argc > 3)  dstSize = Size(stoi(argv[3]), stoi(argv[3]));
		if (argc > 4)  moves = stoi(argv[4]);
		if (argc > 5)  direction = Point2f(stod(argv[5]), stod(argv[5]));
		if (argc > 6)  maxDev = Point2f(stod(argv[6]), stod(argv[6]));
		if (argc > 7)  angle = stod(argv[7]);
		if (argc > 8)  noise = stod(argv[8]);
		if (argc > 9)  precision = stod(argv[9]);
	}
	else
	{
		cout << "Usage : %s <image> <dst_folder> <dst_size> <moves> <direction> <deviation> <angle> <noise> <precision> <\n", argv[0];
		cout << "Using default parameters only\n";
	}


	//base image for generator
	Mat image = imread(source, IMREAD_GRAYSCALE);
	//output for ground truth file
	ofstream movesFile(folder + "\\moves.txt");

	//check if enough space left for movement
	Size emptySpace = image.size() - dstSize;
	Point2f maxShift = pabs(direction) + pabs(maxDev);
	if (emptySpace.width < maxShift.x || emptySpace.height < maxShift.y)
	{
		cout << "Too small source image for this translation" << endl;
		return EXIT_FAILURE;
	}

	//set precision
	if (precision > 1.0 || precision < 0.0)
	{
		cout << "Precision has to be positive and less than 1" << endl;
		return EXIT_FAILURE;
	}
	int scale = ceil(pow(precision, -1)); //calculate minimum up-scale needed to achieve precision
	precision = pow(scale, -1);
	movesFile << scale << endl;

	//up-scale to achieve given precision
	Mat bigger;
	resize(image, bigger, Size(), scale, scale, INTER_NEAREST);
	Rect mBiggerROI = Rect((bigger.size() - dstSize * scale) / 2, dstSize * scale); //ROI in the centre
	Point intDirection = Point(direction * scale);
	Point intMaxDev = Point(pabs(maxDev) * scale);

	RNG rng(0xFFFFFF);
	Point position = Point(0, 0);
	double rotation = 0;

	Mat dst;
	Mat rotatedBigger = bigger.clone();
	Point2f sumMeanDev(0);
	for (int i = 0; i <= moves; i++)
	{
		if (i != 0)
		{
			//get next translation vector
			Point move(intDirection.x + rng.uniform(-intMaxDev.x, intMaxDev.x), intDirection.y + rng.uniform(-intMaxDev.y, intMaxDev.y));
			move.y = -move.y; //invert Y axis for Euclidean move

			/*
			TRANSLATION
			*/
			Point nextROIpoint = mBiggerROI.tl() + move;

			//change direction if boundary approached
			if (nextROIpoint.x < 0 || nextROIpoint.x > bigger.cols - mBiggerROI.width)
			{
				intDirection.x = -intDirection.x;
				move.x = -move.x;
			}
			if (nextROIpoint.y < 0 || nextROIpoint.y > bigger.rows - mBiggerROI.height)
			{
				intDirection.y = -intDirection.y;
				move.y = -move.y;
			}

			mBiggerROI += move;
			move.y = -move.y; //re-invert Y axis for Euclidean move
			position += move;


			/*
			ROTATION
			*/
			if (abs(angle) > FLT_EPSILON)
			{
				Point2f pc(bigger.cols / 2.0 + position.x, bigger.rows / 2.0 - position.y);
				rotation += angle + rng.uniform(-angleVar, angleVar);
				Mat r = getRotationMatrix2D(pc, rotation, 1.0);
				warpAffine(bigger, rotatedBigger, r, bigger.size());
			}
			else
				bigger.copyTo(rotatedBigger);

			/*
			OUTPUT TO FILE AND SCREEN
			*/
			movesFile << position.x * precision << "," << position.y * precision << "," << rotation << endl;
			cout << i << ". " << position.x * precision << "," << position.y * precision << "," << rotation << endl;
		}

		/*
		SHRINKING
		*/
		resize(rotatedBigger(mBiggerROI), dst, dstSize, 0., 0., INTER_AREA);

		/*
		NOISE
		*/
		if (noise > DBL_EPSILON)
		{
			sumMeanDev += addNoise(dst, noise);
		}

		imwrite(getName(i, folder), dst);
	}
	movesFile.close();
	bigger.release();
	rotatedBigger.release();
	dst.release();

	return EXIT_SUCCESS;
}
void ImageLibrary::Close( void *handle )
{
	Mat* pSource = (Mat*)handle;
	pSource->release();
	delete pSource;
}
int main(int argc, char** argv) {

	/* set the number of test, default is 10 */
	int numTestPerCls, showResult;
	if (argc > 2) { 
		numTestPerCls = atoi(argv[1]);
		showResult = atoi(argv[2]);
	} else if (argc > 1) {
		numTestPerCls = atoi(argv[1]);
		showResult = 1;
	} else {
		numTestPerCls = 5;
		showResult = 1;
	}

	/* set the directories of the files */
	string outputDir = "/home/user/rccn_for_cloth/VisResults";
	string testDataDir = "/home/user/rccn_for_cloth/data/clothesDataset/test";

	/* setting the number of class, containing the background */
	vector<int> tgt_cls(numCategory);
	for (int i = 0; i < numCategory; i++) {
		tgt_cls[i] = i + 1;
	}

	/* initial the edge detection for getting proposals */
	FastRCNNCls* fastRCNN = new FastRCNNCls;
	fastRCNN->init_box_model("../models/People/propose.model");

	/* initial the caffe model */
	fastRCNN->set_model("/home/user/rccn_for_cloth/models/CaffeNet/test.prototxt", 
			"/home/user/rccn_for_cloth/output/default/clothesDataset/caffenet_fast_rcnn_iter_40000.caffemodel");

	///* initial image helper */
	//ImageHelper *helper = new ImageHelperBackend();

	for (int subCategory = 1; subCategory <= numCategory; subCategory ++) {

		/* read the mapping files */
		string subCategoryDir = testDataDir + "/" + to_string(subCategory);
		string imageSubDir = subCategoryDir + "/" + "images";
		string* imageName = NULL;
		int itemNumber;
		bool mappingflag = guidMapping::getImageName(
				subCategoryDir + "/" + "GUIDMapping.txt",
				&imageName, &itemNumber);
		if (mappingflag == false) {
			cout<<"Error reading the mapping files!"<<endl;
			return false;
		}


		/* processing each image */
		for (int nImage = 0; nImage< min(itemNumber, numTestPerCls); nImage ++) {

			float beginTime =clock();

			cout<<"Now processing the "<<nImage<<" in the "<<subCategory<<" th dir"<<endl;

			Mat img = imread(imageSubDir + "/" + imageName[nImage]);
			fastRCNN->get_boxes(img);

			/* forward into the caffe net */
			vector<PropBox> randPboxes = fastRCNN->detect(img, tgt_cls);

			/* sort the results according to their confidence */
			vector<PropBox> pboxes(randPboxes.size());
			for (int iSort = 0; iSort < pboxes.size(); iSort ++) {
				float maxVal = 0;
				int maxIndex = 0;
				/* find the max value */
				for (int iFind = 0; iFind < pboxes.size(); iFind ++) {
					if (randPboxes[iFind].confidence > maxVal) {
						maxVal = randPboxes[iFind].confidence;
						maxIndex = iFind;
					}
				}
				pboxes[iSort] = randPboxes[maxIndex];
				randPboxes[maxIndex].confidence = 0;
			}

			float endTime =clock();
			cout << "Time: " << (endTime - beginTime)/CLOCKS_PER_SEC << 's' << endl;

			/* plot the prediction infomation */
			for (int nResults = 0; nResults < showResult; nResults ++) {
				string preText = "P:" + to_string(pboxes[nResults].confidence) +
					"C:" + to_string(pboxes[nResults].cls_id);
				putText(img, preText,
						Point(int(pboxes[nResults].x1), int(pboxes[nResults].y1)),
						fontFace, fontScale, Scalar::all(0),
						thickness, 8);
				rectangle(img, 
						Point(int(pboxes[nResults].x1), int(pboxes[nResults].y1)),
						Point(int(pboxes[nResults].x2), int(pboxes[nResults].y2)),
						cvScalar(0, 0, 255), 2, 8, 0);
			}

			//Image *img_show = helper->CreateImage(img.cols, img.rows, Image::Image_BGR);
			//memcpy(img_show->data, img.data, sizeof(unsigned char) * img.rows * img.cols * 3);

			/* plot the results */
			string outputImageDir;
			if (pboxes[0].cls_id == subCategory) {
				outputImageDir = outputDir + "/" + 
					to_string(subCategory) + "/rightCls" 
					+ to_string(nImage) + ".jpg";
			} else outputImageDir = outputDir + "/" + 
				to_string(subCategory) + "/wrongCls" 
					+ to_string(nImage) + ".jpg";
			imwrite(outputImageDir, img);

			//helper->SaveImage(outputImageDir, img_show);
			//helper->FreeImage(img_show);

			img.release();

		}


		delete[] imageName;
	}


	//delete helper;
	delete fastRCNN;
	return 0;	
}
Example #25
0
int main(){

    // Variables
    cvNamedWindow("Original", CV_WINDOW_NORMAL);
    cvNamedWindow("Transformation", CV_WINDOW_NORMAL);

    // Load an image
    Mat src = imread("../Images/stewie.jpg");
    if( !src.data ){
        return -1;
    }

    // Data
    float width = src.cols;
    float height = src.rows;
    float sx = 1;
    float sy = 1;
    float alpha = 0;
    float tx = -width/2;
    float ty = -height/2;
    float a = 0;
    float perspx = 0;
    float perspy = 0;

    // Keys for changing image features
    printf("Traslation: t, f, g, h\n"
           "Rotation: n, m\n"
           "Scale: z, x\n"
           "Skew: o, p\n"
           "Perspective: w, a, s, d\n");

    // Continuous transformation
    while (1){

        // Copy original image
        Mat tmp = Mat(src.cols, src.rows, src.type());

        // Affine Transformation Matrix (Matrix H)
        Mat K = (Mat_<float>(3,3) << 1,0,width/2,0,1,height/2,0,0,1);
        Mat T = (Mat_<float>(3,3) << 1,0,tx,0,1,ty,0,0,1);
        Mat R = (Mat_<float>(3,3) << cos(alpha),-sin(alpha),0,sin(alpha),cos(alpha),0,0,0,1);
        Mat S = (Mat_<float>(3,3) << sx,0,0,0,sy,0,0,0,1);
        Mat P = (Mat_<float>(3,3) << 1,a,0,0,1,0,0,0,1);
        Mat A = (Mat_<float>(3,3) << 1,0,0,0,1,0,perspx,perspy,1);

        // Compute Transformation Matrix H
        Mat H = K*S*R*T*P*A;

        // Transformation of the points
        warpPerspective(src, tmp, H, cvSize(src.cols, src.rows));

        // Show the transformated template in the image
        imshow("Transformation", tmp);
        imshow("Original", src);

        // Read commands
        int c = cvWaitKey(10) & 255;
        switch(c){
        // m rotate CC (right)
        case 109:
            alpha += 0.1;
            break;
        // n rotate CCW (left)
        case 110:
            alpha -= 0.1;
            break;
        // o skew left
        case 112:
            a += 0.1;
            break;
        // p skew right
        case 111:
            a -= 0.1;
            break;
        // x zoom in
        case 120:
            sx += 0.1;
            sy += 0.1;
            break;
        // z zoom out
        case 122:
            sx -= 0.1;
            sy -= 0.1;
            break;
        // t move up
        case 116:
            ty -= 10;
            break;
        // g move down
        case 103:
            ty += 10;
            break;
        // f move left
        case 102:
            tx -= 10;
            break;
        // h move right
        case 104:
            tx += 10;
            break;
        // a: persp x -
        case 97:
            perspx += 0.0001;
            break;
        // d: persp x +
        case 100:
            perspx -= 0.0001;
            break;
        // w: persp y +
        case 119:
            perspy += 0.0001;
            break;
        // s: persp y -
        case 115:
            perspy -= 0.0001;
            break;
        case 27:
            return 0;
            break;
        }
    }

    // Release memory
    src.release();

    return 0;
}
Example #26
0
int trainASM(path inputDir, path outputDir, path modelDir, path partsPath, path detectorPath){

	const Size dsize = Constants::instance()->getSize();	// User-defined image scale size

	Model model(Constants::instance()->getNum1DLevels(), Constants::instance()->getNum2DLevels(), Constants::instance()->getProfLength1d(), Constants::instance()->getProfLength2d());  // Implicitly calls the default constructor
	
	vector<Parts> allParts;		// Vector to hold the Parts information
	if(Constants::instance()->isVerbose())
		cout << "Loading parts file... ";
	if(Parts::loadPartsFile(allParts, partsPath) == 0){		// Load the parts file
		cerr << "Load Parts File Error...";
		return FAILURE;
	}
	model.setModelParts(allParts);
	if(Constants::instance()->isVerbose())
		cout << "done\n";

	vector<Shape> allShapes;
	if(Constants::instance()->isVerbose())
		cout << "Loading directory of images... ";
	if(Shape::loadDirectory(allShapes, inputDir) == 0){	// Load all points into allShapes vector, also computes the profiles for each image
		cerr << "Load Image/Pts Directory Error...";
		return FAILURE;
	}

	int nSamples = allShapes.size();
	if(Constants::instance()->isVerbose())
		cout << "done, Loaded " << nSamples << " images\n";
	model.setNPoints(allShapes[0].getNPoints());
	
	Shape::preScalePtsAndMat(allShapes, dsize, nSamples); // scale all images and points to preset size
	if(Constants::instance()->isVerbose()){
		cout << "All images and points pre-scaled or padded to " << dsize.width << "x" << dsize.height << endl;
		cout << "Training all profiles... ";
	}
	Shape::trainAllProfiles(allShapes, model.getModelParts(), dsize);	// Train all of the profiles for each image
	if(Constants::instance()->isVerbose())
		cout << "done\n";

	vector< vector <Profile1D> > allMean1DProfiles;		// Profile and Covariance data structures that will get stored to the Model
	vector< vector <Profile2D> > allMean2DProfiles;		//
	vector< vector <Mat> > all1DCovarMatrices;			//
	vector< vector <Mat> > all2DCovarMatrices;			//
	if(Constants::instance()->isVerbose())
		cout << "Training all Covariance Matrices and Mean Profiles... ";
	Shape::trainAll1DCovars(allShapes, allMean1DProfiles, all1DCovarMatrices, nSamples);		// Train all of the covariance matrices for each level of resolution 
	Shape::trainAll2DCovars(allShapes, allMean2DProfiles, all2DCovarMatrices, nSamples);		// Also finds the mean profiles for each level
	if(Constants::instance()->isVerbose())
		cout << "done\n";

	// Set the model objects
	model.setMean1DProfs(allMean1DProfiles);
	model.setMean2DProfs(allMean2DProfiles);
	model.set1DCovars(all1DCovarMatrices);
	model.set2DCovars(all2DCovarMatrices);

	Shape avgShape;
	if(Constants::instance()->isVerbose())
		cout << "Aligning all Shapes... \n";
	Shape::alignShapes(allShapes,avgShape);	 // Align all shapes 
	if(Constants::instance()->isVerbose())
		cout << "done\n";
	
	model.setAvgShape(avgShape);

	Mat eigenValues;
	Mat eigenVectors;

	if(Constants::instance()->isVerbose())
		cout << "Computing PCA... \n";
	Shape::computePCA(allShapes, eigenValues, eigenVectors);	// Compute the PCA
	if(Constants::instance()->isVerbose())
		cout << "done\n";

	model.setEigenValues(eigenValues);
	model.setEigenVectors(eigenVectors);

	Detector *det;
	if(Constants::instance()->getDetector_t() == Constants::VJ){
		VJ_Detector VJDet;
		VJDet.face_cascade = new CascadeClassifier();
		det = &VJDet;
		det->init(detectorPath);
	}
#ifdef WITH_PITTPATT
	else if(Constants::instance()->getDetector_t() == Constants::PP){
		PP_Detector PPDet;
		det = &PPDet;
		det->init(detectorPath);
	}
#endif
	if(Constants::instance()->isVerbose())
		cout << "Computing Face Detection... \n";
	if(det->detectAllImages(allShapes) == 0){
		cerr << "Face Detection Function Error... Program Exiting";
		return FAILURE;
	}
	if(Constants::instance()->isVerbose())
		cout << "done\n";

	Shape avgDetectorShape;
	float transX, transY;
	int ref_width = Shape::alignShapesFromDetector(allShapes,avgDetectorShape, transX, transY);
	// Assign the detector average shape to the model
	model.setAvgDetectorShape(avgDetectorShape);
	model.setRefWidth(ref_width);
	model.setDetTranslate(transX, transY);

	// Training Complete
	// Write ASM to binary/text file
	if(Constants::instance()->isVerbose())
		cout << "Writing Model to file... \n\n";
#ifdef _MSC_VER
	if(model.writeASMtoTxtFile(modelDir) == 0){
		cerr << "Failed to write ASM to specified .txt file!!!" << endl;
	}
#endif
	if(model.writeASMtoBinFile(modelDir) == 0){
		cerr << "Failed to write ASM to specified file!!!" << endl;
		return FAILURE;
	}

	eigenValues.release();
	eigenVectors.release();


	if(Constants::instance()->isVerbose())
		cout << "Finished!" << endl;
	return SUCCESS;
}
Example #27
0
// special case, when the convertor needs full ArgInfo structure
static bool pyopencv_to(PyObject* o, Mat& m, const ArgInfo info)
{
    bool allowND = true;
    if(!o || o == Py_None)
    {
        if( !m.data )
            m.allocator = &g_numpyAllocator;
        return true;
    }

    if( PyInt_Check(o) )
    {
        double v[] = {PyInt_AsLong((PyObject*)o), 0., 0., 0.};
        m = Mat(4, 1, CV_64F, v).clone();
        return true;
    }
    if( PyFloat_Check(o) )
    {
        double v[] = {PyFloat_AsDouble((PyObject*)o), 0., 0., 0.};
        m = Mat(4, 1, CV_64F, v).clone();
        return true;
    }
    if( PyTuple_Check(o) )
    {
        int i, sz = (int)PyTuple_Size((PyObject*)o);
        m = Mat(sz, 1, CV_64F);
        for( i = 0; i < sz; i++ )
        {
            PyObject* oi = PyTuple_GET_ITEM(o, i);
            if( PyInt_Check(oi) )
                m.at<double>(i) = (double)PyInt_AsLong(oi);
            else if( PyFloat_Check(oi) )
                m.at<double>(i) = (double)PyFloat_AsDouble(oi);
            else
            {
                failmsg("%s is not a numerical tuple", info.name);
                m.release();
                return false;
            }
        }
        return true;
    }

    if( !PyArray_Check(o) )
    {
        failmsg("%s is not a numpy array, neither a scalar", info.name);
        return false;
    }

    PyArrayObject* oarr = (PyArrayObject*) o;

    bool needcopy = false, needcast = false;
    int typenum = PyArray_TYPE(oarr), new_typenum = typenum;
    int type = typenum == NPY_UBYTE ? CV_8U :
               typenum == NPY_BYTE ? CV_8S :
               typenum == NPY_USHORT ? CV_16U :
               typenum == NPY_SHORT ? CV_16S :
               typenum == NPY_INT ? CV_32S :
               typenum == NPY_INT32 ? CV_32S :
               typenum == NPY_FLOAT ? CV_32F :
               typenum == NPY_DOUBLE ? CV_64F : -1;

    if( type < 0 )
    {
        if( typenum == NPY_INT64 || typenum == NPY_UINT64 || type == NPY_LONG )
        {
            needcopy = needcast = true;
            new_typenum = NPY_INT;
            type = CV_32S;
        }
        else
        {
            failmsg("%s data type = %d is not supported", info.name, typenum);
            return false;
        }
    }

#ifndef CV_MAX_DIM
    const int CV_MAX_DIM = 32;
#endif

    int ndims = PyArray_NDIM(oarr);
    if(ndims >= CV_MAX_DIM)
    {
        failmsg("%s dimensionality (=%d) is too high", info.name, ndims);
        return false;
    }

    int size[CV_MAX_DIM+1];
    size_t step[CV_MAX_DIM+1];
    size_t elemsize = CV_ELEM_SIZE1(type);
    const npy_intp* _sizes = PyArray_DIMS(oarr);
    const npy_intp* _strides = PyArray_STRIDES(oarr);
    bool ismultichannel = ndims == 3 && _sizes[2] <= CV_CN_MAX;

    for( int i = ndims-1; i >= 0 && !needcopy; i-- )
    {
        // these checks handle cases of
        //  a) multi-dimensional (ndims > 2) arrays, as well as simpler 1- and 2-dimensional cases
        //  b) transposed arrays, where _strides[] elements go in non-descending order
        //  c) flipped arrays, where some of _strides[] elements are negative
        if( (i == ndims-1 && (size_t)_strides[i] != elemsize) ||
            (i < ndims-1 && _strides[i] < _strides[i+1]) )
            needcopy = true;
    }

    if( ismultichannel && _strides[1] != (npy_intp)elemsize*_sizes[2] )
        needcopy = true;

    if (needcopy)
    {
        if (info.outputarg)
        {
            failmsg("Layout of the output array %s is incompatible with cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels)", info.name);
            return false;
        }

        if( needcast ) {
            o = PyArray_Cast(oarr, new_typenum);
            oarr = (PyArrayObject*) o;
        }
        else {
            oarr = PyArray_GETCONTIGUOUS(oarr);
            o = (PyObject*) oarr;
        }

        _strides = PyArray_STRIDES(oarr);
    }

    for(int i = 0; i < ndims; i++)
    {
        size[i] = (int)_sizes[i];
        step[i] = (size_t)_strides[i];
    }

    // handle degenerate case
    if( ndims == 0) {
        size[ndims] = 1;
        step[ndims] = elemsize;
        ndims++;
    }

    if( ismultichannel )
    {
        ndims--;
        type |= CV_MAKETYPE(0, size[2]);
    }

    if( ndims > 2 && !allowND )
    {
        failmsg("%s has more than 2 dimensions", info.name);
        return false;
    }

    m = Mat(ndims, size, type, PyArray_DATA(oarr), step);
    m.u = g_numpyAllocator.allocate(o, ndims, size, type, step);
    m.addref();

    if( !needcopy )
    {
        Py_INCREF(o);
    }
    m.allocator = &g_numpyAllocator;

    return true;
}