我會建議使用mysql函數來比較你的隨機給定的圖像。首先讓我們創建一個簡單的示例表
DROP TABLE IF EXISTS images;
CREATE TABLE images (
id INTEGER AUTO_INCREMENT PRIMARY KEY,
rgb_values VARCHAR(255)
);
現在讓我們定義我們將在我們的查詢中使用的函數。第一個允許使用拆就任何分隔字符串,並通過指數獲得所需的元素回:
DROP FUNCTION SPLIT_STR;
CREATE FUNCTION SPLIT_STR(
x VARCHAR(255),
delim VARCHAR(12),
pos INT
)
RETURNS VARCHAR(255)
RETURN
REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
delim, '')
;
接下來,我們定義一個函數來計算你想這個問題根據您的算法的圖像差(或任何ALGO使用):
DROP FUNCTION IMAGE_DIFF;
CREATE FUNCTION IMAGE_DIFF(
from_val VARCHAR(255),
to_val VARCHAR(255)
)
RETURNS INTEGER(4)
RETURN
ABS((SPLIT_STR(to_val, ',', 1) - SPLIT_STR(from_val, ',',1))) +
ABS((SPLIT_STR(to_val, ',', 2) - SPLIT_STR(from_val, ',',2))) +
ABS((SPLIT_STR(to_val, ',', 3) - SPLIT_STR(from_val, ',',3))) +
ABS((SPLIT_STR(to_val, ',', 4) - SPLIT_STR(from_val, ',',4))) +
ABS((SPLIT_STR(to_val, ',', 5) - SPLIT_STR(from_val, ',',5))) +
ABS((SPLIT_STR(to_val, ',', 6) - SPLIT_STR(from_val, ',',6))) +
ABS((SPLIT_STR(to_val, ',', 7) - SPLIT_STR(from_val, ',',7))) +
ABS((SPLIT_STR(to_val, ',', 8) - SPLIT_STR(from_val, ',',8))) +
ABS((SPLIT_STR(to_val, ',', 9) - SPLIT_STR(from_val, ',',9)))
;
讓我們創建一些示例數據:
INSERT INTO images(rgb_values) VALUES ("237,128,73,69,35,249,199,183,178");
INSERT INTO images(rgb_values) VALUES ("39,212,164,170,202,49,93,77,145");
INSERT INTO images(rgb_values) VALUES ("28,242,83,167,92,161,115,38,108");
INSERT INTO images(rgb_values) VALUES ("72,81,73,2,77,109,177,204,120");
INSERT INTO images(rgb_values) VALUES ("165,149,106,248,39,26,167,237,139");
INSERT INTO images(rgb_values) VALUES ("183,40,156,131,120,19,71,88,69");
INSERT INTO images(rgb_values) VALUES ("138,136,112,36,69,245,130,196,24");
INSERT INTO images(rgb_values) VALUES ("1,194,153,107,16,102,164,154,74");
INSERT INTO images(rgb_values) VALUES ("172,161,17,179,140,244,23,219,115");
INSERT INTO images(rgb_values) VALUES ("166,151,48,62,154,227,44,21,201");
INSERT INTO images(rgb_values) VALUES ("118,73,212,180,150,64,254,177,68");
INSERT INTO images(rgb_values) VALUES ("119,220,226,254,14,175,123,11,134");
INSERT INTO images(rgb_values) VALUES ("118,93,238,31,77,36,105,151,216");
INSERT INTO images(rgb_values) VALUES ("123,108,177,136,9,24,119,175,88");
INSERT INTO images(rgb_values) VALUES ("11,207,12,215,215,80,101,213,143");
INSERT INTO images(rgb_values) VALUES ("132,158,46,188,7,245,241,126,214");
INSERT INTO images(rgb_values) VALUES ("167,238,186,86,109,164,219,199,238");
INSERT INTO images(rgb_values) VALUES ("216,93,139,246,153,39,226,152,143");
INSERT INTO images(rgb_values) VALUES ("98,229,7,203,230,224,57,154,252");
INSERT INTO images(rgb_values) VALUES ("7,95,145,120,35,6,116,240,64");
INSERT INTO images(rgb_values) VALUES ("45,194,172,223,96,168,18,4,215");
INSERT INTO images(rgb_values) VALUES ("243,161,214,235,134,190,207,63,127");
INSERT INTO images(rgb_values) VALUES ("74,189,249,85,148,169,65,3,81");
INSERT INTO images(rgb_values) VALUES ("46,113,191,20,108,139,60,249,6");
INSERT INTO images(rgb_values) VALUES ("153,246,189,175,5,125,9,197,160");
INSERT INTO images(rgb_values) VALUES ("202,248,23,59,81,175,197,180,114");
INSERT INTO images(rgb_values) VALUES ("73,136,252,137,222,197,118,64,69");
INSERT INTO images(rgb_values) VALUES ("172,224,251,32,154,175,201,33,14");
INSERT INTO images(rgb_values) VALUES ("141,126,112,12,45,214,243,127,49");
INSERT INTO images(rgb_values) VALUES ("116,155,23,205,62,235,111,136,205");
,然後使用我們的新定義的函數對要使用比較形象運行一個查詢:
mysql> SELECT id
-> , image_diff(rgb_values, '255,191,234,123,85,23,255,255,255') rgb_diff
-> FROM images
-> ORDER BY 2 DESC;
+----+----------+
| id | rgb_diff |
+----+----------+
| 19 | 1150 |
| 10 | 1148 |
| 3 | 1122 |
| 27 | 1094 |
| 9 | 1070 |
| 15 | 1069 |
| 23 | 1061 |
| 21 | 1059 |
| 7 | 1034 |
| 12 | 1024 |
| 24 | 1022 |
| 30 | 1016 |
| 29 | 989 |
| 28 | 962 |
| 2 | 947 |
| 4 | 933 |
| 16 | 893 |
| 6 | 885 |
| 8 | 875 |
| 20 | 848 |
| 25 | 835 |
| 26 | 815 |
| 1 | 777 |
| 22 | 758 |
| 14 | 745 |
| 11 | 706 |
| 18 | 683 |
| 5 | 656 |
| 13 | 645 |
| 17 | 494 |
+----+----------+
30 rows in set (0.01 sec)
我可以理解在查詢中做這件事的唯一方法是超越醜陋,而且非常保證效率低下。在我看來,這可能是一個合理的查詢,如果這在9個獨立的列。 – therealsix 2011-04-08 03:41:42
是的,這是我第一次想到,我忘了提及它。它看起來迄今爲止是最好的選擇。謝謝 – HappyDeveloper 2011-04-08 04:01:34
查看我剛發佈的答案。所有在服務器上的mysql都完成了。 – Wes 2011-04-08 04:14:26