chiu filter

A forum for exporter development discussion.
User avatar
arneoog
Indigo 100
Posts: 504
Joined: Sun Jun 25, 2006 2:19 am
Contact:

Post by arneoog » Sat May 05, 2007 3:24 am

Okay :)
Here it is :P

Code: Select all

<?php
//-Arne OOG's Firefly Filter------------//
//-Written by Arne OOG------------------//
//-Copyright 2007, Arne OOG-------------//

//--------------------------------------//
//-DESCRIPTION: ------------------------//
//--This Firefly script uses a median---//
//--filter to determin the color of-----//
//--pixels that the script finds--------//
//--brighter than the surrounding pixels//
//--------------------------------------//

//VARIABLES
$filename = (!$_GET[i]) ? "test001.jpg" : $_GET[i];
$y = 0;
$x = 0;
$radius = 1;
$threshold = (!$_GET[t]) ? 1.1 : $_GET[t];

//INITIALIZE
list($width, $height) = getimagesize($filename);
$image = imagecreatefromjpeg($filename);

//THE DESPECKLE FILTER
for($i = 0; $i < ($width * $height); $i++){

	if($x >= $width){
		$y++;
		$x = 0;
	}
	
	$x++;
	//GET CURRENT PIXEL
	$cur_pixel_color = imagecolorat($image, $x, $y);
	$pixel_color = imagecolorsforindex($image, $cur_pixel_color);
	$current_color = imagecolorallocate($image, $pixel_color['red'], $pixel_color['green'], $pixel_color['blue']);

	//GET THE MEDIAN OF THE PIXELS AROUND
	$pixel_color1 = imagecolorat($image, ($x + 0), ($y - 1));
	$pixel_color2 = imagecolorat($image, ($x - 1), ($y + 0));
	$pixel_color3 = imagecolorat($image, ($x + 0), ($y + 1));
	$pixel_color4 = imagecolorat($image, ($x + 1), ($y + 0));
	
	$color_median1 = imagecolorsforindex($image, $pixel_color1);
	$color_median2 = imagecolorsforindex($image, $pixel_color2);
	$color_median3 = imagecolorsforindex($image, $pixel_color3);
	$color_median4 = imagecolorsforindex($image, $pixel_color4);
	
	$color_red = ($color_median1['red'] + $color_median2['red'] + $color_median3['red'] + $color_median4['red']) / 4;		
	$color_gre = ($color_median1['green'] + $color_median2['green'] + $color_median3['green'] + $color_median4['green']) / 4;
	$color_blu = ($color_median1['blue'] + $color_median2['blue'] + $color_median3['blue'] + $color_median4['blue']) / 4;
	
	$avarage_color = imagecolorallocate($image, $color_red, $color_gre, $color_blu);

	//DETERMINE IF THE PIXEL SHOULD BE REPLACED
	$red_thres = ($pixel_color['red'] + 1) / ($color_red + 1);
	$gre_thres = ($pixel_color['green'] + 1) / ($color_gre + 1);
	$blu_thres = ($pixel_color['blue'] + 1) / ($color_blu + 1);
	
	$col_thres = ($red_thres + $gre_thres + $blu_thres) / 3;
	
	if($col_thres >= $threshold){
		imagesetpixel($image, $x, $y, $avarage_color);
	}

}

//OUTPUT
header('Content-type: image/jpeg');
imagejpeg($image, null, 100);
?> 

User avatar
dougal2
Developer
Posts: 2532
Joined: Wed Nov 15, 2006 8:17 am
Location: South London

Post by dougal2 » Sat May 05, 2007 3:37 am

there's a lot of mean averages in there, and I can't see a median average... you sure this is a median filter?

although, that said, i can't argue with the fact that it works. :)


As far as I understand it, a median filter scans the imange, and for every pixel, makes an array of the surrounding pixel colours and the pixel itself, sorts that array, and then takes the the value of color_array[count(colour_array)/2] for the new pixel value.

User avatar
arneoog
Indigo 100
Posts: 504
Joined: Sun Jun 25, 2006 2:19 am
Contact:

Post by arneoog » Sat May 05, 2007 4:13 am

Hehe :)

Code: Select all

//GET THE MEDIAN OF THE PIXELS AROUND 
$pixel_color1 = imagecolorat($image, ($x + 0), ($y - 1)); 
$pixel_color2 = imagecolorat($image, ($x - 1), ($y + 0)); 
$pixel_color3 = imagecolorat($image, ($x + 0), ($y + 1)); 
$pixel_color4 = imagecolorat($image, ($x + 1), ($y + 0)); 
    
$median1 = imagecolorsforindex($image, $pixel_color1); 
$median2 = imagecolorsforindex($image, $pixel_color2); 
$median3 = imagecolorsforindex($image, $pixel_color3); 
$median4 = imagecolorsforindex($image, $pixel_color4); 
    
$red = ($median1['red'] + $median2['red'] + $median3['red'] + $median4['red']) / 4;       
$gre = ($median1['green'] + $median2['green'] + $median3['green'] + $median4['green']) / 4; 
$blu = ($median1['blue'] + $median2['blue'] + $median3['blue'] + $median4['blue']) / 4; 
    
$avarage_color = imagecolorallocate($image, $red, $gre, $blu); 
It is taking the avarage of the pixels around it.. ;)
If it a true median filter I don't know, but it is close, heh.. :P

User avatar
dougal2
Developer
Posts: 2532
Joined: Wed Nov 15, 2006 8:17 am
Location: South London

Post by dougal2 » Sat May 05, 2007 4:25 am

you're taking the mean average, per channel of the pixels above, below, to the left and to the right.

Code: Select all

[ ] [A] [ ]
[L] [P] [R]
[ ] [B] [ ]
P = (A + B + L + R) / 4;

Median average means to do the following:
pixels = Array(A, B, L, R)
sort(pixels);
P = pixels[count(pixels)/2]; //this wouldn't work in yopur case because count(pixels) is even - you should take an odd number of samples instead.

Post Reply
19 posts

Who is online

Users browsing this forum: No registered users and 16 guests