コード例 #1
0
ファイル: RampToGray.cpp プロジェクト: jondo/pureImage
/*virtual*/ void RampToGray::_Execute (Arguments& input_args, Arguments& output_args) {

	// validate input arguments
	CheckInputArguments (input_args, GetInputSignature());
	CheckOutputArguments (output_args, GetOutputSignature());
	
	ByteImage in(input_args[0]), out(output_args[0]);
	pI_bool do_create_data(pI_TRUE);
	if (out.HasData()) {
		if ((out.GetChannels() == 1) &&
			(out.GetWidth() == in.GetWidth()) &&
			(out.GetHeight() == in.GetHeight()))
			do_create_data = pI_FALSE;
	}
	if (do_create_data == pI_TRUE) {
		if (out.HasData()) {
			_runtime.GetCRuntime()->FreeArgumentData (
			 _runtime.GetCRuntime(),
			 output_args[0].get());
		}
		out.SetWidth (in.GetWidth());
		out.SetHeight (in.GetHeight());
		out.SetChannels (1);
		try {
			out.CreateData();
		} catch (exception::MemoryException) {
			throw exception::ExecutionException ("Failed to create grayscale image.");
		}
	}

	pI_byte r, g, b;
	pI_float rf, gf, bf;
	pI_size index1, index2;
	// Note: currently, this is done in ByteImage::CreateData()
	// out.SetPitch ((((8 * in.GetWidth()) + 31) / 32) * 4);
	out.SetPath (in.GetPath());
	for (pI_size h = 0; h < in.GetHeight(); ++h) {
		for (pI_size w = 0; w < in.GetWidth(); ++w) {
			r = in.GetData (h, w, 0), g = in.GetData (h, w, 1), b = in.GetData (h, w, 2);
			rf = static_cast<pI_float> (r) / 255.0f;
			gf = static_cast<pI_float> (g) / 255.0f;
			bf = static_cast<pI_float> (b) / 255.0f;
			// in case of gray value, keep it
			if ((r == g) && (r == b)) {
				out.SetData (h, w, 0, r);
				continue;
			}
			GetBestCandidates (rf, gf, bf, index1, index2);
			// check if best candidates are neighbours
			if (std::abs (static_cast<int> (index1) - static_cast<int> (index2)) > 1) {
				// if not, use best candidate
				out.SetData (h, w, 0, static_cast<pI_byte> ((static_cast<float> (index1) / static_cast<float> (_colour_map.size())) * 255.0f));
			} else {
				// otherwise, interpolate
				t_cc cc1(_colour_map[index1 < index2 ? index1 : index2]);
				t_cc cc2(_colour_map[index1 > index2 ? index1 : index2]);
				t_cc diff_cc(Difference (cc1.r, cc2.r), Difference (cc1.g, cc2.g), Difference (cc1.b, cc2.b));
				t_cc diff_p_cc2(Difference (cc2.r, rf), Difference (cc2.g, gf), Difference (cc2.b, bf));
				pI_float norm_diff(0.0f);
				if (diff_cc.r > 0.0)
					norm_diff += diff_p_cc2.r / diff_cc.r;
				if (diff_cc.g > 0.0)
					norm_diff += diff_p_cc2.g / diff_cc.g;
				if (diff_cc.b > 0.0)
					norm_diff += diff_p_cc2.b / diff_cc.b;
				norm_diff /= 3.0f;
				out.SetData (h, w, 0, static_cast<pI_byte> (((static_cast<float> (index1) + norm_diff) / static_cast<float> (_colour_map.size())) * 255.0f));
			}
			
		}
	}
}
コード例 #2
0
void refine_fast_s(double **x, double *y, double *weights,
			int n, int p, double *res,
			double *tmp, double *tmp2,
			double **tmp_mat, double **tmp_mat2,
			double *beta_cand, int kk,
			int conv, double b, double rhoc,
			double *is,
			double *beta_ref, double *scale)
{
/*
// weights = vector of length n
// res = vector of length n
// x = matrix with the data
// y = vector with responses
// tmp = aux vector of length n
// tmp2 = aux vector of length n
// tmp_mat = aux matrix p x p
// tmp_mat2 = aux matrix p x (p+1)
*/
void fast_s_irwls(double **x, double *y,
		double *weights, int n, int p, double *beta_ref,
		double **tmp_mat, double *tmp, double *tmp2);
double norm_diff(double *x, double *y, int n);
double norm(double *x, int n);
int lu(double **a,int *P, double *x);
void get_weights_rhop(double *r, double s, int n,
		double rhoc, double *w);
void r_sum_w_x(double **x, double *w, int n, int p,
			double *tmp,
			double *sum);
void r_sum_w_x_xprime(double **x, double *w, int n, int p,
			double **tmp, double **ans);
double loss_rho(double *r, double scale, int n, int p, double rhoc);
double MAD(double *a, int n, int center, double *tmp,
			double *tmp2);
double vecprime_vec(double *a, double *b, int n);
register int i,j;
int zeroes=0;
double initial_scale = *is, s0;

for(j=0;j<n;j++)
		if( fabs(res[j] = y[j] - vecprime_vec(x[j], beta_cand, p))
				< ZERO ) zeroes++;
/* if "perfect fit", return it with a 0 assoc. scale */
/* if( zeroes > (((double)n + (double)p)/2.) ) */
if( zeroes > ((double)n /2.) )
{
	// Rprintf("\nToo many zeroes, refine_fast_s\n");
	for(i=0;i<p;i++) beta_ref[i] = beta_cand[i];
	*scale = 0.0;
	return;
};

if( initial_scale < 0.0 )
	initial_scale = MAD(res, n, 0, tmp, tmp2);

s0 = initial_scale;
if( conv > 0 )
		kk = MAX_ITER_FAST_S;
if(kk > 0) {
for(i=0; i < kk; i++) {

	/* one step for the scale */
	s0 = s0 * sqrt( loss_rho(res,
					s0, n, p, rhoc) / b );
	/* compute weights for IRWLS */
	get_weights_rhop(res, s0, n, rhoc, weights);
	/* compute the matrix for IRWLS */
	r_sum_w_x_xprime(x, weights, n, p, tmp_mat,
				tmp_mat2);
	/* compute the vector for IRWLS */
	for(j=0; j<n; j++)
			weights[j] = weights[j] * y[j];
	r_sum_w_x(x, weights, n, p, tmp, tmp2);
	for(j=0; j<p; j++)
		tmp_mat2[j][p] = tmp2[j];
	/* solve the system for IRWLS */
	lu(tmp_mat2, &p, beta_ref);
	/* check for convergence? */
	if(conv > 0) {
		if(norm_diff(beta_cand, beta_ref, p) /
					norm(beta_cand, p) < EPSILON ) {
			// Rprintf("\nRelative norm less than EPSILON\n");
			break;
		};
	};
	for(j=0;j<n;j++)
		res[j] = y[j] - vecprime_vec(x[j], beta_ref , p);
	for(j=0; j<p; j++)
		beta_cand[j] = beta_ref[j];
};
};
*scale = s0;
}
コード例 #3
0
int rwls(double **a, int n, int p,
			double *estimate,
			double *i_estimate,
			double scale, double epsilon,
			int max_it, double Psi_constant
			)
{
/* a <- matrix n x (p+1) (n rows and p+1 columns) where the data's stored
 * res <- vector n of residuals
 * b <- auxiliar matrix to store A'A | A'c
 */
int lu(double **, int *, double *);
// void disp_vec(double*,int);
double norm_diff(double *, double *, int);
double Psi_reg(double, double);
double Loss_Tukey(double*, int, double);

double **b,s,*beta1, *beta2, *beta0, *weights, *resid;
double r; // ,loss1,loss2,lambda;
int iterations=0; //, iter_lambda;
register int i,j,k;
if ( (b = (double **) malloc ( p * sizeof(double *) ) )==NULL )
	{// Rprintf("\nRun out of memory in rwls\n");
		exit(1); };
for (i=0;i<p;i++)
	if ( (b[i] = (double *) malloc ( (p+1) * sizeof(double) ) )==NULL )
		{// Rprintf("\nRun out of memory in rwls\n");
			exit(1); };
beta1 = (double *) malloc( p * sizeof(double) );
beta2 = (double *) malloc( p * sizeof(double) );
beta0 = (double *) malloc( p * sizeof(double) );
weights = (double *) malloc( n * sizeof(double) );
resid = (double *) malloc( n * sizeof(double) );
for(i=0;i<p;i++)
	beta2[i] = (beta1[i]=i_estimate[i]) + 1;
/* main loop */
while( (norm_diff(beta1,beta2,p) > epsilon) &&
	( ++iterations < max_it ) ) {
for(i=0;i<n;i++) {
	s=0;
	for(j=0;j<p;j++)
		s += a[i][j] * beta1[j];
	r = a[i][p]- s;
	if(fabs(r/scale)<1e-7)
		weights[i] = 1.0 / scale / Psi_constant;
                else
        	weights[i] = Psi_reg(r/scale, Psi_constant) / (r/scale);
};
for(j=0;j<p;j++) beta2[j]=beta1[j];
// /* get the residuals and loss for beta2 */
// for(i=0;i<n;i++)
// 	{ s = 0;
// 	for(j=0;j<p;j++)
// 		s += a[i][j] * beta2[j];
// 	resid[i] = (a[i][p] - s)/scale;
// 	};
// loss2 = Loss_Tukey(resid,n,Psi_constant);
/* S+ version of the following code
 * A <- matrix(0, p, p)
 * Y <- rep(0, p)
 * for(i in 1:n) {
 * A <- A + w[i] * a[i,0:(p-1)] %*% t(a[i,0:(p-1)])
 * Y <- Y + w[i] * a[i,0:(p-1)] * a[i,p]
 * }
 * beta1 <- solve(A, Y)
 */
for(j=0;j<p;j++)
	for(k=0;k<=p;k++)  {
		b[j][k]=0.0;
		 for(i=0;i<n;i++)
			b[j][k] += a[i][j] * a[i][k] * weights[i];
		};
lu(b,&p,beta1);
/* is beta1 good enough? */
/* get the residuals and loss for beta1 */
// for(i=0;i<n;i++)
// 	{ s = 0;
// 	for(j=0;j<p;j++)
// 		s += a[i][j] * beta1[j];
// 	resid[i] = (a[i][p] - s)/scale;
// 	};
// loss1 = Loss_Tukey(resid,n,Psi_constant);
// for(j=0;j<p;j++) beta0[j] = beta1[j];
// lambda = 1.;
// iter_lambda=0;
// while( ( loss1 > loss2 ) ) {
// 	Rprintf("%f - %f\n", loss1, loss2);
// 	lambda /= 2.;
// 	for(j=0;j<p;j++)
// 		beta0[j] = (1 - lambda) * beta2[j] + lambda * beta1[j];
// 	/* get the residuals and loss for beta0 */
// 	for(i=0;i<n;i++)
// 		{ s = 0;
// 		for(j=0;j<p;j++)
// 			s += a[i][j] * beta0[j];
// 		resid[i] = a[i][p] - s;
// 		};
// 	loss1 = Loss_Tukey(resid,n,Psi_constant);
// 	if( ++iter_lambda > 10) {
// 		/* Rprintf("\nStuck in local search. Rwls. ");
// 		Rprintf("%f - %f\n",loss1,loss2);  */
// 		loss1 = loss2; /* force the exit */
// 		for(j=0;j<p;j++) beta0[j] = beta2[j];
// 		/* return(1); */
// 	};
// }; /* end while(loss2 <= loss1 ) */
// for(j=0;j<p;j++) beta1[j] = beta0[j];
}; /* end while(norm_diff(...)   */
// for(j=0;j<p;j++) estimate[j]=beta0[j];
for(j=0;j<p;j++) estimate[j]=beta1[j];
free(weights);free(beta1);free(beta2);
free(beta0);free(resid);
for(i=0;i<p;i++) free(b[i]);
free(b);
if( iterations == max_it )
	return 1;
	else
	return 0;
}
コード例 #4
0
ファイル: sobelOpenCV.cpp プロジェクト: rl404/CSC495-DIP
int main(int argc, char *argv[])
{
	IplImage* img = 0;
	int height,width,step,channels;
	unsigned char *data;

	// load an image
	img= cvLoadImage("kantai.png");
	if(!img){
		printf("Could not load image file: %s\n",argv[1]);
		exit(0);
	}

	// get the image data
	height    = img->height;
	width     = img->width;
	step      = img->widthStep;
	channels  = img->nChannels;
	data      = (uchar *)img->imageData;

	printf("Processing a %dx%d image with %d channels\n",height,width,channels);
	printf("step = %d\n", step);

	IplImage* imgGrayscale = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1); // 8-bit grayscale is enough.
	// convert to grayscale.
	cvCvtColor(img, imgGrayscale, CV_BGR2GRAY);

	// Create an image for the outputs
	IplImage* imgSobelX = cvCreateImage( cvGetSize(img), IPL_DEPTH_32F, 1 ); // to prevent overflow.
	IplImage* imgSobelY = cvCreateImage( cvGetSize(img), IPL_DEPTH_32F, 1 );
	IplImage* imgSobelAdded = cvCreateImage( cvGetSize(img), IPL_DEPTH_32F, 1 );
	IplImage* imgSobel = cvCreateImage( cvGetSize(img), IPL_DEPTH_8U, 1 ); // final image is enough to be an 8-bit plane.


	// Sobel
	cvSobel(imgGrayscale, imgSobelX, 1, 0, 3);
	cvSobel(imgGrayscale, imgSobelY, 0, 1, 3);
	cvAdd(imgSobelX, imgSobelY, imgSobelAdded);
	cvConvertScaleAbs(imgSobelAdded, imgSobel); //scaled to 8-bit level; important for visibility.


	//----------------------- OULINE EXTRACTION -------------------------------
	// Normal diff
	IplImage* imgNormDiff = cvCreateImage(cvGetSize(img), 8, 1);
	cvCopy(imgGrayscale,imgNormDiff);
	norm_diff(imgNormDiff);

	// Roberts
	IplImage* imgRoberts = cvCreateImage(cvGetSize(img), 8, 1);
	cvCopy(imgGrayscale,imgRoberts);
	roberts(imgRoberts);

	// Sobel
	IplImage* imgSobel2 = cvCreateImage(cvGetSize(img), 8, 1);
	cvCopy(imgGrayscale,imgSobel2);
	sobel(imgSobel2);

	// Laplacian
	IplImage* imgLap = cvCreateImage(cvGetSize(img), 8, 1);
	cvCopy(imgGrayscale,imgLap);
	laplacian(imgLap);

	//--------------------------- ENHANCEMENT --------------------------------
	// Laplacian
	IplImage* imgLap2 = cvCreateImage(cvGetSize(img), 8, 3);
	IplImage* imgRed = cvCreateImage(cvGetSize(img), 8, 1);
	IplImage* imgGreen = cvCreateImage(cvGetSize(img), 8, 1);
	IplImage* imgBlue = cvCreateImage(cvGetSize(img), 8, 1);

	cvSplit(img, imgRed, imgGreen, imgBlue, NULL);

	laplacian2(imgBlue);
	laplacian2(imgGreen);
	laplacian2(imgRed);
	cvMerge(imgRed,imgGreen,imgBlue, NULL, imgLap2);

	// Variant
	IplImage* imgVariant = cvCreateImage(cvGetSize(img), 8, 3);
	IplImage* imgRed2 = cvCreateImage(cvGetSize(img), 8, 1);
	IplImage* imgGreen2 = cvCreateImage(cvGetSize(img), 8, 1);
	IplImage* imgBlue2 = cvCreateImage(cvGetSize(img), 8, 1);

	cvSplit(img, imgRed2, imgGreen2, imgBlue2, NULL);

	variant(imgBlue2);
	variant(imgGreen2);
	variant(imgRed2);
	cvMerge(imgRed2,imgGreen2,imgBlue2, NULL, imgVariant);

	// Sobel
	IplImage* imgSobel3 = cvCreateImage(cvGetSize(img), 8, 3);
	IplImage* imgRed3 = cvCreateImage(cvGetSize(img), 8, 1);
	IplImage* imgGreen3 = cvCreateImage(cvGetSize(img), 8, 1);
	IplImage* imgBlue3 = cvCreateImage(cvGetSize(img), 8, 1);

	cvSplit(img, imgRed3, imgGreen3, imgBlue3, NULL);

	sobel2(imgBlue3);
	sobel2(imgGreen3);
	sobel2(imgRed3);
	cvMerge(imgRed3,imgGreen3,imgBlue3, NULL, imgSobel3);




	// create a window
	cvNamedWindow("Original", CV_WINDOW_KEEPRATIO);

	cvNamedWindow("Normal different line", CV_WINDOW_KEEPRATIO);
	cvNamedWindow("Roberts line", CV_WINDOW_FREERATIO);
	cvNamedWindow("Sobel line", CV_WINDOW_FREERATIO);
	cvNamedWindow("Laplacian line", CV_WINDOW_KEEPRATIO);

	cvNamedWindow("Laplacian Color", CV_WINDOW_KEEPRATIO);
	cvNamedWindow("Variant", CV_WINDOW_KEEPRATIO);
	cvNamedWindow("Sobel", CV_WINDOW_KEEPRATIO);
	/*cvNamedWindow( "Sobel-x" );
  cvNamedWindow( "Sobel-y" );
  cvNamedWindow( "Sobel-Added" );
  cvNamedWindow( "Sobel-Added (scaled)" );*/

	// show the image
	cvShowImage("Original", img);
	cvShowImage("Normal different line", imgNormDiff);
	cvShowImage("Roberts line",imgRoberts);
	cvShowImage("Sobel line", imgSobel2);
	cvShowImage("Laplacian line", imgLap);

	cvShowImage("Laplacian Color", imgLap2);
	cvShowImage("Variant", imgVariant);
	cvShowImage("Sobel", imgSobel3);

	/*cvShowImage("Sobel-x", imgSobelX);
  cvShowImage("Sobel-y", imgSobelY);
  cvShowImage("Sobel-Added", imgSobelAdded);
  cvShowImage("Sobel-Added (scaled)", imgSobel);*/

	// wait for a key
	cvWaitKey(0);

	// release the image
	cvReleaseImage(&img);
	cvReleaseImage(&imgGrayscale);
	cvReleaseImage(&imgNormDiff);
	cvReleaseImage(&imgRoberts);
	cvReleaseImage(&imgSobel2);
	cvReleaseImage(&imgLap);

	cvReleaseImage(&imgLap2);
	cvReleaseImage(&imgVariant);
	cvReleaseImage(&imgSobel3);

	cvReleaseImage(&imgSobelX);
	cvReleaseImage(&imgSobelY);
	cvReleaseImage(&imgSobelAdded);
	cvReleaseImage(&imgSobel);


	return 0;
}