簡體   English   中英

比較python中的LBP

[英]Compare the LBP in python

我生成了這樣的紋理圖像

我必須比較兩個紋理。 我用直方圖比較法。

image_file = 'output_ori.png'
img_bgr = cv2.imread(image_file)
height, width, channel = img_bgr.shape

hist_lbp = cv2.calcHist([img_bgr], [0], None, [256], [0, 256])
print("second started")

image_fileNew = 'output_scan.png'
img_bgr_new = cv2.imread(image_fileNew)
height_new, width_new, channel_new = img_bgr_new.shape
print("second lbp")

hist_lbp_new = cv2.calcHist([img_bgr_new], [0], None, [256], [0, 256])

print("compar started")

compare = cv2.compareHist(hist_lbp, hist_lbp_new, cv2.HISTCMP_CORREL)

print(compare)

但這種方法效果不佳。 它顯示了兩種不同圖像紋理的類似結果。 此外,它沒有顯示太多的變化來識別打印和掃描效果。 我如何比較紋理? 我想過分析GLCM特征。

import cv2
import numpy as np
from skimage.feature import greycomatrix

img = cv2.imread('images/noised_img1.jpg', 0)

image = np.array(img, dtype=np.uint8)
g = greycomatrix(image, [1, 2], [0, np.pi/2], levels=4, normed=True, symmetric=True)
contrast = greycoprops(g, 'contrast')
print(contrast)

在這個方法中,我得到輸出為2 * 2矩陣。 如何比較幾個特征的兩個矩陣,如對比度,相似性,同質性,ASM,能量和相關性?

評論澄清

import numpy as np
from PIL import Image

class LBP:
    def __init__(self, input, num_processes, output):
        # Convert the image to grayscale
        self.image = Image.open(input).convert("L")
        self.width = self.image.size[0]
        self.height = self.image.size[1]
        self.patterns = []
        self.num_processes = num_processes
        self.output = output

    def execute(self):
        self._process()
        if self.output:
            self._output()

    def _process(self):
        pixels = list(self.image.getdata())
        pixels = [pixels[i * self.width:(i + 1) * self.width] for i in range(self.height)]

        # Calculate LBP for each non-edge pixel
        for i in range(1, self.height - 1):
            # Cache only the rows we need (within the neighborhood)
            previous_row = pixels[i - 1]
            current_row = pixels[i]
            next_row = pixels[i + 1]

            for j in range(1, self.width - 1):
                # Compare this pixel to its neighbors, starting at the top-left pixel and moving
                # clockwise, and use bit operations to efficiently update the feature vector
                pixel = current_row[j]
                pattern = 0
                pattern = pattern | (1 << 0) if pixel < previous_row[j-1] else pattern
                pattern = pattern | (1 << 1) if pixel < previous_row[j] else pattern
                pattern = pattern | (1 << 2) if pixel < previous_row[j+1] else pattern
                pattern = pattern | (1 << 3) if pixel < current_row[j+1] else pattern
                pattern = pattern | (1 << 4) if pixel < next_row[j+1] else pattern
                pattern = pattern | (1 << 5) if pixel < next_row[j] else pattern
                pattern = pattern | (1 << 6) if pixel < next_row[j-1] else pattern
                pattern = pattern | (1 << 7) if pixel < current_row[j-1] else pattern
                self.patterns.append(pattern)

    def _output(self):
        # Write the result to an image file
        result_image = Image.new(self.image.mode, (self.width - 2, self.height - 2))
        result_image.putdata(self.patterns)
        result_image.save("output.png")

我用這段代碼生成了紋理。 我有紋理,我有計算紋理屬性的方法,但問題是如何識別兩個紋理之間的相似性。

假設您有兩個類,例如蒸粗麥粉針織品 ,並且您希望將未知的彩色圖像分類為蒸粗麥粉或針織品。 一種可能的方法是:

  1. 將彩色圖像轉換為灰度。
  2. 計算本地二進制模式。
  3. 計算局部二進制模式的歸一化直方圖。

以下代碼段實現了此方法:

import numpy as np
from skimage import io, color
from skimage.feature import local_binary_pattern

def lbp_histogram(color_image):
    img = color.rgb2gray(color_image)
    patterns = local_binary_pattern(img, 8, 1)
    hist, _ = np.histogram(patterns, bins=np.arange(2**8 + 1), density=True)
    return hist

couscous = io.imread('https://i.stack.imgur.com/u3xLI.png')
knitwear = io.imread('https://i.stack.imgur.com/Zj14J.png')
unknown = io.imread('https://i.stack.imgur.com/JwP3j.png')

couscous_feats = lbp_histogram(couscous)
knitwear_feats = lbp_histogram(knitwear)
unknown_feats = lbp_histogram(unknown)

然后,您需要測量未知圖像的LBP直方圖與表示兩個所考慮的類的圖像的直方圖之間的相似性(或不相似性)。 直方圖之間的歐幾里德距離是一種流行的不相似度量。

In [63]: from scipy.spatial.distance import euclidean

In [64]: euclidean(unknown_feats, couscous_feats)
Out[64]: 0.10165884804845844

In [65]: euclidean(unknown_feats, knitwear_feats)
Out[65]: 0.0887492936776889

在這個例子中,未知圖像將被歸類為針織品,因為不同的未知 - 蒸粗麥粉大於不相似的未知針織品 這與未知圖像實際上是不同類型的針織物這一事實非常一致。

import matplotlib.pyplot as plt

hmax = max([couscous_feats.max(), knitwear_feats.max(), unknown_feats.max()])
fig, ax = plt.subplots(2, 3)

ax[0, 0].imshow(couscous)
ax[0, 0].axis('off')
ax[0, 0].set_title('Cous cous')
ax[1, 0].plot(couscous_feats)
ax[1, 0].set_ylim([0, hmax])

ax[0, 1].imshow(knitwear)
ax[0, 1].axis('off')
ax[0, 1].set_title('Knitwear')
ax[1, 1].plot(knitwear_feats)
ax[1, 1].set_ylim([0, hmax])
ax[1, 1].axes.yaxis.set_ticklabels([])

ax[0, 2].imshow(unknown)
ax[0, 2].axis('off')
ax[0, 2].set_title('Unknown (knitwear)')
ax[1, 2].plot(unknown_feats)
ax[1, 1].set_ylim([0, hmax])
ax[1, 2].axes.yaxis.set_ticklabels([])

plt.show(fig)

地塊

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM