public function colourCalibrate($colours, $expand = 25, $SD = 2) { // $colours lists x-coordinate, y-coordinate, and L, a* and b* values for each colour on the checker chart // $expand = number of pixels plus or minus the reference x and y-corrdinates // $SD = exclude pixels more than $sd standard deviations from the mean for that area if (!imageistruecolor($this->_image)) { return false; } $w = $this->getWidth(); $h = $this->getHeight(); $rgb255 = array(); foreach ($colours as $cname => $c) { // get color from x and y coordinates $imgcolor = array(); for ($x = $c[0] - $expand; $x <= $c[0] + $expand; $x++) { for ($y = $c[1] - $expand; $y <= $c[1] + $expand; $y++) { $rgb = imagecolorat($this->_image, $x, $y); $imgcolor['r'][] = $rgb >> 16 & 0xff; $imgcolor['g'][] = $rgb >> 8 & 0xff; $imgcolor['b'][] = $rgb & 0xff; } } foreach ($imgcolor as $rgb => $valuearray) { $mode = mode($valuearray, 0.25); $stdev = stdev($valuearray); foreach ($valuearray as $i => $v) { if ($v > $mode + $SD * $stdev || $v < $mode - $SD * $stdev) { // remove from list unset($valuearray[$i]); } } $averages[$rgb] = round(mean($valuearray), 12); $stdevs[$rgb] = round(stdev($valuearray), 4); $counts[$rgb] = count($valuearray); } $rgb255[] = $averages; $lab = rgb2lab($averages['r'], $averages['g'], $averages['b']); $deltaE = deltaE($lab, array($c[2], $c[3], $c[4])); $check[$cname] = sprintf("Lab(%.1f, %.1f, %.1f)\n", round($lab['L'], 1), round($lab['a'], 1), round($lab['b'], 1)); /* $check .= "--myLab: (" . implode(',', $lab['lab']) . ")\n"; $check .= "--chartLab: ($c[2], $c[3], $c[4])\n"; $check .= "--deltaE = $deltaE\n"; $check .= "--RGB: (" . implode(',', $averages) . ")\n"; $check .= "--SD: (" . implode(',', $stdevs) . ")\n"; $check .= "--N: (" . implode(',', $counts) . ")\n"; */ if (!is_nan($deltaE)) { $deltaEs[] = $deltaE; } } $meanDeltaE = round(mean($deltaEs), 4); $maxDeltaE = round(max($deltaEs), 4); $patch = $expand * 2 + 1; $this->setDescription(array("colour calibrate" => array("path dimension" => "{$patch} x {$patch}", "SD" => $SD, "deltaE" => array("M" => $meanDeltaE, "max" => $maxDelta), "check" => $check))); /* MATLAB code ************************************ % Calculate camera Characterisation - Least Squares fit of rgb to Lab patches: rgb = Rgb_vals/255; for i = 1:length(rgb) train(i,:) = rgb2rgbpoly(rgb(i,:),11); end R=train\spectrod65; % Calculate predicted lab values from characterisation lab_calc = train*R; squaredDifference = (lab_calc-spectrod65).^2; meanDeltaE = mean(sqrt(sum(squaredDifference'))) maxDeltaE = max(sqrt(sum(squaredDifference'))) **************************************************/ include_once "Math/Matrix.php"; foreach ($colours as $v) { $spectrod65[] = array($v[2], $v[3], $v[4]); } foreach ($rgb255 as $i => $v) { foreach ($v as $j => $c) { $rgb1[$i][] = $c / 255; } } foreach ($rgb1 as $v) { $train[] = rgb2rgbpoly($v, 11); } $rgbM = new Math_Matrix($rgb1); $trainM = new Math_matrix($train); return $this; }
function image_lab($image) { $width = imagesx($image); // echo "width: " . $width . "-"; $height = imagesy($image); // echo "height: " . $height . "<br/>"; $thumb_width = 4; $thumb_height = 5; $thumb = imagecreatetruecolor($width, $height); // echo $thumb . "<br/>"; imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height); $lab = array(); for ($x = 0; $x < $thumb_width; $x++) { for ($y = 0; $y < $thumb_height; $y++) { $index = imagecolorat($thumb, $x, $y); $rgb = imagecolorsforindex($thumb, $index); $lab[] = rgb2lab(array($rgb['red'], $rgb['green'], $rgb['blue'])); } } return $lab; }
$filecontents = "x,y,L,a,b\n"; if ($_POST['ignore_mask'] == 'true') { $mask_color = imagecolorat($image, 0, 0); $return['ignore_mask'] = $mask_color; } else { $mask_color = -1; $return['ignore_mask'] = $mask_color; } for ($x = 0; $x < $return['width']; $x++) { for ($y = 0; $y < $return['height']; $y++) { $color_index = imagecolorat($image, $x, $y); if ($color_index != $mask_color) { $r = $color_index >> 16 & 0xff; $g = $color_index >> 8 & 0xff; $b = $color_index & 0xff; $lab = rgb2lab($r, $g, $b); $l = round($lab['L'], 4); $a = round($lab['a'], 4); $b = round($lab['b'], 4); $filecontents .= "{$x},{$y},{$l},{$a},{$b}\n"; } } } if (file_exists($filename)) { unlink($filename); } if (!($file = fopen($filename, 'w'))) { $return['errorText'] = "Could not open the file {$return['newFileName']}"; } else { if (!fwrite($file, $filecontents)) { $return['errorText'] = "Could not write to the file {$return['newFileName']}";