【问题标题】:Compare the LBP in python比较python中的LBP
【发布时间】:2018-09-23 02:41:10
【问题描述】:

我生成了这样的纹理图像

我必须比较两个纹理。我使用了直方图比较法。

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")

我用这段代码生成了纹理。我有纹理,我有计算纹理属性的方法,但问题是如何识别两个纹理之间的相似性。

【问题讨论】:

  • 您在问题标题中使用了“LBP”。 LBP (or any of its variants) 是比较纹理的好方法。为什么问题的主体根本不谈论 LBP?相反,您谈论的是直方图,它根本不表征纹理,而 GLCM,在过去 25 年的每次测试中都被 LBP 和其他方法吹出了水面。
  • 我使用 LBP 生成纹理。现在的任务是比较两个纹理。为了比较,我谈到了 GLCM。从您的回答中,我可以理解的是:我们可以使用 LBP 比较纹理。我对吗?情况如何?
  • LBP(局部二进制模式)是一种表征纹理的方法。我不知道你是如何使用它们来生成纹理的?如果您在 Google 上搜索“LBP 纹理”,您将获得大量论文、软件文档等,这将进一步帮助您。 SciKit 和 OpenCV(您为其添加了标签)都有实现。
  • 这就是我理解您正在做的事情:您从一些图像开始,计算 LBP,但不是在直方图中累积每个像素的值,而是将其输出为(二进制?)图片。然后你想使用不同的纹理表征技术来量化这些。这解释了您使用直方图的原因。
  • 我建议你计算LBP.patterns的直方图(不要转换成图像,因为数据类型转换会导致问题!)。查看 LBP 文献。有一些方法可以进一步减少 256 个不同的值,这可能对您的情况有用,因为生成的特征将具有更少的维度并且更容易比较。

标签: python image-processing scikit-image glcm lbph-algorithm


【解决方案1】:

假设您有两个类别,例如 couscousknitwear,并且您希望将 unknown 彩色图像分类为 couscous 或 knitwear .一种可能的方法是:

  1. 将彩色图像转换为灰度图像。
  2. 计算本地二进制模式。
  3. 计算局部二进制模式的归一化直方图。

下面的sn-p实现了这个方法:

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

在本例中,未知图像将被分类为针织品,因为差异 unknown-couscous 大于差异 unknown-knitwear。这与未知图像实际上是不同类型的针织品这一事实非常吻合。

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)

【讨论】:

    猜你喜欢
    • 2016-01-10
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    • 2013-04-13
    • 2013-07-16
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多