Python+OpenCV:图像二进制鲁棒独立基本特征(BRIEF, Binary Robust Independent Elementary Features)

82 篇文章 20 订阅

Python+OpenCV:图像二进制鲁棒独立基本特征(BRIEF, Binary Robust Independent Elementary Features)

理论

We know SIFT uses 128-dim vector for descriptors. Since it is using floating point numbers, it takes basically 512 bytes. Similarly SURF also takes minimum of 256 bytes (for 64-dim).

Creating such a vector for thousands of features takes a lot of memory which are not feasible for resource-constraint applications especially for embedded systems. Larger the memory, longer the time it takes for matching.

But all these dimensions may not be needed for actual matching. We can compress it using several methods like PCA, LDA etc.

Even other methods like hashing using LSH (Locality Sensitive Hashing) is used to convert these SIFT descriptors in floating point numbers to binary strings.

These binary strings are used to match features using Hamming distance.

This provides better speed-up because finding hamming distance is just applying XOR and bit count, which are very fast in modern CPUs with SSE instructions.

But here, we need to find the descriptors first, then only we can apply hashing, which doesn't solve our initial problem on memory.

BRIEF comes into picture at this moment. It provides a shortcut to find the binary strings directly without finding descriptors.

It takes smoothened image patch and selects a set of nd (x,y) location pairs in an unique way (explained in paper). Then some pixel intensity comparisons are done on these location pairs.

For eg, let first location pairs be p and q. If I(p)<I(q), then its result is 1, else it is 0. This is applied for all the nd location pairs to get a nd-dimensional bitstring.

This nd can be 128, 256 or 512. OpenCV supports all of these, but by default, it would be 256 (OpenCV represents it in bytes. So the values will be 16, 32 and 64).

So once you get this, you can use Hamming Distance to match these descriptors.

One important point is that BRIEF is a feature descriptor, it doesn't provide any method to find the features. So you will have to use any other feature detectors like SIFT, SURF etc.

The paper recommends to use CenSurE which is a fast detector and BRIEF works even slightly better for CenSurE points than for SURF points.

In short, BRIEF is a faster method feature descriptor calculation and matching. It also provides high recognition rate unless there is large in-plane rotation.

BRIEF in OpenCV

####################################################################################################
# 图像二进制鲁棒独立基本特征(BRIEF, Binary Robust Independent Elementary Features)
def lmc_cv_image_binary_robust_detection():
    """
        函数功能: 图像二进制鲁棒独立基本特征(BRIEF, Binary Robust Independent Elementary Features)。
    """

    stacking_images = []
    image_file_name = ['D:/99-Research/Python/Image/Butterfly01.jpg',
                       'D:/99-Research/Python/Image/Butterfly02.jpg',
                       'D:/99-Research/Python/Image/Butterfly03.jpg',
                       'D:/99-Research/Python/Image/Butterfly04.jpg']
    for i in range(len(image_file_name)):
        # 读取图像
        image = lmc_cv.imread(image_file_name[i])
        image = lmc_cv.cvtColor(image, lmc_cv.COLOR_BGR2RGB)
        gray_image = lmc_cv.cvtColor(image, lmc_cv.COLOR_BGR2GRAY)

        # Initiate FAST detector
        star = lmc_cv.xfeatures2d.StarDetector_create()

        # Initiate BRIEF extractor
        brief = lmc_cv.xfeatures2d.BriefDescriptorExtractor_create()

        # find the keypoints with STAR
        keypoints = star.detect(gray_image, None)

        # compute the descriptors with BRIEF
        keypoints, descriptors = brief.compute(gray_image, keypoints)
        print("描述子个数: {}".format(brief.descriptorSize()))
        print("描述子形状:{}".format(descriptors.shape))

        result_image = lmc_cv.drawKeypoints(gray_image, keypoints, None, color=(255, 0, 0),
                                            flags=lmc_cv.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

        # stacking images side-by-side
        stacking_image = np.hstack((image, result_image))
        stacking_images.append(stacking_image)

    # 显示图像
    for i in range(len(stacking_images)):
        pyplot.figure('Binary Robust Independent Elementary Features %d' % (i + 1))
        pyplot.subplot(1, 1, 1)
        pyplot.imshow(stacking_images[i], 'gray')
        pyplot.title('Binary Robust Independent Elementary Features')
        pyplot.xticks([])
        pyplot.yticks([])
    pyplot.show()

    # 根据用户输入保存图像
    if ord("q") == (lmc_cv.waitKey(0) & 0xFF):
        # 销毁窗口
        pyplot.close('all')
    return

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
图像分割是计算机视觉中的重要任务之一,它的目标是将一幅图像分割成多个具有独立语义的区域。在OCR中,图像分割是将文本区域从图像中分离出来的重要步骤。PythonOpenCV是常用的图像处理工具,下面我们来介绍如何使用PythonOpenCV实现OCR图像分割。 首先,我们需要安装OpenCV和Tesseract OCR。可以通过以下命令安装: ``` pip install opencv-python pip install pytesseract ``` 接下来,我们可以使用以下代码对图像进行分割: ```python import cv2 import pytesseract # 读入图像 img = cv2.imread("test.jpg") # 灰度化 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 二值化 thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1] # 腐蚀操作 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3)) erode = cv2.erode(thresh, kernel, iterations=1) # 查找轮廓 contours, hierarchy = cv2.findContours(erode, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 绘制轮廓 for contour in contours: x, y, w, h = cv2.boundingRect(contour) cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2) # 显示结果 cv2.imshow('result', img) cv2.waitKey(0) ``` 代码解释: 1. 首先读入图像。 2. 灰度化:将图像转换为灰度图像,方便后续处理。 3. 二值化:将图像转换为黑白图像,方便后续处理。 4. 腐蚀操作:对二值图像进行腐蚀操作,将字符区域连接成一个整体。 5. 查找轮廓:使用OpenCV的findContours函数查找轮廓。 6. 绘制轮廓:将轮廓绘制在原始图像上。 7. 显示结果:显示处理结果。 使用pytesseract库可以将分割出来的文本区域进行OCR识别,具体代码如下: ```python import cv2 import pytesseract # 读入图像 img = cv2.imread("test.jpg") # 灰度化 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 二值化 thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1] # 腐蚀操作 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3)) erode = cv2.erode(thresh, kernel, iterations=1) # 查找轮廓 contours, hierarchy = cv2.findContours(erode, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 绘制轮廓并识别文本 for contour in contours: x, y, w, h = cv2.boundingRect(contour) cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2) roi = img[y:y+h, x:x+w] text = pytesseract.image_to_string(roi, lang='chi_sim') print(text) # 显示结果 cv2.imshow('result', img) cv2.waitKey(0) ``` 代码解释: 1. 首先读入图像。 2. 灰度化:将图像转换为灰度图像,方便后续处理。 3. 二值化:将图像转换为黑白图像,方便后续处理。 4. 腐蚀操作:对二值图像进行腐蚀操作,将字符区域连接成一个整体。 5. 查找轮廓:使用OpenCV的findContours函数查找轮廓。 6. 绘制轮廓并识别文本:将轮廓绘制在原始图像上,并使用pytesseract库对文本进行OCR识别。 7. 显示结果:显示处理结果。 以上就是使用PythonOpenCV实现OCR图像分割的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值