Problem description

There is a project, basically is to judge an article in the picture is not obtrusive.

Material preparation

So I took 4 random pictures from the Internet:As you can see,The first 3 images should be similar in color and reading experience, while the last one should be different.

When we only scale the image (to run fast) and then use the BGR channel to generate the histogram to calculate the similarity:Only to find that,Only the similarity between the first picture and the second picture is greater than 0.5, while the similarity between the second, third, and third and fourth pictures is almost all less than or equal to 0.1.

Thinking method

Therefore, after reflection, I think that the essence of judging the color difference between two pictures is to judge the shape of their histogram distribution is not similar, and should not consider whether they are left or right, light or dark. One image is a little brighter, but they’re similar.

Based on this idea, I first violently equalized the three channels, BGR and HLS, to verify the feasibility of judging the distribution shape. But at the same time, it is found that the independent equalization will lead to the reduction of the ability to distinguish different images. Therefore, it can be inferred that it is necessary to flatten and equalize the brightness, and at the same time affect the change of other channels associated with it.

So, what came up was this:

  1. First, scale the image to a uniform size to increase the speed of operation.
  2. The image was transferred from BGR channel to HSV channel (through experiments, HSV channel is better than HLS channel).
  3. Equalize the V(lightness) in HSV.
  4. Then the image is transferred from HSV channel back to BGR channel so as to achieve the purpose of affecting other channels while equalizing brightness.
  5. Finally, BGR channel is used to calculate the similarity, and the similarity greater than 0.5 can be considered as similar.

The test results

You can see,After processing, the similarity between the first and second pictures, as well as between the second and third pictures, was greater than 0.7, while the similarity between the third and fourth pictures was only about 0.4. We have achieved what we set out to do.


Deficiency in

  1. Only the equilibrium of V channel was explored, and the possible associations of other channels were not studied.
  2. The similarity of the third and fourth pictures after processing is a little high, so we need to find ways to reduce it.

code

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt


def create_rgb_hist(image) :
    """" Create RGB three-channel histogram (histogram matrix) """
    h, w, c = image.shape
    Create an initial matrix (16*16*16,1) as the histogram matrix
    Number 16*16*16 means that there are 16 bins of three channels per channel
    rgbhist = np.zeros([16 * 16 * 16.1], np.float32)
    bsize = 256 / 16
    for row in range(h):
        for col in range(w):
            b = image[row, col, 0]
            g = image[row, col, 1]
            r = image[row, col, 2]
            Construct an artificial index of the histogram matrix based on the three-channel values of each pixel
            index = int(b / bsize) * 16 * 16 + int(g / bsize) * 16 + int(r / bsize)
            # The matrix formed here is the histogram matrix
            rgbhist[int(index), 0] + =1
    plt.ylim([0.10000])
    plt.grid(color='r', linestyle=The '-', linewidth=0.5, alpha=0.3)
    return rgbhist


def hist_compare(hist1, hist2) :
    Histogram comparison function
    # create an RGB three-channel histogram of the first image (histogram matrix) hist1 = create_rGB_hist (image1) # create an RGB three-channel histogram of the second image (histogram matrix) HIST2 = create_rgb_hist(image2)'''
    # Perform histogram comparison in three ways
    match1 = cv.compareHist(hist1, hist2, cv.HISTCMP_BHATTACHARYYA)
    match2 = cv.compareHist(hist1, hist2, cv.HISTCMP_CORREL)
    match3 = cv.compareHist(hist1, hist2, cv.HISTCMP_CHISQR)
    print("Pasteur distance: %s, correlation: %s, Chi square: %s" % (match1, match2, match3))


def handle_img(img) :
    img = cv.resize(img, (100.100))
    img = cv.cvtColor(img, cv.COLOR_BGR2HSV)
    img[:, :, 2] = cv.equalizeHist(img[:, :, 2])
    img = cv.cvtColor(img, cv.COLOR_HSV2BGR)
    return img


img1 = cv.imread("1.jpg")
img1 = handle_img(img1)
cv.imshow("img1", img1)

img2 = cv.imread("2.jpg")
img2 = handle_img(img2)
cv.imshow("img2", img2)

img3 = cv.imread("3.jpg")
img3 = handle_img(img3)
cv.imshow("img3", img3)

img4 = cv.imread("4.jpg")
img4 = handle_img(img4)
cv.imshow("img4". img4) hist1 = create_rgb_hist(img1) hist2 = create_rgb_hist(img2) hist3 = create_rgb_hist(img3) hist4 = create_rgb_hist(img4) plt.subplot(1.4.1)
plt.title("hist1")
plt.plot(hist1)
plt.subplot(1.4.2)
plt.title("hist2")
plt.plot(hist2)
plt.subplot(1.4.3)
plt.title("hist3")
plt.plot(hist3)
plt.subplot(1.4.4)
plt.title("hist4")
plt.plot(hist4)

hist_compare(hist1, hist2)
hist_compare(hist2, hist3)
hist_compare(hist3, hist4)

plt.show()

cv.waitKey(0)
cv.destroyAllWindows()
Copy the code