+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2020-05(17)

2020-06(40)

2020-07(30)

2020-08(30)

2020-09(66)

Opencv学习笔记(二十六)文本检测mser+非极大值抑制nms

发布于2021-04-26 01:15     阅读(262)     评论(0)     点赞(25)     收藏(3)


0

1

2

3

4

5

6

7

8

mser检测

nms非极大值抑制

代码

import cv2
import matplotlib.pyplot as plt
import numpy as np


# NMS 方法(Non Maximum Suppression,非极大值抑制)
def nms(boxes, overlapThresh):
    if len(boxes) == 0:
        return []

    if boxes.dtype.kind == "i":
        boxes = boxes.astype("float")

    pick = []

    # 取四个坐标数组
    x1 = boxes[:, 0]
    y1 = boxes[:, 1]
    x2 = boxes[:, 2]
    y2 = boxes[:, 3]

    # 计算面积数组
    area = (x2 - x1 + 1) * (y2 - y1 + 1)  # 计算各个坐标框的面积

    # 按得分排序(如没有置信度得分,可按坐标从小到大排序,如右下角坐标)
    idxs = np.argsort(y2)  # np.argsort() ,返回元素原来的索引值。可通过索引值输出排序后的数组
    # x= np.array([3, 1, 2])
    # a = np.argsort(x,axis=0) # axis=0为列排序, axis=-1为最大维度方向排序。
    # print(a) 为[1, 2, 0]
    # print(x[a]) 为[1, 2, 3] 通过索引输出排序后的数组
    # 开始遍历,并删除重复的框
    while len(idxs) > 0:
        # 将最右下方的框放入pick数组
        last = len(idxs) - 1
        i = idxs[last]  # i表示右下角最大的框
        pick.append(i)

        # 找剩下的其余框中最大坐标和最小坐标
        xx1 = np.maximum(x1[i], x1[idxs[:last]])
        yy1 = np.maximum(y1[i], y1[idxs[:last]])
        xx2 = np.minimum(x2[i], x2[idxs[:last]])
        yy2 = np.minimum(y2[i], y2[idxs[:last]])

        # 计算重叠面积占对应框的比例,即 IoU
        w = np.maximum(0, xx2 - xx1 + 1)
        h = np.maximum(0, yy2 - yy1 + 1)
        overlap = (w * h) / area[idxs[:last]]

        # 如果 IoU 大于指定阈值,则删除
        # np.where(condation), 若满足条件,返回索引值;np.where()[0],返回行的索引值;np.where()[1]返回列的索引
        # np.where(condation, x, y),满足条件返回x, 不满足返回y.
        # np.concatenate((a1, a2,...), axis=0/1):数组拼接,a1,a2为数组, axis=0为行拼接(默认),axis=1为列拼接
        idxs = np.delete(idxs, np.concatenate(([last], np.where(overlap > overlapThresh)[0])))

    return boxes[pick].astype("int")


if __name__ == '__main__':
    src = cv2.imread(r'F:\OPENCV\Opencv\digit.png')
    img = src.copy()
    img2 = img.copy()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # mser算法
    mser = cv2.MSER_create(_min_area=10)
    regions, boxes = mser.detectRegions(gray)

    keep = []
    for box in boxes:
        x, y, w, h = box
        keep.append([x, y, x + w, y + h])
        cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)

    keep2 = np.array(keep)
    pick = nms(keep2, 0.3)
    for (startX, startY, endX, endY) in pick:
        cv2.rectangle(img2, (startX, startY), (endX, endY), (0, 0, 255), 2)
    titles = ['src', 'img1', 'img2']
    images = [src, img, img2]
    plt.figure(figsize=(3, 1))
    for i in range(len(images)):
        plt.subplot(1, 3, i + 1)
        plt.imshow(images[i][:, :, ::-1], 'gray')
        plt.title(titles[i])
        plt.xticks([])
        plt.yticks([])
    plt.savefig(r'F:\OPENCV\Opencv\mser.jpg', dpi=500)
    plt.show()

结果显示
在这里插入图片描述

原文链接:https://blog.csdn.net/weixin_43170082/article/details/116108270

0

1

2

3

4

5

6

7



所属网站分类: 技术文章 > 博客

作者:dfd323

链接: https://www.pythonheidong.com/blog/article/953646/bcac7ba82817bc8c7c66/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

25 0
收藏该文
已收藏

评论内容:(最多支持255个字符)