比较python中的LBP

2024-02-24

我生成了这样的纹理图像

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

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

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


假设你有两个类,例如couscous and knitwear,并且您希望对unknown彩色图像作为蒸粗麦粉或针织品。一种可能的方法是:

  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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

比较python中的LBP 的相关文章

随机推荐

  • .NET 中泛型集合和非泛型集合之间的内存使用差异

    我读到收藏品如今在 NET 中 众所周知 使用有一些优点通用集合 over 非通用的 它们是类型安全的 没有转换 没有装箱 拆箱 这就是通用集合具有性能优势的原因 如果我们认为非泛型集合将每个成员存储为object 那么我们可以认为泛型也具
  • 适用于 OS X 的虚拟 HID 键盘

    我正在尝试创建一个虚拟的蓝牙Mac 操作系统的键盘客户端 这意味着我的 Mac 将充当 BT KB 我读到了 OS X 中的蓝牙 API 在 ObjC 中 并且我还发现了适用于 Mac 的 HID API 在 C 中 为了完成这项工作 我知
  • 如何使用Android Studio获取代码覆盖率?

    我正在使用 Android Studio 开发应用程序 我能够运行测试代码 但是 我不知道如何在 android studio 中获得代码覆盖率 我已经看过以下链接 Android Gradle 代码覆盖率 https stackoverf
  • PHP/MYSQL 父子关系

    我有一个这样的表 id name 父 ID 然后我想根据它们的 id 选择某些行 所以像这样 SELECT FROM TABLE WHERE id IN 1 5 8 9 35 我想从这个查询中也显示父 子关系 例如 id parent 1
  • AWS S3图像保存丢失元数据

    我正在使用用 python 2 7x 编写的 AWS Lambda 函数 该函数下载 保存到 tmp 然后将图像文件上传回存储桶 我的图像元数据从带有 http 标头 例如 Content Type image jpeg 等 的原始存储桶开
  • 如何以编程方式设置 app:srcCompat="@drawable/pic" 的 ImageButton 属性?

    如何设置ImageButton的财产 app srcCompat drawable pic 以编程方式 就像是myImageButton setBackgroundResource R drawable eng2 但财产app srcCom
  • 当我尝试连接到服务器时出现“远程服务器超时”异常

    尝试连接时openfire服务器通过以下代码 Connection connection new XMPPConnection https 192 168 0 101 5222 connection connect 我收到一个异常 上面写着
  • 将 NULL 日期时间值从 T-SQL 返回到 C# DataTable ==> DataRow 而不是空字符串

    该值 row 10 来自 DataRow 对象 来自 T SQL 中的 SQL 结果集 我相信它有一个 对象 类型 在我的数据库中 该特定记录的该字段的值为 NULL 但它在结果集中返回空字符串 而不是空值 我想解决根本问题并让我的结果集返
  • RDCOMClient (Outlook) - ggplot

    我正在使用RDCOMClient用于创建 Outlook 电子邮件的库 我想发送一个ggplot作为电子邮件正文内的图像 内联 not作为附件 我认为这可能的唯一方法是将绘图作为图像添加到HTMLBody财产 我尝试了两种不同的方法来在 h
  • XAML 中的中心弹出窗口

    我使用以下代码创建了一个弹出窗口 但我不知道如何将其居中我尝试在运行时自动更改边距 但我不知道该怎么做 但是有人知道如何使弹出窗口居中吗 它没有标准维度 因为我需要全球化我的程序
  • Android android.view.InflateException 二进制 XML 文件第 16 行:错误膨胀类片段

    这是 MainActivity 类 import java io BufferedReader import java io IOException import java io InputStream import java io Inp
  • Asp.net Web API 2.2 OData4 是否支持 group by 子句?

    Asp net Web API 2 2 OData v4 是否支持聚合和 group by 子句 我找不到任何决定性的答案 另一种方法是使用以下方式实现您的服务QueryByCube提供的linq扩展方法自适应LINQ http www a
  • 如何使用 Boost XML 解析器

    我编写了一个 XML 解析器来读取 XML 文件并将其转换为我的 Position 类的对象 效果很好 请看下面它的代码 XML 文件
  • 当两个错误具有相同的字符串时,errors.Is(err, target error) 返回 false

    这不应该失败 因为两个错误具有相同的字符串 但它仍然失败 if errors Is err testspec expectErr t Errorf Error mismatch want v get v testspec expectErr
  • Java中包和目录的区别

    In a Java项目 是否保留所有 java同一文件夹中的文件意味着它们位于同一包中 有什么区别与将所有项目文件保存在一个文件夹中相比 为我们的项目制作一个包 这个线程 https stackoverflow com questions
  • 在 Linux 上使用 bfd.h 编译错误

    我是 Linux 编程新手 正在尝试使用 BFD 库 这是我正在尝试编译的当前代码 include
  • MySQL 中保护列,不允许更新,仅允许在 NULL 时插入

    我想保护日期列中的现有日期不被覆盖 因此 不允许更新日期列 仅当现有字段值为 NULL 日期列默认为 NULL 时才允许插入 是triggers http dev mysql com doc refman 5 0 en triggers h
  • 有没有办法查看 Sql Server 2005 中最近的昂贵查询?

    我最近发现了活动监视器SQL Server 2008 我知道 呃 对吧 最近昂贵的查询 选项卡对我帮助很大 现在我正在尝试调试一个客户端中数据库的过度使用 但是那里的服务器SQL Server 2005 那里有一个活动监视器 但没有 最近昂
  • 为什么四元数有四个变量?

    Unity 引擎的官方文档不包含此内容 而且我的数学 物理研究还不够深入 还没有遇到过四元数 但我知道它与旋转有关 我不明白的是为什么四元数有四个变量 w x y z 而Unity中只有三个旋转轴 四元数基本上是 3D 空间中的一个轴 具有
  • 比较python中的LBP

    我生成了这样的纹理图像 我必须比较两个纹理 我使用了直方图比较方法 image file output ori png img bgr cv2 imread image file height width channel img bgr s