示例#1
0
def hist_compare(image1, image2):
    size = image1.shape[:2]
    image1 = cv.resize(image1, size)
    image2 = cv.resize(image2, size)
    image1 = cv.cvtColor(image1, cv.COLOR_BGR2GRAY)
    image2 = cv.cvtColor(image2, cv.COLOR_BGR2GRAY)
    # image1 = cv.equalizeHist(image1)
    # image2 = cv.equalizeHist(image2)
    hist1 = cv.calcHist([image1], [0], None, [256], [0, 256])
    hist2 = cv.calcHist([image2], [0], None, [256], [0, 256])
    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("巴氏距离", match1)
    print("相关性", match2)
    print("卡方", match3)
    # 直方图绘制
    # 2-2-1
    plt.subplot(221)
    plt.imshow(image1, cmap=plt.cm.gray)
    # 2-2-3
    plt.subplot(223)
    plt.imshow(image2, cmap=plt.cm.gray)
    # 2-2-2
    plt.subplot(222)
    plt.plot(hist1)  # 线
    # 2-2-4
    plt.subplot(224)
    plt.plot(hist2)  # 线
    plt.show()
示例#2
0
def hist_compare(image1, image2):
    hist1 = creat_rgb_hist(image1)
    hist2 = creat_rgb_hist(image2)
    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("巴氏:%s,相关性:%s,卡方:%s" % (match1, match2, match3))
示例#3
0
def calc_difference(hist_1, hist_2):
    img_hist_diff = cv2.compareHist(hist_1, hist_2, cv2.HISTCMP_BHATTACHARYYA)
    img_template_probability_match = cv2.matchTemplate(
        hist_1, hist_2, cv2.TM_CCOEFF_NORMED)[0][0]
    img_template_diff = 1 - img_template_probability_match

    # taking only 10% of histogram diff, since it's less accurate than template method
    commutative_image_diff = (img_hist_diff / 10) + img_template_diff
    logging.debug("calculated image difference %f", commutative_image_diff)
    return commutative_image_diff
示例#4
0
def hist_diff(img_1, img_2):
    """ Calculates distance between images based on the distance between their
    histograms. Example found at:
    https://www.pyimagesearch.com/2014/07/14/3-ways-compare-histograms-using-opencv-python/
    """

    hist_1 = cv2.calcHist([img_1], [0, 1, 2], None, [8, 8, 8],
                          [0, 256, 0, 256, 0, 256])
    hist_1 = cv2.normalize(hist_1, hist_1).flatten()
    hist_2 = cv2.calcHist([img_2], [0, 1, 2], None, [8, 8, 8],
                          [0, 256, 0, 256, 0, 256])
    hist_2 = cv2.normalize(hist_2, hist_2).flatten()

    return cv2.compareHist(hist_1, hist_2, cv2.HISTCMP_BHATTACHARYYA)
def compare(img_source, img_reference):
    hist_requete = cv.calcHist([img_requete], [0], None, [256], [0, 256])
    hist_ref = cv.calcHist([img_reference], [0], None, [256], [0, 256])
    current_matching_value = cv.compareHist(hist_requete, hist_ref,
                                            cv.HISTCMP_CORREL)
    return current_matching_value
示例#6
0
            break
        for r_x in range(0, width, slide):
            sys.stdout.write("\r load_n : %d" % read_num)
            sys.stdout.flush()
            if r_x + size >= width:
                break
            dst = src[r_y:r_y + size, r_x:r_x + size]  #image
            resize_img = cv2.resize(dst, (resize_shape))

            X.append(resize_img)  #image_list

            r.append([r_y, r_x])  #coordinate_list

            resize_img = resize_img * 255.0
            img_hist = cv2.calcHist([resize_img], [0], None, [256], [0, 256])
            ret = cv2.compareHist(white_hist, img_hist, 0)
            ret_list.append(ret)  #background_list

            read_num += 1

        X = np.asarray(X)
        predicted_value = model.predict(X, verbose=0)

        for nth, r_n in enumerate(r):
            if ret_list[nth] < 1.0:
                predicted_keeper[r_n[0]:r_n[0] + size,
                                 r_n[1]:r_n[1] + size] += predicted_value[nth]
                num_inferences[r_n[0]:r_n[0] + size, r_n[1]:r_n[1] + size] += 1

    for colormap_y in range(height):
        for colormap_x in range(width):
示例#7
0
def classifyImageByHist(imagePath, datasetHists, showStep=False):
    imageName = os.path.basename(imagePath)
    imageBGR = cv2.imread(imagePath)

    imageFeatureVector = imageBGR2FeatureVector(imageBGR)
    imageHist = imageBGR2FlatHist(imageBGR)

    result = []
    resultLastIndex = 0

    if showStep:
        for key, item in datasetHists.items():
            for i in range(len(datasetHists[key])):
                imageHistFormatted = []
                print("\n" + key + "[" + str(i) + "]: ")
                for j in range(len(datasetHists[key][i])):
                    imageHistFormatted.append('{0:.5f}'.format(
                        datasetHists[key][i][j]))

                for n in range(len(imageHistFormatted)):
                    # if n % 8 == 0:
                    # print('')

                    print(imageHistFormatted[n], end=' ')

                    if n == len(imageHistFormatted) - 1:
                        print()

        print("\nuji:")
        for n in range(len(imageHist)):
            print('{0:.5f}'.format(imageHist[n]), end=' ')

            if n == len(imageHist) - 1:
                print()

        for key, item in datasetHists.items():
            for i in range(len(datasetHists[key])):
                imageHistFormatted = []
                print("\nkomparasi uji dengan " + key + "[" + str(i) + "]: ")
                for j in range(len(datasetHists[key][i])):
                    buffer = min(imageHist[j], datasetHists[key][i][j])

                    imageHistFormatted.append('{0:.5f}'.format(buffer))

                for n in range(len(imageHistFormatted)):
                    print(imageHistFormatted[n], end=' ')

                    if n == len(imageHistFormatted) - 1:
                        print()

                imageHistFormatted = np.array(imageHistFormatted).astype(
                    np.float)
                result.append((key, sum(imageHistFormatted)))

                # print("hasil sum: %f" % (result[resultLastIndex][1]))
                resultLastIndex += 1
    else:
        for key, value in datasetHists.items():
            for i in range(6):
                imageHistDataset = value[i]

                histValue = '{0:.5f}'.format(
                    cv2.compareHist(imageHist, imageHistDataset,
                                    cv2.HISTCMP_INTERSECT))
                result.append((key, float(histValue)))

                # print(str(key) + "[" + str(i) + "]: " + str(result[resultLastIndex][1]))
                resultLastIndex += 1

    print(
        "[ K O M P A R A S I   G A M B A R   %s   D E N G A N   D A T A S E T ]"
        % (imageName))
    result.sort(key=lambda tup: tup[1], reverse=True)
    for r in result:
        print(r)

    classes = []
    for key, value in datasetHists.items():
        classes.append((key, 0))

    k = 5
    print()
    print("[ P E M B E R I A N   R A N K I N G   D E N G A N   K   =   %d ]" %
          (k))
    for i in range(len(classes)):
        for j in range(k):
            if result[j][0] == classes[i][0]:
                classes[i] = (classes[i][0], classes[i][1] + 1)

    classes.sort(key=lambda tup: tup[1], reverse=True)
    for c in classes:
        print(c)

    print()
    print("[ H A S I L   K L A S I F I K A S I ]")
    print("Kelas gambar %s adalah %s" % (imageName, classes[0][0]))
    print(
        "________________________________________________________________________________\n"
    )
示例#8
0
def get_match_rate(source, area):
    """" Retrieve a similitude rate between a source image and a template """
    hist_source = cv.calcHist([source], [0], None, [256], [0, 256])
    hist_area = cv.calcHist([area], [0], None, [256], [0, 256])
    return cv.compareHist(hist_source, hist_area, cv.HISTCMP_CORREL)