Database design to store image color pattern in MySQL for searching Image by color

前端 未结 3 850
时光说笑
时光说笑 2021-01-18 19:58

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 mo

相关标签:
3条回答
  • 2021-01-18 20:11

    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
    
    0 讨论(0)
  • 2021-01-18 20:16

    How about you first times all the red values by the number of times they appeared and then add those values together to get the overall red value. Then divide by the total number of times appeared. Do the same for Green and Blue.

    For example:

    total red = ((252*125)+(194*126)+(109*11440)+(2*12761)+(255*40769)) 
                / (125 + 126 + 11440 + 12761 + 40769)
              = 180
    

    Then just store them in the database like

                 id         red      green       blue     image_path
    Image 1      1          225      134         4        /dir/
    Image 2      2          143      0           145      /dir/
    Image 3      3          239      200         111      /dir/
    

    If you were searching for Red images you could do something like:

    SELECT id, image_path WHERE red > 200 AND blue < 100 AND green < 100
    

    Im not sure of the specifics, but you could mess around with the values.

    0 讨论(0)
  • 2021-01-18 20:21

    Why don't you create a database table with the following fields:

    Images Table

    id
    name
    red
    green
    blue
    alpha
    
    0 讨论(0)
提交回复
热议问题