python opencv画局部放大图实例教程

目录
  • 为什么要画局部放大图?
  • 程序逻辑
  • 程序实例
  • 总结

这项功能的目的是为了方便使用opencv做图像标注工具。

为什么要画局部放大图?

在做图像数据标注时,很难一次就做到精准标注,经常需要微调才能达到比较好的标注效果。如果目标比较小,即使微调也难以做到精准,所以就需要另外一个窗口对标注区域进行局部放大以方便微调。

程序逻辑

本文中标注信息以矩形框作为示例,矩形框是图像标注中最常用到的一种标注信息形态。其他标注信息的设计逻辑雷同。

程序主要逻辑是:鼠标在任意窗口中做的操作都要同步映射到另外一个窗口。主要通过两个维度进行控制:1. 鼠标在主窗口还是在放大窗口;2. 鼠标的操作。其中主窗口指的是用来显示完整大图的窗口,放大窗口是用来显示局部放大区域的窗口。

  • 鼠标在主窗口

    • 鼠标移动:以当前点(current_pt)为中心显示一块放大区域,显示的范围由一个参数指定default_zoom_image_size。为了确保以current_pt为中心向四个方向均能取到default_zoom_image_size一半的值,current_pt的活动范围是大图减去一定的边缘区域,这个边缘区域就是default_zoom_image_size的一半。在活动范围内,current_pt等同于鼠标当前点,在边缘区域,current_pt等于距离鼠标当前点最近的边缘点。
    • 鼠标画矩形框:左键按下并拖动可以画矩形框,矩形框不会超出大图边界
    • 删除矩形框:有两种情况会删除矩形框 —— 1. 左键画出来的矩形框面积为零;2. 右键点击
  • 鼠标在放大窗口
    • 鼠标移动:什么也不做。
    • 鼠标画矩形框:左键按下并拖动可以画矩形框,矩形框可以超出放大窗口的边界,但是不会超出大图边界
    • 删除矩形框:同主窗口的两种情况
    • 鼠标左键或右键点击:可以起到选择current_pt的作用

总体来说,我们在主窗口做任何操作都可以比较随意,因为主窗口中除了矩形框外,图像本身不发生变化。而在放大窗口中做操作就需要相对保守一些,不然会非常乱,以至于无法操作。所以我们在主窗口中画矩形框时,放大窗口会随着矩形框不停改变;而我们在放大窗口中画矩形框时,放大窗口则保持不变(不然眼花缭乱)。

程序实例

把程序逻辑搞明白后下面代码就比较容易懂了。细节就不多说了,只说一个需要注意的大方向:我们需要为两个窗口各写一个鼠标回调函数。

先看一下单纯画矩形框的代码可能会有助于理解下面程序中的一些细节:opencv-python鼠标画矩形框:cv2.rectangle()

# -*- coding: utf-8 -*-
import cv2

class Rect(object):
    def __init__(self, pt1=(0, 0), pt2=(0, 0)):
        self.tl = pt1
        self.br = pt2
        self.regularize()

    def regularize(self):
        """
        make sure tl = TopLeft point, br = BottomRight point
        """
        tl = (min(self.tl[0], self.br[0]), min(self.tl[1], self.br[1]))
        br = (max(self.tl[0], self.br[0]), max(self.tl[1], self.br[1]))
        self.tl = tl
        self.br = br

    def get_center(self):
        """
        get center point of Rect
        """
        center_x = (self.tl[0] + self.br[0]) // 2
        center_y = (self.tl[1] + self.br[1]) // 2
        return center_x, center_y

    def get_width(self):
        """
        get width of Rect
        """
        return abs(self.br[0] - self.tl[0])

    def get_height(self):
        """
        get height of Rect
        """
        return abs(self.br[1] - self.tl[1])

    def height_over_width(self):
        """
        ratio of height over width
        """
        return self.get_height() / self.get_width()

    def get_area(self):
        """
        get area of Rect
        """
        return self.get_width() * self.get_height()

class DrawZoom(object):
    def __init__(self, image, color,
                 current_pt=(0, 0),
                 default_zoom_image_size=(256, 256)):
        self.original_image = image
        self.color = color
        self.thickness = 2
        self.current_pt = current_pt
        self.default_zoom_image_size = default_zoom_image_size

        self.rect_in_big_image = Rect()
        self.rect_in_zoom_image = Rect()
        self.zoom_offset = (0, 0)

        self.is_drawing_big = False
        self.is_drawing_zoom = False
        self.exist_rect = False

        self.big_image = image.copy()
        self.zoom_image = None
        self.zoom_image_backup = None
        self.get_zoom_image()

    def get_image_height(self):
        """
        get height of big image
        """
        return self.original_image.shape[0]

    def get_image_width(self):
        """
        get width of big image
        """
        return self.original_image.shape[1]

    def get_margin_height(self):
        """
        get height of margin. in the margin area of big image, coordinate of
        current_pt does NOT change
        """
        return self.default_zoom_image_size[0] // 2

    def get_margin_width(self):
        """
        get width of margin
        """
        return self.default_zoom_image_size[1] // 2

    def get_zoom_image(self, height_ratio_expand=0.2, width_ratio_expand=0.2):
        """
        get zoom image for two cases: the rect exists or not.
        height_ratio_expand and width_ratio_expand are used for expanding some
        area of rect
        """
        if not self.exist_rect:
            self.get_zoom_image_for_current_pt()
        elif self.rect_in_big_image.get_area() > 0:
            self.get_zoom_image_for_rect(height_ratio_expand,
                                         width_ratio_expand)

    def get_zoom_image_for_current_pt(self):
        """
        get zoom image for current mouse point (when rect does not exist)
        """
        # (x, y) is center coordinate
        x = max(self.current_pt[0], self.get_margin_width())
        x = min(x, self.get_image_width() - self.get_margin_width())
        y = max(self.current_pt[1], self.get_margin_height())
        y = min(y, self.get_image_height() - self.get_margin_height())

        tl_x = x - self.get_margin_width()
        tl_y = y - self.get_margin_height()
        br_x = x + self.get_margin_width()
        br_y = y + self.get_margin_height()
        tl_x, tl_y, br_x, br_y = self.shrink_rect(tl_x, tl_y, br_x, br_y)
        self.zoom_image = self.big_image[tl_y:br_y, tl_x:br_x]
        self.zoom_image_backup = self.original_image[tl_y:br_y, tl_x:br_x]
        self.zoom_offset = (tl_x, tl_y)

    def get_zoom_image_for_rect(self, height_ratio_expand, width_ratio_expand):
        """
        get zoom image when rect exists
        """
        if self.rect_in_big_image.get_area() == 0:
            return None
        height_over_width_for_win_zoom = \
            self.default_zoom_image_size[1] / self.default_zoom_image_size[0]
        center = self.rect_in_big_image.get_center()
        if self.rect_in_big_image.height_over_width() > \
                height_over_width_for_win_zoom:
            half_height = int(0.5 * (1 + height_ratio_expand) *
                              self.rect_in_big_image.get_height())
            half_width = int(half_height / height_over_width_for_win_zoom)
        else:
            half_width = int(0.5 * (1 + width_ratio_expand) *
                             self.rect_in_big_image.get_width())
            half_height = int(half_width * height_over_width_for_win_zoom)
        tl_x = center[0] - half_width
        tl_y = center[1] - half_height
        br_x = center[0] + half_width
        br_y = center[1] + half_height
        tl_x, tl_y, br_x, br_y = self.shrink_rect(tl_x, tl_y, br_x, br_y)
        self.zoom_image = self.big_image[tl_y:br_y, tl_x:br_x]
        self.zoom_image_backup = self.original_image[tl_y:br_y, tl_x:br_x]
        self.zoom_offset = (tl_x, tl_y)

    @staticmethod
    def clip(value, low, high):
        """
        clip value between low and high
        """
        output = max(value, low)
        output = min(output, high)
        return output

    def shrink_point(self, x, y):
        """
        shrink point (x, y) to inside big image
        """
        x_shrink = self.clip(x, 0, self.get_image_width())
        y_shrink = self.clip(y, 0, self.get_image_height())
        return x_shrink, y_shrink

    def shrink_rect(self, pt1_x, pt1_y, pt2_x, pt2_y):
        """
        shrink rect to inside big image
        """
        pt1_x = self.clip(pt1_x, 0, self.get_image_width())
        pt1_y = self.clip(pt1_y, 0, self.get_image_height())
        pt2_x = self.clip(pt2_x, 0, self.get_image_width())
        pt2_y = self.clip(pt2_y, 0, self.get_image_height())
        rect = Rect((pt1_x, pt1_y), (pt2_x, pt2_y))
        rect.regularize()
        tl_x, tl_y = rect.tl
        br_x, br_y = rect.br
        return tl_x, tl_y, br_x, br_y

    def reset_big_image(self):
        """
        reset big_image (for show) using original image
        """
        self.big_image = self.original_image.copy()

    def reset_zoom_image(self):
        """
        reset zoom_image (for show) using the zoom image backup
        """
        self.zoom_image = self.zoom_image_backup.copy()

    def draw_rect_in_big_image(self):
        """
        draw rect in big image
        """
        cv2.rectangle(self.big_image,
                      self.rect_in_big_image.tl, self.rect_in_big_image.br,
                      color=self.color, thickness=self.thickness)

    def draw_rect_in_zoom_image(self):
        """
        draw rect in zoom image
        """
        cv2.rectangle(self.zoom_image,
                      self.rect_in_zoom_image.tl, self.rect_in_zoom_image.br,
                      color=self.color, thickness=self.thickness)

    def update_drawing_big(self):
        """
        update drawing big image, and map the corresponding area to zoom image
        """
        if self.exist_rect:
            self.draw_rect_in_big_image()
        self.get_zoom_image()

    def update_drawing_zoom(self):
        """
        update drawing big and zoom image when drawing rect in zoom image
        """
        if self.exist_rect:
            self.draw_rect_in_big_image()
            self.draw_rect_in_zoom_image()

def onmouse_big_image(event, x, y, flags, draw_zoom):
    if event == cv2.EVENT_LBUTTONDOWN:
        # pick first point of rect
        draw_zoom.is_drawing_big = True
        draw_zoom.rect_in_big_image.tl = (x, y)
        draw_zoom.exist_rect = True
    elif draw_zoom.is_drawing_big and event == cv2.EVENT_MOUSEMOVE:
        # pick second point of rect and draw current rect
        draw_zoom.rect_in_big_image.br = draw_zoom.shrink_point(x, y)
        draw_zoom.reset_big_image()
        draw_zoom.update_drawing_big()
    elif event == cv2.EVENT_LBUTTONUP:
        # finish drawing current rect
        draw_zoom.is_drawing_big = False
        draw_zoom.rect_in_big_image.br = draw_zoom.shrink_point(x, y)
        draw_zoom.rect_in_big_image.regularize()
        if draw_zoom.rect_in_big_image.get_area() == 0:
            draw_zoom.reset_big_image()
            draw_zoom.rect_in_big_image = Rect()
            draw_zoom.exist_rect = False
        draw_zoom.update_drawing_big()
    elif (not draw_zoom.is_drawing_big) and event == cv2.EVENT_RBUTTONDOWN:
        # right button down to erase current rect
        draw_zoom.rect_in_big_image = Rect()
        draw_zoom.exist_rect = False
        draw_zoom.reset_big_image()
        draw_zoom.update_drawing_big()
    else:
        # default case: mouse move without rect
        draw_zoom.current_pt = (x, y)
        draw_zoom.update_drawing_big()

def onmouse_zoom_image(event, x, y, flags, draw_zoom):
    if event == cv2.EVENT_LBUTTONDOWN:
        # pick first point of rect
        draw_zoom.is_drawing_zoom = True
        draw_zoom.rect_in_zoom_image.tl = (x, y)
        draw_zoom.rect_in_big_image.tl = (x + draw_zoom.zoom_offset[0],
                                          y + draw_zoom.zoom_offset[1])
        draw_zoom.exist_rect = True
    elif draw_zoom.is_drawing_zoom and event == cv2.EVENT_MOUSEMOVE:
        # pick second point of rect and draw current rect
        draw_zoom.rect_in_zoom_image.br = (x, y)
        draw_zoom.rect_in_big_image.br = draw_zoom.shrink_point(
            x + draw_zoom.zoom_offset[0], y + draw_zoom.zoom_offset[1])
        draw_zoom.reset_zoom_image()
        draw_zoom.reset_big_image()
        draw_zoom.update_drawing_zoom()
    elif event == cv2.EVENT_LBUTTONUP:
        # finish drawing current rect
        draw_zoom.is_drawing_zoom = False
        draw_zoom.rect_in_big_image.br = draw_zoom.shrink_point(
            x + draw_zoom.zoom_offset[0], y + draw_zoom.zoom_offset[1])
        draw_zoom.rect_in_big_image.regularize()
        if draw_zoom.rect_in_big_image.get_area() == 0:
            draw_zoom.reset_big_image()
            draw_zoom.rect_in_big_image = Rect()
            draw_zoom.rect_in_zoom_image = Rect()
            draw_zoom.exist_rect = False
            draw_zoom.current_pt = draw_zoom.shrink_point(
                x + draw_zoom.zoom_offset[0], y + draw_zoom.zoom_offset[1])
        draw_zoom.update_drawing_big()
    elif (not draw_zoom.is_drawing_big) and event == cv2.EVENT_RBUTTONDOWN:
        # right button down to erase current rect
        draw_zoom.rect_in_big_image = Rect()
        draw_zoom.rect_in_zoom_image = Rect()
        draw_zoom.exist_rect = False
        draw_zoom.reset_big_image()
        draw_zoom.current_pt = draw_zoom.shrink_point(
            x + draw_zoom.zoom_offset[0], y + draw_zoom.zoom_offset[1])
        draw_zoom.update_drawing_big()
    else:
        # mousemove in zoom image will not change the content of image
        pass

if __name__ == '__main__':
    WIN_NAME_BIG = 'big_image'
    WIN_NAME_ZOOM = 'zoom_image'
    image = cv2.imread('1.jpg')
    draw_zoom = DrawZoom(image, (0, 255, 0))
    cv2.namedWindow(WIN_NAME_BIG, 0)
    cv2.namedWindow(WIN_NAME_ZOOM, 0)
    cv2.setMouseCallback(WIN_NAME_BIG, onmouse_big_image, draw_zoom)
    cv2.setMouseCallback(WIN_NAME_ZOOM, onmouse_zoom_image, draw_zoom)
    while True:
        cv2.imshow(WIN_NAME_BIG, draw_zoom.big_image)
        cv2.imshow(WIN_NAME_ZOOM, draw_zoom.zoom_image)
        key = cv2.waitKey(30)
        if key == 27:  # ESC
            break
    cv2.destroyAllWindows()

结果:

总结

到此这篇关于python opencv画局部放大图的文章就介绍到这了,更多相关python opencv局部放大图内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • python+opencv实现论文插图局部放大并拼接效果

    目录 前言 cv2.rectangle() 函数 cv2.resize() 函数 np.hstack() 和 np.vstack() 函数 实现局部框定放大拼接 总结 前言 在制作论文插图时,有时要求将图片的局部放大来展示细节内容,同时将放大图拼接在原图上以方便观察对比. 当然直接利用电脑自带的画图软件或者别的软件也可以很方便地实现,但是如果碰到多个算法处理一张图片后多张图片进行对比就不太方便了,这里主要贴一下 python 代码的实现. cv2.rectangle() 函数 cv2.recta

  • python opencv画局部放大图实例教程

    目录 为什么要画局部放大图? 程序逻辑 程序实例 总结 这项功能的目的是为了方便使用opencv做图像标注工具. 为什么要画局部放大图? 在做图像数据标注时,很难一次就做到精准标注,经常需要微调才能达到比较好的标注效果.如果目标比较小,即使微调也难以做到精准,所以就需要另外一个窗口对标注区域进行局部放大以方便微调. 程序逻辑 本文中标注信息以矩形框作为示例,矩形框是图像标注中最常用到的一种标注信息形态.其他标注信息的设计逻辑雷同. 程序主要逻辑是:鼠标在任意窗口中做的操作都要同步映射到另外一个窗

  • Anaconda下配置python+opencv+contribx的实例讲解

    先吐槽一下opencv 3.1.0的版本cv2.sift和surf就不能用了 看解释是说 什么 "non-free",,必须要到opencv_contrib库中才有,而这个库的编译不是一点点的困难 堪称史上最恶 这几天为了装open_contrib反复编译各种报错已经很无奈了. 查遍了各种大神的各种攻略,花积分下载了各种攻略..基本上没有一个能全部解决的办法. 回帖或者其他的 要么只说 ""我解决了 " 并不说方法,要么就是不详不尽 或者比较高深 其实吧

  • python OpenCV GrabCut使用实例解析

    这篇文章主要介绍了python OpenCV GrabCut使用实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 先上一个效果图: 使用Python3.7+OpenCV 3.x. 需要引入 numpy库. 在图上用鼠标左键和右键标记前景和后景即可. 如果需要重新标记图像,关闭程序重新运行. 以下是具体实现代码 # -*- coding:utf-8 -*- ''' Python: 3.5.7 opencv 3.x 在图上用鼠标左键和右键标记

  • python opencv 画外接矩形框的完整代码

    画外接矩形框,可以画成一个最大的,也可以分别画. # -*- coding: utf-8 -*- import cv2 image = cv2.imread('G:/110w2/mask_tif4/00.png') print(image.shape) print(image.shape[0]) # h print(image.shape[1]) # w # 图像转灰度图 img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) #cv2.imwrite('G:

  • Appium+Python自动化环境搭建实例教程

    目录 前言 环境准备 jdk安装 android-sdk下载安装 android-sdk环境变量 adb环境 连接手机 前言 appium可以说是做app最火的一个自动化框架,它的主要优势是支持android和ios,另外脚本语言也是支持java和Python. 小编擅长Python,所以接下来的教程是appium+python的实例. 学习appium最大的难处在于环境的安装,从入门到真正的放弃,然后就没然后了,10%的人被环境折腾一周以上,只有剩下的10%人品好,可以顺利安装. 环境准备 小

  • python中的多线程实例教程

    本文以实例形式较为详细的讲述了Python中多线程的用法,在Python程序设计中有着比较广泛的应用.分享给大家供大家参考之用.具体分析如下: python中关于多线程的操作可以使用thread和threading模块来实现,其中thread模块在Py3中已经改名为_thread,不再推荐使用.而threading模块是在thread之上进行了封装,也是推荐使用的多线程模块,本文主要基于threading模块进行介绍.在某些版本中thread模块可能不存在,要使用dump_threading来代

  • Python Tkinter简单布局实例教程

    本文实例展示了Python Tkinter实现简单布局的方法,示例中备有较为详尽的注释,便于读者理解.分享给大家供大家参考之用.具体如下: # -*- coding: utf-8 -*- from Tkinter import * root = Tk() # 80x80代表了初始化时主窗口的大小,0,0代表了初始化时窗口所在的位置 root.geometry('80x80+10+10') # 填充方向 ''' Label(root, text = 'l1', bg = 'red').pack(f

  • Python OpenCV简单的绘图函数使用教程

    目录 1.画直线的函数是cv2.line 2.画矩形的函数是cv2.rectangle 3.画圆函数是cv2.circle 4.画椭圆的函数是cv2.elipes 5.画多边形的函数是cv2.polylines 6.添加文字的函数是cv2.putText 1.画直线的函数是cv2.line cv2.line函数语法: cv2.line(img,start_point,end_point,color,thickness=0) cv2.line函数参数解释: img:需要画的图像 start_poi

  • Python学习之用pygal画世界地图实例

    有关pygal的介绍和安装,大家可以参阅<pip和pygal的安装实例教程>,然后利用pygal实现画世界地图.代码如下: #coding=utf-8 import json import pygal.maps.world #Pygal样式保存在模块style中,包括RotateStyle调整颜色和LightColorizedStyle加亮颜色 #也可以写成from pygal.style import LightColorizedStyle, RotateStyle import pygal

  • python opencv检测目标颜色的实例讲解

    实例如下所示: # -*- coding:utf-8 -*- __author__ = 'kingking' __version__ = '1.0' __date__ = '14/07/2017' import cv2 import numpy as np import time if __name__ == '__main__': Img = cv2.imread('example.png')#读入一幅图像 kernel_2 = np.ones((2,2),np.uint8)#2x2的卷积核

随机推荐