Php::Value FastImage::crop(Php::Parameters &parameters)
{
	Php::Value self(this);

	int x = parameters[0];
	int y = parameters[1];
	int w = parameters[2];
	int h = parameters[3];

    if (x < 0) throwMustBePositiveOrNilException("1", "FastImage::crop", parameters[0].stringValue());
    if (y < 0) throwMustBePositiveOrNilException("2", "FastImage::crop", parameters[1].stringValue());
    if (w <= 0) throwMustBePositiveException("3", "FastImage::crop", parameters[2].stringValue());
    if (h <= 0) throwMustBePositiveException("4", "FastImage::crop", parameters[3].stringValue());

    if ((x + w) > cvImage.size().width) {
    	throwOutOfBoundException("FastImage::crop");
    }

    if ((y +h) > cvImage.size().height) {
         	throwOutOfBoundException("FastImage::crop");
    }

	cv::Rect roi(x, y, w, h);
	cv::Mat croppedImage = cvImage(roi);

	cvImage = croppedImage;
	croppedImage.release();

	return self;
}
Esempio n. 2
0
		void process( FrameData &frameData ) {
			vector< vector< FrameData::Cluster> > &clusters = frameData.clusters;
			vector< vector< FrameData::Match > > matches = frameData.matches;
			
			Image *img = frameData.images[0].get();
			cv::Mat cvImage( img->height, img->width, CV_8UC1 );
			for (int y = 0; y < img->height; y++) {
				for (int x = 0; x < img->width; x++) {
					cvImage.at<uchar>(y, x) =  (float) img->data[img->width*y+x];
				}
			}	
			
			vector<int> modelSize;
			foreach( model, *models ) 
				modelSize.push_back(model->IPs["SIFT"].size());			
					
			for ( int model = 0; model < (int)clusters.size(); model ++ ) {
				int featNum = modelSize[model];				
				for ( int cluster = 0; cluster < (int)clusters[model].size(); cluster ++ ) {
					// Unify the observation features
					list<int>::iterator it;
					int count = 0;
					vector<Node> nodes;
					map<Pt<3>, int> usedObsPts;
					vector<int> obsPts;	
					for ( it = clusters[model][cluster].begin(); it != clusters[model][cluster].end(); it ++ ) {
						
						int idx = *it;
						Pt<3> obsPt = matches[model][idx].cloud3D;
						Pt<3> mdlPt = matches[model][idx].coord3D;
						Pt<2> imgPt = matches[model][idx].coord2D;
						int mdlIdx = matches[model][idx].featIdx;
						if ( usedObsPts[obsPt] == 0 ) {
							usedObsPts[obsPt] = count;
							Node node;
							node.obsPos = obsPt;
							node.mdlPos.push_back(mdlPt);
							node.imgPos.push_back(imgPt);
							node.obsIdx = idx;
							node.obsIdxs.push_back( idx );
							node.mdlIdxs.push_back( mdlIdx );
							nodes.push_back( node );
							count ++;
						}
						else {
							int tmp = usedObsPts[obsPt];
							nodes[tmp].mdlPos.push_back(mdlPt);
							nodes[tmp].imgPos.push_back(imgPt);
							nodes[tmp].mdlIdxs.push_back( mdlIdx );	
							nodes[tmp].obsIdxs.push_back( idx );						
						}
					}
					
					map<int, int> usedMdlPts;
					
					// Weighted vertex cover
					/*
					for ( int node = 0; node < nodes.size(); node ++ ) {
						int matchNum = nodes[node].obsIdxs.size();
						if ( matchNum != 1 ) {
							// multiple matches for 1 observed feature
							double minDist = INT_MAX;
							int minIdx;
							for ( int i = 0; i < nodes[node].obsIdxs.size(); i ++ ) {
								int matchIdx = nodes[node].obsIdxs[i];
								double matchDist = matches[model][matchIdx].dist;
								if ( dist < minDist ) {
									minDist = dist;
									minIdx = i;
								}
							}
							for ( int i = 0; i < nodes[node].obsIdxs.size(); i ++ ) {
								if ( i != minIdx ) {
									nodes[node].obsIdxs.erase( nodes[node].obsIdxs.begin() + i );
									nodes[node].mdlIdxs.erase( nodes[node].mdlIdxs.begin() + i );
								}
							}						
						}
						int mdlSize = nodes[node].mdlIdxs.size();
						if ( mdlSize == 1 ) {
							int mdlIdx = nodes[node].mdlIdxs[0];
							if ( usedMdlPts[mdlIdx] == 0 ) {
								usedMdlPts[mdlIdx] = node;
							}
							else {
								// compare the matched distances
								int preNode = usedMdlPts[mdlIdx];
								int preMatch = nodes[preNode].obsIdx;
								int curMatch = nodes[node].obsIdx;
							}
						}							
					}
					*/
					// Unweighted vertex cover
					for ( int it = 0; it < nodes.size(); it ++ ) {
						for ( int i = 0; i < nodes[it].mdlIdxs.size(); i ++ ) {
							int mdlIdx = nodes[it].mdlIdxs[i];
							if ( usedMdlPts[mdlIdx] == 0 ) {
								usedMdlPts[mdlIdx] = it;
							}
							else {
								nodes.erase( nodes.begin() + it );
							}
						}
					}
					
					// resetting cluster data
					clusters[model][cluster].clear();
					for ( int it = 0; it < nodes.size(); it ++ )
						clusters[model][cluster].push_back( nodes[it].obsIdx );
//					for ( map<Pt<3>, int>::iterator it = usedObsPts.begin(); it != usedObsPts.end(); ++ it )
//						cout << it->first << " => " << it->second << '\n';		

				}
			}	
		}