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

前端 未结 3 886
时光说笑
时光说笑 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
    

提交回复
热议问题