最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python+OpenCV實(shí)現(xiàn)鼠標(biāo)畫(huà)瞄準(zhǔn)星的方法詳解

 更新時(shí)間:2022年08月07日 14:08:42   作者:拜陽(yáng)  
所謂瞄準(zhǔn)星指的是一個(gè)圓圈加一個(gè)圓圈內(nèi)的十字線,就像玩射擊游戲狙擊槍開(kāi)鏡的樣子一樣。本文將利用Python+OpenCV實(shí)現(xiàn)鼠標(biāo)畫(huà)瞄準(zhǔn)星,感興趣的可以嘗試一下

所謂瞄準(zhǔn)星指的是一個(gè)圓圈加一個(gè)圓圈內(nèi)的十字線,就像玩射擊游戲狙擊槍開(kāi)鏡的樣子一樣。這里并不是直接在圖上畫(huà)一個(gè)瞄準(zhǔn)星,而是讓這個(gè)瞄準(zhǔn)星跟著鼠標(biāo)走。在圖像標(biāo)注任務(wù)中,可以利用瞄準(zhǔn)星進(jìn)行一些輔助,特別是回歸類(lèi)的任務(wù),使用該功能可以使得關(guān)鍵點(diǎn)的標(biāo)注更加精準(zhǔn)。

關(guān)于鼠標(biāo)回調(diào)函數(shù)的說(shuō)明可以參考:opencv-python的鼠標(biāo)交互操作

函數(shù)說(shuō)明

import cv2后,可以分別help(cv2.circle)和help(cv2.line)查看兩個(gè)函數(shù)的幫助信息:

cv2.circle()

 

其中四個(gè)必選參數(shù):

img:底圖,uint8類(lèi)型的ndarray

center:圓心坐標(biāo),是一個(gè)包含兩個(gè)數(shù)字的tuple(必需是tuple),表示(x, y)

radius:圓半徑,必需是整數(shù)

color:顏色,是一個(gè)包含三個(gè)數(shù)字的tuple或list,表示(b, g, r)

其他是可選參數(shù):

thickness:點(diǎn)的線寬。必需是大于0的整數(shù),必需是整數(shù),不能小于0。默認(rèn)值是1

lineType:線的類(lèi)型。可以取的值有cv2.LINE_4,cv2.LINE_8,cv2.LINE_AA。其中cv2.LINE_AA的AA表示抗鋸齒,線會(huì)更平滑,畫(huà)圓的時(shí)候使用該類(lèi)型比較好。

cv2.line()

 line(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) -> img
    .   @brief Draws a line segment connecting two points.
    .   
    .   The function line draws the line segment between pt1 and pt2 points in the image. The line is
    .   clipped by the image boundaries. For non-antialiased lines with integer coordinates, the 8-connected
    .   or 4-connected Bresenham algorithm is used. Thick lines are drawn with rounding endings. Antialiased
    .   lines are drawn using Gaussian filtering.
    .   
    .   @param img Image.
    .   @param pt1 First point of the line segment.
    .   @param pt2 Second point of the line segment.
    .   @param color Line color.
    .   @param thickness Line thickness.
    .   @param lineType Type of the line. See #LineTypes.
    .   @param shift Number of fractional bits in the point coordinates.

其中四個(gè)必選參數(shù):

img:底圖,uint8類(lèi)型的ndarray

pt1:起點(diǎn)坐標(biāo),是一個(gè)包含兩個(gè)數(shù)字的tuple(必需是tuple),表示(x, y)

pt2:終點(diǎn)坐標(biāo),類(lèi)型同上

color:顏色,是一個(gè)包含三個(gè)數(shù)字的tuple或list,表示(b, g, r)

其他是可選參數(shù):

thickness:點(diǎn)的線寬。必需是大于0的整數(shù),必需是整數(shù),不能小于0。默認(rèn)值是1

lineType:線的類(lèi)型??梢匀〉闹涤衏v2.LINE_4,cv2.LINE_8,cv2.LINE_AA。其中cv2.LINE_AA的AA表示抗鋸齒,線會(huì)更平滑,畫(huà)圓的時(shí)候使用該類(lèi)型比較好。

簡(jiǎn)單的例子

# -*- coding: utf-8 -*-

import cv2
import numpy as np


def imshow(winname, image):
    cv2.namedWindow(winname, 1)
    cv2.imshow(winname, image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


if __name__ == '__main__':
    image = np.zeros((256, 256, 3), np.uint8)
    center = (128, 128)
    radius = 50
    color = (0, 255, 0)
    thickness = 2

    pt_left = (center[0] - radius, center[1])
    pt_right = (center[0] + radius, center[1])
    pt_top = (center[0], center[1] - radius)
    pt_bottom = (center[0], center[1] + radius)

    cv2.circle(image, center, radius, color, thickness, lineType=cv2.LINE_AA)
    cv2.line(image, pt_left, pt_right, color, thickness)
    cv2.line(image, pt_top, pt_bottom, color, thickness)
    imshow('draw_crosshair', image)

結(jié)果如下:

利用鼠標(biāo)回調(diào)函數(shù)畫(huà)瞄準(zhǔn)星

操作說(shuō)明:

鼠標(biāo)移動(dòng)時(shí)以鼠標(biāo)為圓心跟隨一個(gè)瞄準(zhǔn)星

鼠標(biāo)滾輪控制瞄準(zhǔn)星的大小

+, -號(hào)控制鼠標(biāo)滾輪時(shí)瞄準(zhǔn)星的變化量

代碼如下:

# -*- coding: utf-8 -*-

import cv2

WIN_NAME = 'draw_crosshair'


class DrawCrosshair(object):
    def __init__(self, image, color, center, radius, thickness=1):
        self.original_image = image
        self.image_for_show = image.copy()
        self.color = color
        self.center = center
        self.radius = radius
        self.thichness = thickness
        self.increment = 5

    def increase_radius(self):
        self.radius += self.increment

    def decrease_radius(self):
        self.radius -= self.increment
        self.radius = max(self.radius, 0)

    def increase_increment(self):
        self.increment += 1

    def decrease_increment(self):
        self.increment -= 1
        self.increment = max(self.increment, 1)

    def reset_image(self):
        """
        reset image_for_show using original image
        """
        self.image_for_show = self.original_image.copy()

    def draw_circle(self):
        cv2.circle(self.image_for_show,
                   center=self.center,
                   radius=self.radius,
                   color=self.color,
                   thickness=self.thichness,
                   lineType=cv2.LINE_AA)

    def draw_crossline(self):
        pt_left = (self.center[0] - self.radius, self.center[1])
        pt_right = (self.center[0] + self.radius, self.center[1])
        pt_top = (self.center[0], self.center[1] - self.radius)
        pt_bottom = (self.center[0], self.center[1] + self.radius)
        cv2.line(self.image_for_show, pt_left, pt_right,
                 self.color, self.thichness)
        cv2.line(self.image_for_show, pt_top, pt_bottom,
                 self.color, self.thichness)

    def draw(self):
        self.reset_image()
        self.draw_circle()
        self.draw_crossline()


def onmouse_draw_rect(event, x, y, flags, draw_crosshair):
    if event == cv2.EVENT_MOUSEWHEEL and flags > 0:
        draw_crosshair.increase_radius()
    if event == cv2.EVENT_MOUSEWHEEL and flags < 0:
        draw_crosshair.decrease_radius()

    draw_crosshair.center = (x, y)
    draw_crosshair.draw()


if __name__ == '__main__':
    # image = np.zeros((512, 512, 3), np.uint8)
    image = cv2.imread('luka.jpg')
    draw_crosshair = DrawCrosshair(image,
                                   color=(0, 255, 0),
                                   center=(256, 256),
                                   radius=100,
                                   thickness=2)
    cv2.namedWindow(WIN_NAME, 1)
    cv2.setMouseCallback(WIN_NAME, onmouse_draw_rect, draw_crosshair)
    while True:
        cv2.imshow(WIN_NAME, draw_crosshair.image_for_show)
        key = cv2.waitKey(30)
        if key == 27:  # ESC
            break
        elif key == ord('+'):
            draw_crosshair.increase_increment()
        elif key == ord('-'):
            draw_crosshair.decrease_increment()
    cv2.destroyAllWindows()

結(jié)果如下,有了瞄準(zhǔn)星的輔助,我們可以更加精準(zhǔn)地找到Luka的眼睛中心。同理,我們?cè)谧鋈四橁P(guān)鍵點(diǎn)標(biāo)注時(shí),這個(gè)功能也可以讓我們更加精準(zhǔn)地找到人眼睛的中心。

到此這篇關(guān)于Python+OpenCV實(shí)現(xiàn)鼠標(biāo)畫(huà)瞄準(zhǔn)星的方法詳解的文章就介紹到這了,更多相關(guān)Python OpenCV瞄準(zhǔn)星內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 基于Python實(shí)現(xiàn)的百度貼吧網(wǎng)絡(luò)爬蟲(chóng)實(shí)例

    基于Python實(shí)現(xiàn)的百度貼吧網(wǎng)絡(luò)爬蟲(chóng)實(shí)例

    這篇文章主要介紹了基于Python實(shí)現(xiàn)的百度貼吧網(wǎng)絡(luò)爬蟲(chóng),實(shí)例分析了Python實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲(chóng)的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • python 處理dataframe中的時(shí)間字段方法

    python 處理dataframe中的時(shí)間字段方法

    下面小編就為大家分享一篇python 處理dataframe中的時(shí)間字段方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • python訪問(wèn)mysql數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法(2則示例)

    python訪問(wèn)mysql數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法(2則示例)

    這篇文章主要介紹了python訪問(wèn)mysql數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了兩種Python操作MySQL數(shù)據(jù)庫(kù)的相關(guān)技巧,需要的朋友可以參考下
    2016-01-01
  • OpenCV制作Mask圖像掩碼的案例

    OpenCV制作Mask圖像掩碼的案例

    這篇文章主要介紹了OpenCV制作Mask圖像掩碼的案例,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-02-02
  • python中Scrapy?shell的使用

    python中Scrapy?shell的使用

    這篇文章主要介紹了python入門(mén)之Scrapy?shell的使用,scrapy提供了一個(gè)shell。用來(lái)方便的測(cè)試規(guī)則,下面我們一起進(jìn)入文章學(xué)習(xí)該內(nèi)容吧,需要的小伙伴可以參考一下,希望對(duì)你有所幫助
    2022-02-02
  • python中二維陣列的變換實(shí)例

    python中二維陣列的變換實(shí)例

    這篇文章主要介紹了python中二維陣列的變換實(shí)例,通過(guò)對(duì)比兩種不同的方法分析了二維陣列變換的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2014-10-10
  • python覆蓋寫(xiě)入,追加寫(xiě)入的實(shí)例

    python覆蓋寫(xiě)入,追加寫(xiě)入的實(shí)例

    今天小編就為大家分享一篇python覆蓋寫(xiě)入,追加寫(xiě)入的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-06-06
  • Python?Fire創(chuàng)建簡(jiǎn)單的命令行接口

    Python?Fire創(chuàng)建簡(jiǎn)單的命令行接口

    Python?Fire是一個(gè)開(kāi)源庫(kù),它能夠自動(dòng)生成命令行接口,讓Python程序變得更加友好和易用,本文主要為大家介紹了Python?Fire如何根據(jù)Python函數(shù)自動(dòng)生成命令行接口,感興趣的可以了解下
    2023-11-11
  • pygame 精靈的行走及二段跳的實(shí)現(xiàn)方法(必看篇)

    pygame 精靈的行走及二段跳的實(shí)現(xiàn)方法(必看篇)

    下面小編就為大家?guī)?lái)一篇pygame 精靈的行走及二段跳的實(shí)現(xiàn)方法(必看篇)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-07-07
  • Python之time模塊的時(shí)間戳,時(shí)間字符串格式化與轉(zhuǎn)換方法(13位時(shí)間戳)

    Python之time模塊的時(shí)間戳,時(shí)間字符串格式化與轉(zhuǎn)換方法(13位時(shí)間戳)

    今天小編就為大家分享一篇Python之time模塊的時(shí)間戳,時(shí)間字符串格式化與轉(zhuǎn)換方法(13位時(shí)間戳),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-08-08

最新評(píng)論

封开县| 佳木斯市| 原阳县| 锡林郭勒盟| 梁河县| 洛南县| 根河市| 湖南省| 松溪县| 梨树县| 冀州市| 汉中市| 富川| 工布江达县| 视频| 莫力| 上林县| 镇平县| 稷山县| 大荔县| 封开县| 睢宁县| 罗平县| 冷水江市| 乳山市| 满洲里市| 丹东市| 绵阳市| 壶关县| 宾川县| 牡丹江市| 西昌市| 霍林郭勒市| 宜良县| 新巴尔虎左旗| 隆林| 贵港市| 花莲市| 阜宁县| 普宁市| 望谟县|