Re: adding noise to an image



Thanks for the answers. Maybe my original questions were not formulated
very well. My problem is not how to produce the noise (what pixels to
select and what values to set them to). My problem is how to draw them
onto the DrawingArea. 

I can't just modify the pixels of the pixbuf, because the pixbuf goes into
the background, and the user draws shapes on top of it. So if the noise
goes with the pixbuf, the shapes won't be noisy. The noise must cover
both the background AND the foreground shapes. So the only way I can
think of putting the noise into the DrawingArea is with a 

Darea->get_window()->draw_point(gc, x,y); 

in a loop. This is one of my questions, is this the right/only/best way?

Then, to draw a point with a different color, I need to set the GC's
foreground color:

gc->set_foreground(some_graylevel_color);

My second question is do I need to allocate all 256 graylevel colors?
Is there a way to work with un-allocated colors?




Incidentally, this is how I generate noise:

1) Salt and Pepper noise: 

let 0<p<1 a fixed, user-defined parameter. 
With probability p, select a pixel. 
If the pixel was selected, with probability 1/2 set it to white,
otherwise set it to black.

2) Uniform noise: same as salt and pepper, except the selected pixels are
set to an arbitrary random value between [0,255].

So the code would be like this:

for (i=0; i<image_size; i++){
	
	// throw the dice:
	double val = rand()/(RAND_MAX+1.0);  // uses high bits.
	if(val<p){
		val = rand()/(RAND_MAX+1.0); // dice for color
		image[i] = val<0.5 ? white : black ; // S&P
	} 
}







On Tue, 18 Oct 2005 21:15:13 -0400, Amadeus W. M. wrote:

> After figuring out how to draw shapes in a drawing area with a pixbuf in
> the background, now I need to add noise, by moving a slider (VScale).
> The shapes are only in the DrawingArea, not in the pixbuf, and the noise
> most go over the entire thing. Randomly, some pixels will be altered,
> others won't. The noise is uniform random between [0,255] (not just black
> and white), so I need to have all 256 gray levels allocated, don't I? 
> 
> I'm thinking this way:
> 
> Gdk::Colors gray[256];
> 
> // allocate colors somewhere.
> 
> for(int y=0; y<Darea->get_height(); y++){
> 	for(int x=0; x<Dara->get_width(); y++){
> 
> 	// Decide if (x,y) should be altered, and if so, 
> 		// choose a random index say, g, between 0 and 255.
> 		gc_->set_foreground(gray[g]);
> 		Darea->get_window()->draw_point(gc_, x,y);
> 	}
> }
> 
> Is this the right way to go about this? 
> How do I allocate the 256 gray levels? I do need to allocate them, don't I?





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]