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%)
-----


No ja obecnie zrobiłem to chyba prościej:

  1. <?php
  2. class avgColor {
  3. var $resampledSize=300; //Img scaling to X pixels [keeps aspect ratio]
  4. var $samplingAvg=10; //Number of results to averaging
  5. private $gdResImg; //Loaded GD2 resource image
  6. private $imgResY; //Img height [axis Y]
  7. private $imgResX; //Img width [axis X]
  8. var $colorMap; //Table of color in image
  9.  
  10. private function resizeResource() {
  11. $max = $this->resampledSize;
  12. $ratio = $this->imgResX/$this->imgResY;
  13. $newX=$newY=$max;
  14. if ($ratio < 1) $newX = $max*$ratio; else $newY = $max/$ratio; //Cares about ratio
  15. $tmpRes = imagecreatetruecolor($newX, $newY);
  16. imagecopyresampled($tmpRes, $this->gdResImg, 0, 0, 0, 0, $newX, $newY, $this->imgResX, $this->imgResY);
  17. $this->imgRes = $tmpRes;
  18. $this->imgResX = $newX;
  19. $this->imgResY = $newY;
  20. unset($tmpRes);
  21. }
  22.  
  23. function __construct($imgPath) {
  24. if(!is_file($imgPath) || !is_readable($imgPath)) {
  25. trigger_error("Cannot open $imgPath", E_USER_ERROR);
  26. return false;
  27. }
  28.  
  29. $imgInfo = getimagesize($imgPath);
  30. $this->imgResY = $imgInfo[1];
  31. $this->imgResX = $imgInfo[0];
  32.  
  33. switch($imgInfo["mime"]) {
  34. case "image/jpeg": $this->gdResImg = imagecreatefromjpeg($imgPath); break;
  35. case "image/png": $this->gdResImg = imagecreatefrompng($imgPath); break;
  36. case "image/gif": $this->gdResImg = imagecreatefromgif($imgPath); break;
  37. case "image/jpeg": $this->gdResImg = imagecreatefromjpeg($imgPath); break;
  38. }
  39.  
  40. $this->resizeResource(); //Resize res for sampling
  41. }
  42.  
  43. private function trcArr(&$arr, $elements=10) { //truncate array to specified nums of elements
  44. $i=1;
  45. foreach($arr as $key=>$val) {
  46. if($i<=$elements) $ret[$key] = $val;
  47. $i++;
  48. }
  49. $arr=$ret;
  50. }
  51.  
  52. private function mapColors($force=false) { //Make colors map for image; 1st param is force rerun function even if the map is already done
  53. if(!empty($this->colorMap)&&!$force) return; //If map is already done do nothing
  54.  
  55. $this->colorMap=array();
  56. for ($x=0;$x<$this->imgResX;$x++) { //Loop for "rows"
  57. for ($y=0;$y<$this->imgResY;$y++) { //Loop for every pixel in "row"
  58. $rgb = imagecolorat($this->gdResImg, $x, $y);
  59. @$this->colorMap[(($rgb >> 16) & 0xFF)."-".(($rgb >> 8) & 0xFF)."-".($rgb & 0xFF)]++; //Hide notices about index
  60. }
  61. }
  62.  
  63. arsort($this->colorMap); //Sort arr
  64. $this->trcArr($this->colorMap, $this->samplingAvg); //Strip array
  65. }
  66.  
  67.  
  68.  
  69. private function avgColor() { //Better method for avg color
  70. $this->mapColors(); //Make map w/o force
  71. $color=array("r"=>0,"g"=>0,"b"=>0,"rgb"=>"fff");
  72.  
  73. foreach($this->colorMap as $key=>$val) { //Sum colors
  74. $tmp = explode("-", $key); //Extract from R-G-B notation
  75. $color["r"] += $tmp[0]; //Red
  76. $color["g"] += $tmp[1]; //Green
  77. $color["b"] += $tmp[2]; //Blue
  78. }
  79.  
  80.  
  81. $color["r"] = $color["r"]/$this->samplingAvg;
  82. $color["g"] = $color["g"]/$this->samplingAvg;
  83. $color["b"] = $color["b"]/$this->samplingAvg;
  84. $color["rgb"] = dechex($color["r"]).dechex($color["g"]).dechex($color["b"]);
  85.  
  86. return $color;
  87. }
  88.  
  89. function get() {
  90. return $this->avgColor();
  91. }
  92.  
  93. }
  94. ?>
  95.  
  96. /************/
  97. <?php
  98. /********************************************************************************
    */
  99. function _st() {
  100. $mtime = explode(" ", microtime());
  101. return $mtime[1] + $mtime[0];
  102. }
  103.  
  104. function _et($_st) {
  105. $mtime = explode(" ",microtime());
  106. $execT = (($mtime[1] + $mtime[0]) - $_st);
  107. echo "\n\n".str_repeat("-", 40)."\nExec time: ".(($execT<1)?($execT*1000)."ms":$execT."s")."\n\n";
  108. }
  109. /********************************************************************************
    */
  110.  
  111. ini_set('display_errors', '1');
  112.  
  113. require_once("sim.class.php");
  114. $_sim = new avgColor("imgs/647636.jpg");
  115.  
  116. $_st=_st();
  117. var_dump($_sim->get());
  118. _et($_st);
  119. ?>


I obrazek 12Mpx robi w 15ms. Pokaż obrazek abyśmy testowali na tym samym (IMG:style_emoticons/default/winksmiley.jpg)
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 8.10.2025 - 03:18