Similarity is a very vague thing. . . If you just want to implement a simple version, I found a very interesting algorithm on stackoverflow:
Reduce the two images to a smaller, same size, such as 64*64 or 32*32. When zooming out, the value of each pixel is a function of the values of the surrounding n pixels. To put it simply, average is used.
Expand the color range so that the darkest color in the picture is completely black and the lightest color is completely white
Rotate/flip the image so that the pixels in the four corners of the image are arranged clockwise in descending order starting from the upper left corner.
Then find the average similarity of each point of the two pictures (the similarity can be calculated by using the difference, the square of the difference, or multiplying it by a weight based on the position. Get some data and run it to select the one with the best effect).
Interestingly, this algorithm can even support pre-computing a batch of images and then querying similar ones from the database:
To look up a possible match in a database, store the pixel colors as inpidual columns in the database, index a bunch of them (but not all, unless you use a very small image), and do a query that uses a range for each pixel value, ie. every image where the pixel in the small image is between -5 and +5 of the image you want to look up.
Perceptual hash algorithm - Perceptual hash algorithm
Simple steps:
Reference link: Principle of similar image search - Ruan Yifeng | Color distribution method and content feature method
Similarity is a very vague thing. . . If you just want to implement a simple version, I found a very interesting algorithm on stackoverflow:
Then find the average similarity of each point of the two pictures (the similarity can be calculated by using the difference, the square of the difference, or multiplying it by a weight based on the position. Get some data and run it to select the one with the best effect).
Interestingly, this algorithm can even support pre-computing a batch of images and then querying similar ones from the database: