Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Analiza zdjęcia pod wzgledem kolorystyki
mhs
post
Post #1





Grupa: Zarejestrowani
Postów: 764
Pomógł: 3
Dołączył: 30.04.2003

Ostrzeżenie: (0%)
-----


Witam,

chciałbym przygotować mechanizm, który automatycznie poddajcie analizie określone zdjęcia i następnie generuje dla niego 5-6 podstawowych kolorów które najtrafniej określają kolorystykę dane zdjęcia.

Coś podobne załóżmy jak tutaj (http://freszki.pl) np http://freszki.pl/freszka/548,videoegg czy http://www.dreamstime.com/royalty-free-sto...rs-image9781689 (dominant colors) czyli dla zdjęcia określone są podstawowe kolory i na bazie przypisanych tych wszystkich kolorów możliwe jest przygotowanie wyszukiwarki która znajdzie określone zdjęcia w podobnej kolorystyce.

Spotkał się ktoś z Was z takimi mechanizmami (jakimiś gotowymi bibliotekami) i ma pomysł jak "szybko" i sprawnie coś takiego przygotować?

Pozdrawiam, mhs.

Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
kiler129
post
Post #2





Grupa: Zarejestrowani
Postów: 566
Pomógł: 35
Dołączył: 21.06.2006

Ostrzeżenie: (0%)
-----


/** JESLI SZUKASZ ROZWIAZANIA UZYJ KODU Z KONCA TEGO POSTU **/

Problem (chyba) rozwiązany.
Godzinę wikipedii później i pisania na stackoverflow mam taki oto kod:

  1. <?php
  2. function showColor($c1, $c2, $c3) {
  3. $hex = rgb2hex($c1,$c2,$c3);
  4. echo '<div style="background-color:#'.$hex.'; height:30px; display:inline;">'.str_repeat(" ", 25).'</div> <b>#'.$hex.'</b> (<b><font color="red">R:</font></b> '.$c1.' | <b><font color="green">G:</font></b> '.$c2.' | <b><font color="blue">B:</font></b> '.$c3.')<br><br>';
  5. }
  6.  
  7. function rgb2hex($r,$g,$b) {
  8. $colArr=array("r"=>$r,"g"=>$g,"b"=>$b);
  9. return (($colArr["r"]<16)?"0".dechex($colArr["r"]):dechex($colArr["r"])).(($colArr["g"]<16)?"0".dechex($colArr["g"]):dechex($colArr["g"])).(($colArr["b"]<16)?"0".dechex($colArr["b"]):dechex($colArr["b"]));
  10. }
  11.  
  12. function rgb2lab($r,$g,$b) {
  13. $rgb = array($r,$g,$b);
  14. $eps = 216/24389; $k = 24389/27;
  15. $xr = 0.964221; $yr = 1.0; $zr = 0.825211;
  16. $rgb[0] = $rgb[0]/255;
  17. $rgb[1] = $rgb[1]/255;
  18. $rgb[2] = $rgb[2]/255;
  19. $rgb[0] = ($rgb[0] <= 0.04045)?($rgb[0]/12.92):pow(($rgb[0]+0.055)/1.055,2.4);
  20. $rgb[1] = ($rgb[1] <= 0.04045)?($rgb[1]/12.92):pow(($rgb[1]+0.055)/1.055,2.4);
  21. $rgb[2] = ($rgb[2] <= 0.04045)?($rgb[2]/12.92):pow(($rgb[2]+0.055)/1.055,2.4);
  22. $x = 0.4360747*$rgb[0] + 0.3850649*$rgb[1] + 0.1430804 *$rgb[2];
  23. $y = 0.2225045*$rgb[0] + 0.7168786*$rgb[1] + 0.0606169 *$rgb[2];
  24. $z = 0.0139322*$rgb[0] + 0.0971045*$rgb[1] + 0.7141733 *$rgb[2];
  25. $xr = $x/$xr; $yr = $y/$yr; $zr = $z/$zr;
  26. $fx = ($xr > $eps)?pow($xr, 1/3):($fx = ($k * $xr + 16) / 116);
  27. $fy = ($yr > $eps)?pow($yr, 1/3):($fy = ($k * $yr + 16) / 116);
  28. $fz = ($zr > $eps)?pow($zr, 1/3):($fz = ($k * $zr + 16) / 116);
  29. $lab = array();
  30. $lab[] = round(( 116 * $fy ) - 16);
  31. $lab[] = round(500*($fx-$fy));
  32. $lab[] = round(200*($fy-$fz));
  33. return $lab;
  34. }
  35.  
  36. function deltaE($lab1, $lab2) {
  37. $l = 1; $c = 1;
  38. $c1 = sqrt($lab1[1]*$lab1[1]+$lab1[2]*$lab1[2]);
  39. $c2 = sqrt($lab2[1]*$lab2[1]+$lab2[2]*$lab2[2]);
  40. $h1 = (((180000000/M_PI)*atan2($lab1[1],$lab1[2])+360000000)%360000000)/1000000;
  41. $t = (164<=$h1 AND $h1<=345)?(0.56+abs(0.2*cos($h1+168))):(0.36+abs(0.4*cos($h1+35)));
  42. $f = sqrt(pow($c1,4)/(pow($c1,4)+1900));
  43. $sl = ($lab1[0]<16)?(0.511):((0.040975*$lab1[0])/(1+0.01765*$lab1[0]));
  44. $sc = (0.0638*$c1)/(1+0.0131*$c1)+0.638;
  45. $sh = $sc*($f*$t+1-$f);
  46. return sqrt(pow(($lab1[0]-$lab2[0])/($l*$sl),2)+pow(($c1-$c2)/($c*$sc),2)+pow(sqrt(($lab1[1]-$lab2[1])*($lab1[1]-$lab2[1])+($lab1[2]-$lab2[2])*($lab1[2]-$lab2[2])+($c1-$c2)*($c1-$c2))/$sh,2));
  47. }
  48.  
  49. $c1 = array(30,255,123);
  50. $c2 = array(55,250,100);
  51.  
  52. showColor($c1[0], $c1[1], $c1[2]);
  53. showColor($c2[0], $c2[1], $c2[2]);
  54.  
  55. $cl1 = rgb2lab($c1[0], $c1[1], $c1[2]);
  56. $cl2 = rgb2lab($c2[0], $c2[1], $c2[2]);
  57.  
  58. echo "<pre>";
  59. var_dump(deltaE($cl1, $cl2));
  60. ?>


Obliczy różnicę między dwoma kolorami w przestrzeni L A B - inaczej się ponoć tego dobrze nie da zrobić. Teraz tylko spiąć to z kolorami pixeli zdjęć i gotowe.

Edit
Post jest za dlugi bla bla bla: http://wklej.org/id/438798/ - poddaje sie

Edit
Ok, udało się porównać kolory, dodanie tego do fotek to już nie problem.
Kod klasy:
  1. <?php
  2. class compareColors {
  3. var $tolerance = 3; //tolerance in %
  4.  
  5. private function hex2rgb($hex) { //Convert from #FFFFFF to array(0,0,0) in eg.
  6. $hex = str_replace('#', '', $hex); //Remove #
  7. if(strlen($hex) != 6){ return array(0,0,0); }
  8. return array(
  9. hexdec(substr($hex, 0, 2)),
  10. hexdec(substr($hex, 2, 2)),
  11. hexdec(substr($hex, 4, 2))
  12. );
  13. }
  14.  
  15.  
  16. private function rgb2cmyk($rgb) { //Simple conversion from RGB namespace to CMYK namespace. (warning! colors can look diff btw rgb and cmyk, its ok for comparing but not for real conversion!)
  17. $K = min(array("r"=>(1-$rgb[0]), "g"=>(1-$rgb[1]), "b"=>(1-$rgb[2])));
  18. $C = @((1-$rgb[0]-$K)/(1-$K));
  19. $M = @((1-$rgb[1]-$K)/(1-$K));
  20. $Y = @((1-$rgb[2]-$K)/(1-$K));
  21.  
  22. return(array("C"=>$C, "M"=>$M, "Y"=>$Y, "K"=>$K));
  23. }
  24.  
  25.  
  26. function compareCMYKColors($cmyk1, $cmyk2, $returnScale=false) { //Compare 2 CMYK colors with given tolerance, it also can return "tolerane" of matching instead of bool
  27. $tolerance = ($this->tolerance/100); //Convert from % to float
  28. $diff=array();
  29. $diff[] = abs($cmyk1["C"]-$cmyk2["C"]);
  30. $diff[] = abs($cmyk1["M"]-$cmyk2["M"]);
  31. $diff[] = abs($cmyk1["Y"]-$cmyk2["Y"]);
  32. asort($diff);
  33. reset($diff);
  34. $max1 = each($diff);
  35. $max2 = each($diff);
  36.  
  37. if($returnScale) return ((($max1+$max2)/2)*100); //Convert scale to %
  38. return ($max1["value"]<=$tolerance&&$max2["value"]<=$tolerance) ? true:false;
  39. }
  40.  
  41.  
  42. function compareRGBColors($rgb1, $rgb2, $returnScale=false) { //Alias to above but converts from RGB to CMYK first
  43. return $this->compareCMYKColors($this->rgb2cmyk($rgb1), $this->rgb2cmyk($rgb2), $returnScale);
  44. }
  45.  
  46.  
  47. function compareHEXColors($hex1, $hex2, $returnScale=false) { //Alias to above
  48. return $this->compareRGBColors($this->hex2rgb($hex1), $this->hex2rgb($hex2), $returnScale);
  49. }
  50.  
  51. function bathCompare($base, $colors, $format="hex") {
  52. $out=array();
  53. if($format=="hex") { foreach($colors as $color) { if($this->compareHEXColors($base, $color)) $out[] = $color; } return $out; }
  54. if($format=="rgb") { foreach($colors as $color) { if($this->compareRGBColors($base, $color)) $out[] = $color; } return $out; }
  55. if($format=="cmyk") { foreach($colors as $color) { if($this->compareCMYKColors($base, $color)) $out[] = $color; } return $out; }
  56.  
  57. return false;
  58. }
  59.  
  60. }
  61. ?>


Kod testowy [+ dane testowe]: http://wklej.org/id/439719/

Ten post edytował kiler129 17.12.2010, 16:34:02
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 9.10.2025 - 13:17