Esempio n. 1
0
TEST(VoidElement, CopyElement)
{
    tawara::StringElement se(0x80, "12345");

    std::streamsize se_size(se.size());
    tawara::VoidElement ve1(se);
    EXPECT_EQ(se_size, ve1.size());
    EXPECT_FALSE(ve1.fill());

    tawara::VoidElement ve2(se, true);
    EXPECT_EQ(se_size, ve2.size());
    EXPECT_TRUE(ve2.fill());

    // Test elements with a size right on the border of two encoded sizes for
    // the body size
    for (int ii(0); ii < 10; ++ii)
    {
        tawara::BinaryElement be(0x81, std::vector<char>(0x3FFB + ii, 0xC0));
        tawara::VoidElement ve3(be);
        EXPECT_EQ(be.size(), ve3.size());
    }
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])

{
		
	double *X, *Y;
	
	unsigned char *X8;
	
	 unsigned char *B;
	
	 int m, n, p, q, s;
			
	 unsigned char *Z;

	 void *fun;
	
	//--
  	// compute depending on type of image
  	//--

  	//--
  	// UINT8 image
  	//--
  		  	
  	if (mxGetClassID(prhs[0]) == mxUINT8_CLASS) {
  	
  		//--
  		// INPUT
  		//--
  		
  		// input image
					  
	  	X8 = (unsigned char *) mxGetPr(prhs[0]);
	  	
	  	m = mxGetM(prhs[0]);
	  	n = mxGetN(prhs[0]);
	
		// structuring element matrix
		
		if (mxIsUint8(prhs[1])) {
	  		B = (unsigned char *) mxGetPr(prhs[1]);	  
	  	} else {
	  		mexErrMsgTxt("Structuring element must be of class uint8.");
	  	}
				
		p = (mxGetM(prhs[1]) - 1)/2;
		q = (mxGetN(prhs[1]) - 1)/2;
		
		// get parameters
			
		// get mask
						
		if (nrhs > 2) {
		
			if (mxIsEmpty(prhs[2])) {
			
		  		Z = NULL;
		  		
		  	} else {
		  	
			  	if (mxIsUint8(prhs[2])) {
			  		Z = (unsigned char *) mxGetPr(prhs[2]);
			  	} else {
			  		mexErrMsgTxt("Mask must be of class uint8.");
			  	}
			  	
			  	if ((m != mxGetM(prhs[2])) || (n != mxGetN(prhs[2]))) {
					mexErrMsgTxt("Image and mask must be of the same size.");
				}
				
			}
			
		  	
		} else {
		
			Z = NULL;
		
		}
		
		//--
	  	// OUTPUT 
	  	//--
  		
  		// median filtered image

  		Y = mxGetPr(plhs[0] = mxCreateDoubleMatrix((m - 2*p), (n - 2*q), mxREAL));
  	
	  	//--
	  	// COMPUTATION
	  	//--

  		// choose sorting function
  		
  		s = se_size (B,p,q);
  		
  		switch (s) {
  		
  			case 3:
  				fun = opt_med3_uint8;
  				break;
  			case 5:
  				fun = opt_med5_uint8;
  				break;
  			case 7:
  				fun = opt_med7_uint8;
  				break;
  			case 9:
  				fun = opt_med9_uint8;
  				break;
  			default:
  				if (s % 2) {
  					fun = quick_select_uint8;
  				} else {
  					fun = median_interp_uint8;
  				}
  				
  		}

		// compute median filter
  	
  		median_filter_uint8 (Y, X8, m, n, B, p, q, Z, fun);
  	
  	//--
  	// DOUBLE image
  	//--
  	
  	} else {
  	
  		//--
  		// INPUT
  		//--
  		
  		// input image
					  
	  	X = mxGetPr(prhs[0]);
	  	
	  	m = mxGetM(prhs[0]);
	  	n = mxGetN(prhs[0]);
	
		// structuring element matrix
		
		if (mxIsUint8(prhs[1])) {
	  		B = (unsigned char *) mxGetPr(prhs[1]);	  
	  	} else {
	  		mexErrMsgTxt("Structuring element must be of class uint8.");
	  	}
				
		p = (mxGetM(prhs[1]) - 1)/2;
		q = (mxGetN(prhs[1]) - 1)/2;
		
		// get parameters
			
		// get mask
						
		if (nrhs > 2) {
		
			if (mxIsEmpty(prhs[2])) {
			
		  		Z = NULL;
		  		
		  	} else {
		  	
			  	if (mxIsUint8(prhs[2])) {
			  		Z = (unsigned char *) mxGetPr(prhs[2]);
			  	} else {
			  		mexErrMsgTxt("Mask must be of class uint8.");
			  	}
			  	
			  	if ((m != mxGetM(prhs[2])) || (n != mxGetN(prhs[2]))) {
					mexErrMsgTxt("Image and mask must be of the same size.");
				}
				
			}
			
		  	
		} else {
		
			Z = NULL;
		
		}
		
		//--
	  	// OUTPUT 
	  	//--
  		
  		// median filtered image

  		Y = mxGetPr(plhs[0] = mxCreateDoubleMatrix((m - 2*p), (n - 2*q), mxREAL));
  	
	  	//--
	  	// COMPUTATION
	  	//--

		// choose sorting function
  		
  		s = se_size (B,p,q);
  		
  		switch (s) {
  		
  			case 3:
  				fun = opt_med3_double;
  				break;

  			case 5:
  				fun = opt_med5_double;
  				break;
  			
			case 7:
  				fun = opt_med7_double;
  				break;
  			
			case 9:
  				fun = opt_med9_double;
  				break;
  			
			default:
  				if (s % 2) {
  					fun = quick_select_double;
  				} else {
  					fun = median_interp_double;
  				}
  				
  		}

		// compute median filter
  	
  		median_filter_double (Y, X, m, n, B, p, q, Z, fun);
  	
  	}

}
void median_filter_uint8 (
	double *Y,
	unsigned char *X, int m, int n,
	unsigned char *B, int p, int q, unsigned char *Z,
	unsigned char (*fun) (unsigned char *W, int s))

{
		
	//--
	// index
	//--
	
	 int i, j, ij, k, l, kk; 
	
	 int *J, jj;
	
	//--
	// structuring element
	//--
	
	 int r, s;
			
	 double c, *Vx, *Vy; 
	
	//--
	// buffer
	//--
	
	 unsigned char *W;
			
//--
// INITIALIZATION
//--
	
	//--
	// structuring element
	//--
	
	s = se_size (B, p, q);
	
	Vx = mxCalloc(s, sizeof(double));
	Vy = mxCalloc(s, sizeof(double));
	
	se_b2v (Vx, Vy, B, p, q, s);
	
	//--
	// SE index jumps
	//--
	
	J = mxCalloc(s, sizeof(int));
	
	for (kk = 0; kk < s; kk++) {
		*(J + kk) += *(Vy + kk) + (*(Vx + kk) * m);
	}
				
	//--
	// allocate window buffer
	//--
		
	W = mxCalloc(s, sizeof(unsigned char));

//--
// full computation
//-- 
  	
  	if (Z == NULL) {
  			
	  	// loop image
	  	
	  	for (j = q; j < (n - q); j++) {
	  	for (i = p; i < (m - p); i++) {
	  			  		
	  		ij = i + (j * m);
	  		
	  		// loop se
	  									
			for (k = 0; k < s; k++) {
	        	jj = ij + *(J + k);
	        	*(W + k) = *(X + jj);
			}
		
			// compute and save median
			
			*(Y + (i - p) + (j - q)*(m - 2*p)) = (double) fun (W, s);
			
		}
		}
					
	}
  	  	
//--
// mask operator
//--

	else {
  	
	  	// loop image
	  	
	  	for (j = q; j < (n - q); j++) {
	  	for (i = p; i < (m - p); i++) {
	  		
	  		ij = i + (j * m);
	  		
	  		if (*(Z + ij)) {
	  		
		  		// loop se
		  					
				for (k = 0; k < s; k++) {
		        	jj = ij + *(J + k);
		        	*(W + k) = *(X + jj);
				}	

				// compute and save median
				
				*(Y + (i - p) + (j - q)*(m - 2*p)) = (double) fun (W, s);
			
			} else {
				
				// leave pixel unchanged
				
				*(Y + (i - p) + (j - q)*(m - 2*p)) = *(X + ij);
					
			}
			
		}
		}
	
	}
		
}