Example #1
0
/*!
Niblack binarization algorithm.

@param src [in] Mat, single channel uchar image.
@param dst [out] Mat, result image.
@param windowSize [in] int, window size for calculation.
@param k [in] int, parameter for local threshold.
@return int, 0x0000 = Success.
*/
int Niblack(InputArray src, OutputArray dst, int windowSize, float k)
{
	if (src.type() != CV_8UC1 || src.empty())	
		return 0x0001;	/*!< source image type not supported. */

	/*! update window size, which should be odd. */
	if (windowSize < 2)	
		return 0x0002;	/*!< window size not supported. */
	if (windowSize / 2 == 0)
		windowSize++;

	Mat source, destination;
	Mat sourceUchar = src.getMat();
	sourceUchar.convertTo(source, CV_32FC1);

	/*! calcalte mean and variance via
	D(x) = E(x^2) - (Ex)^2 */
	Mat avg, power, avg_power, power_avg;
	Mat standard;
	boxFilter(source, avg, -1, Size(windowSize, windowSize));
	pow(avg, 2, avg_power);
	pow(source, 2, power);
	boxFilter(power, power_avg, -1, Size(windowSize, windowSize));
	sqrt(power_avg - power_avg, standard);

	/*! calculate local threshold */
	Mat threshold = avg + k * standard;

	/*! Output result */
	dst.create(sourceUchar.size(), CV_8UC1);
	destination = dst.getMat();
	destination = source > threshold;

	return 0x0000;
}
void RealtimeO1BilateralFilter::filter(const Mat& src_, Mat& dest_)
{
	Mat src, dest;

	downsample_size = max(downsample_size,1);
	if(downsample_size == 1)
	{
		src = src_;
		dest=dest_;
	}
	else
	{
		resize(src_,src, Size(src_.cols/downsample_size, src_.rows/downsample_size),0.0,0.0,INTER_AREA);
	}

	if(filter_type==FIR_SEPARABLE)
	{
		Size kernel = Size(2*(radius/downsample_size)+1,2*(radius/downsample_size)+1);
		GaussianBlur(src,dest,kernel,sigma_space/downsample_size,0.0,BORDER_REPLICATE);
	}
	else if(filter_type==IIR_AM)
	{
		GaussianBlurAM(src,dest,sigma_space/downsample_size,filter_iteration);
	}
	else if(filter_type==IIR_SR)
	{
		GaussianBlurSR(src,dest,sigma_space/downsample_size);
	}	
	else if(filter_type==FIR_BOX)
	{
		Size kernel = Size(2*(radius/downsample_size)+1,2*(radius/downsample_size)+1);

		if(src.data==dest.data)
		{
			for(int i=0;i<filter_iteration;i++)
			{
				boxFilter(src,dest,CV_32F,kernel,Point(-1,-1),true);
			}
		}
		else
		{
			src.copyTo(dest);
			for(int i=0;i<filter_iteration;i++)
			{
				boxFilter(dest,dest,CV_32F,kernel,Point(-1,-1),true);
			}

		}
	}

	if(downsample_size != 1)
	{
		resize(dest, dest_,src_.size(),0.0,0.0,INTER_LINEAR);
		//resize(dest, dest_,src_.size(),0.0,0.0,INTER_CUBIC);
	}
}
    void testFilterBox() {
        QLandmarkBoxFilter boxFilter(QGeoCoordinate(20,30),QGeoCoordinate(10,40));

        //landmark is in box
        QLandmark lm;
        lm.setCoordinate(QGeoCoordinate(15,35));
        QVERIFY(MockEngine::testFilter(boxFilter,lm));

        //landmark is outside box
        lm.setCoordinate(QGeoCoordinate(50,50));
        QVERIFY(!MockEngine::testFilter(boxFilter,lm));

        //test landmark inside box when box crosses dateline
        QGeoBoundingBox box;
        box.setTopLeft(QGeoCoordinate(20,170));
        box.setBottomRight(QGeoCoordinate(10,-170));
        boxFilter.setBoundingBox(box);

        lm.setCoordinate(QGeoCoordinate(15,-175));
        QVERIFY(MockEngine::testFilter(boxFilter, lm));

        lm.setCoordinate(QGeoCoordinate(15, 175));
        QVERIFY(MockEngine::testFilter(boxFilter, lm));


        //test landmark outside box when box crosses dateline
        lm.setCoordinate(QGeoCoordinate(15, 160));
        QVERIFY(!MockEngine::testFilter(boxFilter, lm));

        lm.setCoordinate(QGeoCoordinate(15, -160));
        QVERIFY(!MockEngine::testFilter(boxFilter, lm));
    }
/*! \fn int sharpeningPGM(Pgm* pgmIn, Pgm* pgmOut)
 * \brief Sharpen an image \a pgmIn. The final result is stored in \a pgmOut.
 *
 * Apply a filter (the difference between an identity and a box filter) to the image stored in \a pgmIn.
 * \param pgmIn Pointer to the input PGM image structure.
 * \param pgmOut Pointer to the output PGM image structure.
 * \return 0 on success, -1 if either pgmIn or pgmOut are NULL.
 */
int sharpeningPGM(Pgm* pgmIn, Pgm* pgmOut)
{
    if(!pgmIn)
    {
        fprintf(stderr, "Error! No input data. Please Check.\n");
        return -1;
    }
    
    if(!pgmOut)
    {
        fprintf(stderr, "Error! No space to store the result. Please Check.\n");
        return -1;
    }
    
    // create a identity filter
    Filter *idFilter = identityFilter(3,3);
    
    // create a box filter
    Filter *bxFilter = boxFilter(3,3);
    
    // sharpening
    Filter* filter = linearAddFilter(idFilter, bxFilter, 2.0, -1.0);
    freeFilter(&idFilter);
    freeFilter(&bxFilter);
    
    resetPGM(pgmOut);
    convolution2DPGM(pgmIn, pgmOut, filter);
    
    freeFilter(&filter);
    
    return 0;
}
void niBlackThreshold( InputArray _src, OutputArray _dst, double maxValue,
        int type, int blockSize, double delta )
{
    // Input grayscale image
    Mat src = _src.getMat();
    CV_Assert(src.channels() == 1);
    CV_Assert(blockSize % 2 == 1 && blockSize > 1);
    type &= THRESH_MASK;

    // Compute local threshold (T = mean + k * stddev)
    // using mean and standard deviation in the neighborhood of each pixel
    // (intermediate calculations are done with floating-point precision)
    Mat thresh;
    {
        // note that: Var[X] = E[X^2] - E[X]^2
        Mat mean, sqmean, stddev;
        boxFilter(src, mean, CV_32F, Size(blockSize, blockSize),
                Point(-1,-1), true, BORDER_REPLICATE);
        sqrBoxFilter(src, sqmean, CV_32F, Size(blockSize, blockSize),
                Point(-1,-1), true, BORDER_REPLICATE);
        sqrt(sqmean - mean.mul(mean), stddev);
        thresh = mean + stddev * static_cast<float>(delta);
        thresh.convertTo(thresh, src.depth());
    }

    // Prepare output image
    _dst.create(src.size(), src.type());
    Mat dst = _dst.getMat();
    CV_Assert(src.data != dst.data);  // no inplace processing

    // Apply thresholding: ( pixel > threshold ) ? foreground : background
    Mat mask;
    switch (type)
    {
    case THRESH_BINARY:      // dst = (src > thresh) ? maxval : 0
    case THRESH_BINARY_INV:  // dst = (src > thresh) ? 0 : maxval
        compare(src, thresh, mask, (type == THRESH_BINARY ? CMP_GT : CMP_LE));
        dst.setTo(0);
        dst.setTo(maxValue, mask);
        break;
    case THRESH_TRUNC:       // dst = (src > thresh) ? thresh : src
        compare(src, thresh, mask, CMP_GT);
        src.copyTo(dst);
        thresh.copyTo(dst, mask);
        break;
    case THRESH_TOZERO:      // dst = (src > thresh) ? src : 0
    case THRESH_TOZERO_INV:  // dst = (src > thresh) ? 0 : src
        compare(src, thresh, mask, (type == THRESH_TOZERO ? CMP_GT : CMP_LE));
        dst.setTo(0);
        src.copyTo(dst, mask);
        break;
    default:
        CV_Error( CV_StsBadArg, "Unknown threshold type" );
        break;
    }
}
/*! \fn int averagePGM(Pgm* pgmIn, Pgm* pgmOut)
 * \brief Apply a box filter to the image \a pgmIn. The final result is stored in \a pgmOut.
 *
 * For each pixel of \a pgmIn compute the average of a 3x3 subarray centered at the pixel.
 * \param pgmIn Pointer to the input PGM image structure.
 * \param pgmOut Pointer to the output PGM image structure.
 * \return 0 on success, -1 if either pgmIn or pgmOut are NULL.
 */
int averagePGM(Pgm *pgmIn, Pgm* pgmOut)
{
    // apply a box filter
    Filter *bxFilter = boxFilter(3,3);
    
    int ret = convolution2DPGM(pgmIn, pgmOut, bxFilter);
    
    freeFilter(&bxFilter);
    
    return ret;
}
Example #7
0
void testApp::updateTrianglesRandom() {
	Mat mat = Mat(kinect.getHeight(), kinect.getWidth(), CV_32FC1, kinect.getDistancePixels());
	
	Sobel(mat, sobelxy, CV_32F, 1, 1);
	
	sobelxy = abs(sobelxy);
	int randomBlur = panel.getValueI("randomBlur") * 2 + 1;
	boxFilter(sobelxy, sobelbox, 0, cv::Size(randomBlur, randomBlur), Point2d(-1, -1), false);
	
	triangulator.reset();
	points.clear();
	int i = 0;
	attempts = 0;
	int randomCount = panel.getValueI("randomCount");
	float randomWeight = panel.getValueF("randomWeight");
	while(i < randomCount) {
		Point2d curPosition(1 + (int) ofRandom(sobelbox.cols - 3), 
												1 + (int) ofRandom(sobelbox.rows - 3));
		float curSample = sobelbox.at<unsigned char>(curPosition) / 255.f;
		float curGauntlet = powf(ofRandom(0, 1), 2 * randomWeight);
		if(curSample > curGauntlet) {
			points.push_back(toOf(curPosition));
			triangulator.addPoint(curPosition.x, curPosition.y, 0);
			sobelbox.at<unsigned char>(curPosition) = 0; // don't do the same point twice
			i++;
		}
		attempts++;
		if(i > attempts * 100) {
			break;
		}
	}
	
	// add the edges
	int w = mat.cols;
	int h = mat.rows;
	for(int x = 0; x < w; x++) {
		triangulator.addPoint(x, 0, 0);
		triangulator.addPoint(x, h - 1, 0);
	}
	for(int y = 0; y < h; y++) {
		triangulator.addPoint(0, y, 0);
		triangulator.addPoint(w - 1, y, 0);
	}
	
	triangulator.triangulate();
	
	int n = triangulator.triangles.size();
	triangles.resize(n);
	for(int i = 0; i < n; i++) {
		triangles[i].vert3 = triangulator.triangles[i].points[0];
		triangles[i].vert2 = triangulator.triangles[i].points[1];
		triangles[i].vert1 = triangulator.triangles[i].points[2];
	}
}
int main (int argc,char* argv[]){
	if (argc != 2 && argc != 3){
		printf("usage:\n %s /path/to/recoding/filename.oni\n",argv[0]);
		return 0;
	}
	Xn_sensor sensor(WIDTH,HEIGHT);
	sensor.play(argv[1],false);
	cvNamedWindow( "Model Extractor Viewer", 1 );
	IplImage* rgb_image = cvCreateImageHeader(cvSize(WIDTH,HEIGHT), 8, 3);
	IplImage* test = cvCreateImageHeader(cvSize(WIDTH,HEIGHT), 8, 3);
	IplImage* gray = cvCreateImage(cvSize(WIDTH,HEIGHT), 8, 1);
	Mat img;
	pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB>);
	pcl::PointCloud<pcl::PointXYZRGB>::Ptr model (new pcl::PointCloud<pcl::PointXYZRGB>);
	
	//pcl::visualization::CloudViewer viewer("Model Extractor Viewer");

	
	//Read Fiducial from file
	Fiducial fiducial("fiducial.yml");
	Pose pose;
	  while(/*!viewer.wasStopped() && */!sensor.endPlaying()){
		//Get the frame 
		sensor.update();
		sensor.getPCL(cloud);
		cvSetData(rgb_image,sensor.rgb,rgb_image->widthStep);
		//Estimate Camera Pose from fiducial
		cvCvtColor(rgb_image,gray,CV_BGR2GRAY);
		if (fiducial.find(gray,true)){
			pose.estimate(gray,fiducial);
			//fiducial.draw(rgb_image);
		}
		if (pose.isFound()){
			printf("Rotation");
			printMat<double>(pose.getR());
			printf("Translation");
			printMat<double>(pose.getT());
			//Segment volume around the fiducial
			boxFilter(cloud,pose);
			//Create 3D model
			buildModel(cloud,model);
		}
		//viewer.showCloud (model);
		
	}
	pcl::io::savePCDFileBinary ("model.pcd", *model);
	sensor.shutdown();
	return 0;
}
Example #9
0
void Sample::resample() {
    switch (_sampleType) {
    case SampleType::RANDOM:
        randomSample();
        break;
    case SampleType::JITTERED:
        jitteredSample();
        break;
    }
    switch (_filterType) {
    case FilterType::BOX:
        boxFilter();
        break;
    case FilterType::TENT:
        tentFilter();
        break;
    }
}
Example #10
0
void testApp::update() {
	Mat mat = getMat(depthImage);
	
	Sobel(mat, sobelxy, CV_32F, 1, 1);
	sobelxy = abs(sobelxy);
	boxFilter(sobelxy, sobelbox, 0, cv::Size(7, 7));
	
	triangulator.init();
	points.clear();
	int i = 0;
	attempts = 0;
	while(i < 5000) {
		Point2d curPosition((int) ofRandom(sobelbox.cols - 1), (int) ofRandom(sobelbox.rows - 1));
		float curSample = sobelbox.at<unsigned char>(curPosition) / 255.f;
		float curGauntlet = powf(ofRandom(0, 1), 2 * (float) mouseX / ofGetWidth());
		if(curSample > curGauntlet) {
			points.push_back(makeVec(curPosition));
			triangulator.addPoint(curPosition.x, curPosition.y);
			sobelbox.at<unsigned char>(curPosition) = 0; // don't do the same point twice
			i++;
		}
		attempts++;
		if(i > attempts * 100) {
			break;
		}
	}
	
	int w = mat.cols - 1;
	int h = mat.rows - 1;
	triangulator.addPoint(0, 0);
	triangulator.addPoint(w, 0);
	triangulator.addPoint(w, h);
	triangulator.addPoint(0, h);
	
	triangulator.triangulate();	
}
Example #11
0
int main()
{
	
	int N = 2;
	Mat imageStack[N];
	Mat grayStack[N];
	Mat focal_Measure[N];
	

	Mat lap_x;
	Mat lap_y;


	
	// kernel for boosting intensity of modified laplacian
	float kernel[3][3] = {{1,1,1}, {1,1,1}, {1,1,1}};
	Mat boostingFilter = Mat(3, 3, CV_32FC1, kernel);
	//Load the image stack and convert it to grayscale.
	//TODO !
	//Loaded images in unint8. Consider changing them to float during grayscale
	//Conversion for arithmetic precision. 
	for(int i = 0; i < N; i++){
		char buffer[50];
		sprintf(buffer,"align/img%d.jpg",i+1);
		imageStack[i] = imread(buffer);
		
		if(imageStack[i].cols > 1000 || imageStack[i].rows > 1000){
		
			float scale = 1.0/(max(imageStack[i].rows,imageStack[i].cols)/1000+1);
			Mat resizedImg;
			
			resize(imageStack[i],resizedImg,Size(0,0),scale,scale,CV_INTER_AREA);
			imageStack[i] = resizedImg;
		}
		
		
		if(!imageStack[i].data){
			cerr << "Could not open or find the image" << endl;
			exit(0);
		}
		
		if(i>0){
			Mat imgPrev;
			Mat imgNext;
			
			alignToPrevImage(imageStack[i-1],imageStack[i],imgPrev,imgNext);
			imageStack[i-1] = imgPrev;
			imageStack[i] = imgNext;
			
			/*
			namedWindow("img1",CV_WINDOW_NORMAL);
			imshow("img1",imgPrev);
			namedWindow("img2",CV_WINDOW_NORMAL);
			imshow("img2",imgNext);
			cvWaitKey(0);
			*/
		}
		
	}
	
	for(int i = 0; i < N; i++){
		
		char buffer[50];
		sprintf(buffer,"align/img%d.jpg",i+1);
		int rows;
		int cols;

		rows=focal_Measure[0].rows;
		cols=focal_Measure[0].cols;
		
		//Create a new Gray scale image

		Mat grayImg;// = toGray(imageStack[i] );
		Mat grayImgFloat;
		cvtColor(imageStack[i],grayImg,CV_BGR2GRAY,1);
		grayImg.convertTo(grayImgFloat,CV_32FC1,1/255.0);
		grayStack[i] = grayImgFloat;
		
		namedWindow(buffer,WINDOW_AUTOSIZE);
		imshow(buffer,grayImgFloat);
		waitKey(0);

		

		//sid
		Mat gray_image;
 		//cvtColor( imageStack[i], gray_image, CV_BGR2GRAY );
 		//grayStack[i]=gray_image;

		lap_x=lap_dir(grayStack[i],0);
		lap_y=lap_dir(grayStack[i],1);
		


		lap_x=abs(lap_x);
		lap_y=abs(lap_y);
		
		

		Mat modLaplacian;
		addWeighted(lap_x,1,lap_y,1,0.0,modLaplacian);
		

		// commented out-- Sid
		// Size ksize(9,9);
		// float sigma = 10.0;
		// Mat modLapSmooth;
		// GaussianBlur(modLaplacian,modLapSmooth,ksize,sigma);

		

		//siddhartha - 2-May

		//locally boosting all pixel intensities based on a 3X3 neighborhood
		Mat boosted;
		filter2D(modLaplacian, boosted, -1, boostingFilter);

		
		//averaging values of the focal measure: average filter preferred ouver gaussian filter as gaussian does not resolve the issue of noisy patches
		Size ksize(19,19);
		Mat modLapSmooth;
		boxFilter(boosted,modLapSmooth,-1,ksize);
		

		focal_Measure[i]=modLapSmooth;
		
		continue;
		
				
		namedWindow(buffer,WINDOW_AUTOSIZE);
		imshow(buffer,modLapSmooth);
		waitKey(0);

	
	}
	
	for(int i = 0; false && i < N ; i++){
		int u = 100;
		int v = 200;
		cout<< focal_Measure[i].at<float>(u,v)<<endl;
	}
	

	
	int rows;
	int cols;

	rows=focal_Measure[0].rows;
	cols=focal_Measure[0].cols;
	
	cout<<focal_Measure[0].rows<<endl;
	cout<<focal_Measure[0].cols<<endl;

	Mat focusMap= Mat::zeros( rows, cols, CV_8UC1);
	int  maxK;
	double maxVal;
	double tempVal;
	

	for(int y = 0; y < rows; y++)
	{
	  	for(int x = 0; x < cols; x++)
	  	{
			
	  		//Iterate over all the images on the stack and get the one in focus.
	  		maxK=0;
	  		maxVal=focal_Measure[0].at<float>(y,x);
	 		for(int k =1; k< N; k++)
	 		{
	 			tempVal=focal_Measure[k].at<float>(y,x);
	 			// cout<<tempVal<<endl;
	 			if(tempVal>maxVal)
	 			{
	 				maxK=k;
	 				maxVal=tempVal;
	 			}
	  		}
	  		focusMap.at<uchar>(y,x)=maxK;		//TODO take out this scale factor. For visialization and debug	
	  	}
			
 	}

 	//commenting out- Sid ; taken care of smoothing by forming modLapsmooth
	//Focus does not change rapidly among objects. 
	//Smooth it!
	// Size ksize(9,9);
	// float sigma = 12.0;
	// Mat focusMapSmooth;
	// GaussianBlur(focusMap,focusMapSmooth,ksize,sigma);
	
	namedWindow("focusMap",WINDOW_AUTOSIZE);
	imshow("focusMap",focusMap);
	waitKey(0);

	namedWindow("result",WINDOW_AUTOSIZE);
	setMouseCallback("result", CallBackFunc, NULL);
	imshow("result",imageStack[0]);

	int previos=0;
	int current=0;
	int step;
	int i;
	//press ctrl+ mouse click to exit
	while(1)
	{
		while (x_coord == -1 && y_coord == -1) cvWaitKey(100); 

		if(x_coord==0)
			return 0;
	
		//cout<<"coords found:  "<<endl<<x_coord<<endl<<y_coord<<endl;
		current=focusMap.at<uchar>(y_coord,x_coord);
		
		step=current-previos;

		if(step<1)
		{
			for(i=previos; i>=current;i--)
			{
				imshow("result",imageStack[i]);	
				cvWaitKey(2);
			}
			
		}
			
		else
		{
			for(i=previos; i<=current; i++)
			{
				imshow("result",imageStack[i]);	
				cvWaitKey(2);
			}
			
			
		}
			

		//cout<<endl<<step<<endl;

		previos=current;

		
		x_coord=-1;
		y_coord =-1;	
		while (x_coord == -1 && y_coord == -1) cvWaitKey(100); 
	}

	return 0;
}
Example #12
0
Filter CreateBoxFilter(const ParamSet &ps) {
    float xw = ps.FindOneFloat("xwidth", 0.5f);
    float yw = ps.FindOneFloat("ywidth", 0.5f);
    return Filter(xw,yw,boxFilter(xw,yw));
}
Example #13
0
/*	Member function
 * retuns the detected gesture ID
 * Inputs :		frame -> current frame to analyse
 * 				faceRegion -> current face Region
 */
airGestType airGest::analyseGesture( Mat frame ) {
	
	airGestType gest = GEST_INVALID;
	
	resize( frame,
			frame,
			Size( OPTFLW_FRAME_WIDTH, OPTFLW_FRAME_HEIGHT ),
			0,
			0,
			INTER_AREA );
	
	//Prepare canvas to draw intermediate result
	//canvas = frame.clone();
	canvas = Mat( OPTFLW_FRAME_HEIGHT, OPTFLW_FRAME_WIDTH, CV_8UC3, Scalar( 0, 0, 0 ) );
	//accCanvas = Mat( OPTFLW_FRAME_HEIGHT, OPTFLW_FRAME_WIDTH, CV_8UC3, Scalar( 0, 0, 0 ) );
	
	Mat grayFrame;
	//Convert to gray scale
    if( frame.channels() == 3 ) {
		cvtColor( frame, grayFrame, CV_BGR2GRAY );
	}
	else if( frame.channels() == 4 ) {
		cvtColor( frame, grayFrame, CV_BGRA2GRAY );
	}
	else {	//already gray
		grayFrame = frame.clone();
	}
	
	if( currState != AIRGEST_ACTIVE ) { //unless active, return with an invalid code
		prevFrame = grayFrame.clone();
		return gest;
	}
	
	//Here, airGest is active
	currFrame = grayFrame;
	//~ std::cout << "[airGest::analyseGesture] calculating optical flow....";
	calcOpticalFlowFarneback( prevFrame,  // first 8-bit single channel input image
                              currFrame,  // Second image of the same size and same type as prevgray
                              flowMap,    // computed flow image tha has the same size as pregray and type CV_32FC2
							  0.5,       // pryScale, 0.5 means classical pyramid
							  3,          // number of pyramid layers including initial image
                              20,         // winSize
                              3,          // number of iterations the algorithm does at each pyramid level
                              5,          // Size of the pixel neighborhood used to find polynomial expansion in each pixel
                              1.1,        // standard deviation of Guassian
                              OPTFLOW_FARNEBACK_GAUSSIAN );        //
    //~ std::cout << "[COMPLETED]\n";
    //~ std::cout << "-> boxFilter";
    boxFilter( flowMap, flowMap , -1, BLUR_KERNEL_SIZE );
    //~ std::cout << "-> drawFlowMap";
    drawFlowMap();
    //~ std::cout << "-> filterFlow";
    filterFlow();
    
    //imshow( "Current flow", canvas );
    //imshow( "Accumulated Flow", accCanvas );
	
	//copy current frame to prev for using next time
	prevFrame = currFrame.clone();
	
	gest = ( decision == -1.00 )? GEST_PREV:
		   ( decision == +1.00 )? GEST_NEXT:
		   GEST_INVALID;
	
	return gest;
}
Example #14
0
kernelType FBlur::buildKernel(int radius){
  return boxFilter(radius);
}