问题描述
我正在使用PHP和MySQL构建一个图像厨房,我想通过颜色实现图像搜索。通过遵循,我得到了最多的图像颜色。 <?php
$ image = new Imagick(thing.png);
$ pixels = $ image-> getImageHistogram();
foreach($ pixels as $ p){
$ colors = $ p-> getColor();
foreach($ colors as $ c){
print($ c\t);
}
print(\t:\t。$ p-> getColorCount()。\\\
);
}
?>
这将打印出如下:
红色绿色蓝色Alpha:没有出现
252 250 252 1:125
194 156 182 1 :126
109 18 79 1:11440
2 117 162 1:12761
255 255 255 1:40769
尽管我已经完成了颜色,但我坚持设计数据库以将颜色信息和图像路径存储在数据库中。
我的问题是如何设计数据库(表结构)来存储这种数据,搜索可以查询可以有效地应用。
更新:
其次,如何获取匹配颜色的图像。假设用户正在搜索一个颜色#ff0000,那么如何从数据库中获取所有最近匹配的图像。
谢谢
你应该规范化这个。
3表:
Image {image_id,name}
颜色{color_id,red,green,blue,alpha}
ImageHasColor {image_id,color_id,number_of_times_appeared}
插入数据应该很简单,使用 ... insert_id
选择与以下连接:
SELECT * FROM
Image i
JOIN ImageHasColors h
ON i.image_id = h.image_id
JOIN Colors c
ON c.color_id = h.color_id
ORDER BY i.image_id
查看有关如何转换HEX的此链接颜色到RGB值:
搜索前10个真正的红色图片:
SELECT * FROM
Image i
JOIN ImageHasColors h
ON i.image_id = h.image_id
JOIN颜色c
ON c.color_id = h.color_id
WHERE c.red> 200
AND c.green< 50
AND c。绿色50
ORDER BY h.number_of_times_appeared
LIMIT 10
搜索相当黑色的图片:
SELECT * FROM
Image i
JOIN ImageHasColors h
ON i.image_id = h .image_id
JOIN颜色c
ON c.color_id = h.color_id
WHERE c.red< 30
AND c.green< 30
AND c。绿色30
ORDER BY h.number_of_times_appeared
LIMIT 10
I am building a image galley using PHP and MySQL where I want to implement Image search by it's color. By following Imagick::getImageHistogram i got the most presented colors from the images.
<?php
$image = new Imagick("thing.png");
$pixels=$image->getImageHistogram();
foreach($pixels as $p){
$colors = $p->getColor();
foreach($colors as $c){
print( "$c\t" );
}
print( "\t:\t" . $p->getColorCount() . "\n" );
}
?>
This will print out something like:
Red Green Blue Alpha : No of times appeared
252 250 252 1 : 125
194 156 182 1 : 126
109 18 79 1 : 11440
2 117 162 1 : 12761
255 255 255 1 : 40769
Although I am done with getting the colors, I am stuck with designing the database to store the color information along with image path in the database.
My question is how to design a database (table structure) to store this kind of data where search can query can be applied in an effective manner.
Update:
Secondly how can I get the images with a matching color. Let's say user is searching for a color #ff0000, then how can I get all the nearest matched images from the database.
Thank You
You should normalize this.
3 Tables:
Image {image_id, name}
Colors {color_id, red, green, blue, alpha}
ImageHasColor {image_id, color_id, number_of_times_appeared}
Inserting data should be simple, use ...insert_id
functions to get the id from the row you just inserted.
Select with joins like:
SELECT * FROM
Image i
JOIN ImageHasColors h
ON i.image_id = h.image_id
JOIN Colors c
ON c.color_id = h.color_id
ORDER BY i.image_id
Check this link on how to convert HEX color to RGB values: http://bavotasan.com/2011/convert-hex-color-to-rgb-using-php/
Search top 10 really red pictures:
SELECT * FROM
Image i
JOIN ImageHasColors h
ON i.image_id = h.image_id
JOIN Colors c
ON c.color_id = h.color_id
WHERE c.red > 200
AND c.green < 50
AND c. green < 50
ORDER BY h.number_of_times_appeared
LIMIT 10
Search rather black pictures:
SELECT * FROM
Image i
JOIN ImageHasColors h
ON i.image_id = h.image_id
JOIN Colors c
ON c.color_id = h.color_id
WHERE c.red < 30
AND c.green < 30
AND c. green < 30
ORDER BY h.number_of_times_appeared
LIMIT 10
这篇关于数据库设计,用于存储图像颜色模式,用于搜索图像颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!