Jump to content

Thresholding an image producing strange values at output.


ocpaul20

Recommended Posts

I am trying to produce a thresholded image with only an rgb value of zero or 255.

 

As you can see, although I only output 0 and 255,

I am finding that if I output as jpg file, I get other values in there as well.

If I output as gif, I also get other values in there too.

 

I am sure I am doing something stupid as there must be a way to do this.

 

My test file is this attached jpg greyscale file.

 

Is there a way to process this test file so that only 0 or 255 appears in the output image please? Thanks.

 

Here is my program

<?php
$url = 'greyscale_image.jpg';
$outfile = 'greyscaleout';
$src = imagecreatefromjpeg($url);
$w = imagesx($src);$h = imagesy($src);
$loband = 80;$hiband = 255;

$im_r = imagecreatetruecolor($w,$h);
imagefill($im_r,0,0,imagecolorallocate($im_r,255,0,0));

$im_j = imagecreatetruecolor($w,$h);
imagefill($im_j,0,0,imagecolorallocate($im_j,255,0,0));

for ($x=0;$x<$w;$x++) {
	for ($y=0;$y<$h;$y++) {
			$idx = imagecolorat($src,$x,$y);
			$rgb = imagecolorsforindex($src,$idx); // this is safest (for gifs) rather than some calculation - apparently
			$r =$rgb['red'];
			$g = $rgb['green'];
			$b = $rgb['blue'];

			$gryout = $r;
			if ($r <= $loband) {
				$gryout = 0; // everything less than= loband is black
			}
			if ($r > $loband) {
				$gryout = 255; // everything else is white
			}
			if ( ($gryout == 0) || ($gryout == 255) ) {
					// only zero or 255 allowed
					$idx = imagecolorallocate($im_r,$gryout,$gryout,$gryout);
					imagesetpixel($im_r,$x,$y,$idx); //
					imagesetpixel($im_j,$x,$y,$idx); //

					$idx2 = imagecolorat($im_r,$x,$y);//
					if ($idx != $idx2) {
						die($idx2." at x,y ".$x.",".$y);
					}
					$rgb = imagecolorsforindex($im_r,$idx); // this is safest (for gifs) rather than some calculation - apparently
					$r =$rgb['red'];
					$g = $rgb['green'];
					$b = $rgb['blue'];
					if ($r != 0) {
						if ($r != 255) {
							die($r." at x,y ".$x.",".$y);
						}
					}
			} else {
				die('<br>Grey going out to image is '.$gryout);
			}
	}
}
imagegif($im_r,$outfile.'.gif');imagedestroy($im_r);
imagejpeg($im_j,$outfile.'.jpg');imagedestroy($im_j);
// ==========================
// done the writing, now check it
// ==========================
// $outfile = 'greyscale_image'; // test using original
$src = imagecreatefromjpeg($outfile.'.jpg');
$w = imagesx($src);$h = imagesy($src);
$errcount = 0;
$x = 0;
while($x<$w) {
	$y = 0;
	while($y<$h) {
			$idx = imagecolorat($src,$x,$y);//
			$rgb = imagecolorsforindex($src,$idx); // this is safest (for gifs) rather than some calculation
			$r =$rgb['red'];
			$g = $rgb['green'];
			$b = $rgb['blue'];

			if ($r != 0) {
				if ($r != 255) {
					echo sprintf('<br>r(%d,%d) r=%d g=%d b=%d',$x,$y,$r,$g,$b);$errcount++;
				}
			}
			if ($g != 0) {
				if ($g != 255) {
					echo sprintf('<br>g(%d,%d) r=%d g=%d b=%d',$x,$y,$r,$g,$b);$errcount++;
				}
			}
			if ($b != 0) {
				if ($b != 255) {
					echo sprintf('<br>b(%d,%d) r=%d g=%d b=%d',$x,$y,$r,$g,$b);$errcount++;
				}
			}
			if ($errcount > 10) {break 2;}

			$y++;
	}
	$x++;
}
if ($errcount == 0) {die( ' Checked OK' );}
die( ' Checked  NOT OK' );
// =================
?>

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.