根据调色板查找最佳图像相似度

时间:2017-02-15 22:47:04

标签: php mysql

我有这个小脚本用于演示 http://www.predajdielo.sk/tmp.similar.php?source_image=1 (您可以通过source_image $ _GET参数从1到10更改源图像)

我有数千张图片的数据库。每个图像都有预先计算的调色板,以数据库中的格式999999,666666,333333,cccccc,666633,000000,...等格式存储。每个托盘包含16种颜色的信息,此调色板按照图片中最常用的颜色排序。

根据源图像通过数据库查询找到最密切相关的图片的方法是什么?我的解决方案不是很有效:(

SELECT
 item.*,
 MATCH (item.image_digital_hash) AGAINST ('+996666 +999999 +cc9999 +996633 +666699 +666666 +669999 +663333 +999966 +330000 +666633 +ffffff +9999cc +663300 +333333 +000000') AS score
FROM item
INNER JOIN user ON item.user_id = user.id
WHERE
 user.account_status = 'ok' AND
 item.status = 'ok' AND
 item.categorization LIKE '%%umelecka-tvorba,obrazy%%' AND 
 item.image_digital_hash IS NOT NULL AND
 item.image_digital_hash LIKE '996666%%' AND
 MATCH (item.image_digital_hash) AGAINST ('+996666 +999999 +cc9999 +996633 +666699 +666666 +669999 +663333 +999966 +330000 +666633 +ffffff +9999cc +663300 +333333 +000000')
ORDER BY score DESC
LIMIT 100

感谢。

1 个答案:

答案 0 :(得分:0)

好的,我一直在思考这个问题,如何根据使用源图像的调色板找到尽可能多的相关图像。

答案并不那么难。这是关于逻辑:) 首先..每种颜色都有自己的位置,所以我们使用MySql LOCATE函数来查找相对于源颜色代码的位置数。检查颜色代码是否存在,如果不存在,请添加最大可能数字,即7 * 16 = 112.这是我们从源图像中为每种颜色制作的。

然后只需根据检索到的位置的总和对结果进行排序就足够了。结果是具有最接近的调色板的图像。

不太好或不清楚SQL查询,但它工作正常

SELECT
    item.*,
    IF (LOCATE ('996699', item.image_digital_hash) > 0, LOCATE ('996699', item.image_digital_hash), 112) AS tmp_1, 
    IF (LOCATE ('999999', item.image_digital_hash) > 0, LOCATE ('999999', item.image_digital_hash), 112) AS tmp_2, 
    IF (LOCATE ('cccccc', item.image_digital_hash) > 0, LOCATE ('cccccc', item.image_digital_hash), 112) AS tmp_3, 
    IF (LOCATE ('cc99cc', item.image_digital_hash) > 0, LOCATE ('cc99cc', item.image_digital_hash), 112) AS tmp_4, 
    IF (LOCATE ('333333', item.image_digital_hash) > 0, LOCATE ('333333', item.image_digital_hash), 112) AS tmp_5, 
    IF (LOCATE ('666699', item.image_digital_hash) > 0, LOCATE ('666699', item.image_digital_hash), 112) AS tmp_6, 
    IF (LOCATE ('663399', item.image_digital_hash) > 0, LOCATE ('663399', item.image_digital_hash), 112) AS tmp_7, 
    IF (LOCATE ('663366', item.image_digital_hash) > 0, LOCATE ('663366', item.image_digital_hash), 112) AS tmp_8, 
    IF (LOCATE ('9966cc', item.image_digital_hash) > 0, LOCATE ('9966cc', item.image_digital_hash), 112) AS tmp_9, 
    IF (LOCATE ('ffffff', item.image_digital_hash) > 0, LOCATE ('ffffff', item.image_digital_hash), 112) AS tmp_10, 
    IF (LOCATE ('9999cc', item.image_digital_hash) > 0, LOCATE ('9999cc', item.image_digital_hash), 112) AS tmp_11, 
    IF (LOCATE ('cc9999', item.image_digital_hash) > 0, LOCATE ('cc9999', item.image_digital_hash), 112) AS tmp_12, 
    IF (LOCATE ('996666', item.image_digital_hash) > 0, LOCATE ('996666', item.image_digital_hash), 112) AS tmp_13, 
    IF (LOCATE ('666666', item.image_digital_hash) > 0, LOCATE ('666666', item.image_digital_hash), 112) AS tmp_14, 
    IF (LOCATE ('ffcccc', item.image_digital_hash) > 0, LOCATE ('ffcccc', item.image_digital_hash), 112) AS tmp_15, 
    IF (LOCATE ('663333', item.image_digital_hash) > 0, LOCATE ('663333', item.image_digital_hash), 112) AS tmp_16

FROM item

INNER JOIN user ON item.user_id = user.id

WHERE
    user.account_status = 'ok' AND
    item.status = 'ok' AND
    item.categorization LIKE '%%umelecka-tvorba,obrazy%%' AND
    item.image_digital_hash IS NOT NULL

ORDER BY (tmp_1 + tmp_2 + tmp_3 + tmp_4 + tmp_5 + tmp_6 + tmp_7 + tmp_8 + tmp_9 + tmp_10 + tmp_11 + tmp_12 + tmp_13 + tmp_14 + tmp_15 + tmp_16) ASC

LIMIT 100

此处演示http://www.predajdielo.sk/tmp.similar.done.php?source_image=9 (您可以通过source_image $ _GET参数从1到10更改源图像)