Exemplo n.º 1
0
/// ****************************************************
///
///                     CARTOON FILTER
///
/// ****************************************************
bool testApp::cvFilterCartoon(ofxCvColorImage &src, ofxCvColorImage &dst, int w, int h)
{
    //CvtColor(src, dst, code)
    //cv::cvtColor(inputFrame, bgr, CV_BGRA2BGR);
    //  cv::pyrMeanShiftFiltering(bgr.clone(), bgr, sp, sr);
    // PyrMeanShiftFiltering(src, dst, sp, sr, max_level=1, termcrit=(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 5, 1))


    // Temporary storage.
    IplImage* pyr = cvCreateImage( cvSize(w,h), IPL_DEPTH_8U, 3 );
    IplImage* edges = cvCreateImage( cvSize(w,h), IPL_DEPTH_8U, 1 );
    IplImage* edgesRgb = cvCreateImage( cvSize(w,h), IPL_DEPTH_8U, 3 );
    //cvSet(s, cvScalar(0,0,0));


    ofxCvGrayscaleImage tempGrayImg;

    tempGrayImg.allocate(w, h);


    tempGrayImg.setFromColorImage(src);


    //------------------------------
    cvPyrMeanShiftFiltering(src.getCvImage(), pyr, 10, 10);

    //  cv::Canny(gray, edges, 150, 150);
    cvCanny(tempGrayImg.getCvImage(), edges, 150,150);
    cvCvtColor(edges, edgesRgb, CV_GRAY2RGB);
    cvAbsDiff(pyr, edgesRgb, pyr);
    //cvAbsDiff(colorImg.getCvImage(), lastFrame.getCvImage(), colorDiff.getCvImage());
    dst.setFromPixels((unsigned char *)pyr->imageData, w, h);
    return true;
}
Exemplo n.º 2
0
////////////////////////////////////////////////////////////////////////////////
// [http://stackoverflow.com/questions/4831813/image-segmentation-using-mean-shift-explained]
// "The Mean Shift segmentation is a local homogenization technique that is 
// very useful for damping shading or tonality differences in localized objects. 
// [...] replaces each pixel with the mean of the pixels in a range-r 
// neighborhood and whose value is within a distance d."
static void  posterize_image(IplImage* input)
{
  // this line reduces the amount of colours
  cvAndS( input, cvScalar(0xF8, 0xF8, 0xF8), input);
  
  int colour_radius =  4; // number of colour classes ~= 256/colour_radius
  int pixel_radius  =  2;
  int levels        =  1;
  cvPyrMeanShiftFiltering(input, input, pixel_radius, colour_radius, levels);
  
}
Exemplo n.º 3
0
void cv::pyrMeanShiftFiltering( const Mat& src, Mat& dst,
                                double sp, double sr, int maxLevel,
                                TermCriteria termcrit )
{
    if( src.empty() )
        return;

    dst.create( src.size(), src.type() );
    CvMat _src = src, _dst = dst;
    cvPyrMeanShiftFiltering( &_src, &_dst, sp, sr, maxLevel, termcrit );
}
Exemplo n.º 4
0
int main( int argc, char** argv )
{
    char* filename = argc >= 2 ? argv[1] : (char*)"fruits.jpg";
     CvTermCriteria termcrit=cvTermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS,5,1);
	double spRadius = 20.0, colorRadius = 40.0;
	int max_level = 2;

    if( (src = cvLoadImage(filename,-1)) == 0 )
        return 0;

	help();   
    cvNamedWindow( "Image", 1 );
	cvNamedWindow( "Segmented", 1);

    dst = cvCloneImage( src );
    cvShowImage( "Image", src );

    for(;;)
    {
        char c;
		do {//In linux, we get rid of shift codes which turn c negative.
			c = cvWaitKey(0);
			if(c < 0) c = -c;
		} while( (c == 30) || (c == 31)); //Shift codes
//		printf("c=%d\n",c);

        if(( (char)c == 27) || ((char)c == 'q') || ((char)c == 'Q') )//Quit
            break;

		switch(c){
			case 'H':
			case 'h':
				help();
				break;
			case's': //save image as watershed.jpg	
            	cvSaveImage("pyrMeanShiftInput.jpg",src);
             	cvSaveImage("pyrMeanShiftOutput.jpg",dst);
             	printf("Saved image pyrMeanShift*.jpg\n");
				break;
			case 'm':
				max_level -= 1;
				if(max_level <0) max_level = 0;
				printf("max_level = %d\n",max_level);
				break;
			case 'M'://"
				max_level += 1;
				printf("max_level = %d\n",max_level);
				break;
			case 'r':
				spRadius -= 1.0;
				if(spRadius < 1.0) spRadius = 1.0;
				printf("spRadius = %lf\n",spRadius);
				break;
			case 'R':
				spRadius += 1.0;
				printf("spRadius = %lf\n",spRadius);
				break;
			case 'c':
				colorRadius -= 1.0;
				if(colorRadius < 1.0) colorRadius = 1.0;
				printf("colorRadius = %lf\n",colorRadius);
				break;
			case 'C':
				colorRadius += 1.0;
				printf("colorRadius = %lf\n",colorRadius);
				break;
			case 'p':
			case '\n':
	           	double t = (double)cvGetTickCount();
				cvPyrMeanShiftFiltering( src, dst, spRadius, colorRadius, max_level, termcrit);
	           	t = (double)cvGetTickCount() - t;
				printf("For params image(x,y)=(%d,%d) max_level=%d, Radius color=%lf, space=%lf\n",
						src->width,src->height,max_level,colorRadius,spRadius);
            	printf( "exec time = %gms\n", t/(cvGetTickFrequency()*1000.) );
    	        cvShowImage( "Segmented", dst );
  				break;
		}
    }

    return 1;
}