py小工具(转换颜色空间,画图查看轨迹长度与面积)

import cv2 as cv
import numpy as np
import copy as cp
# q键切换rgb/hsv,默认显示rgb
# e清除图像上的轨迹跟文字
# esc退出
filename = 'Genshin.jpg'

img = cv.imread(filename)
img = cv.resize(img, (1024,768), interpolation=cv.INTER_CUBIC)
all_clear = img.copy()
rows, cols = img.shape[:2]

ix, iy = -1, -1
hsv_or_rgb = False
drawing = False  # 绘图种类
is_mouse = False  # 按定鼠标
rgb = ['R:', 'G:', 'B:']
hsv = ['H:', 'S:', 'V:']
contours = []  # 计算轮廓长度和面积
Text_clear = cp.deepcopy(img[:100, :cols//2])


def detect_and_measure(event, x, y, flags, param):
    global ix, iy, Text_clear, hsv_or_rgb, rgb, hsv, is_mouse, contours
    color = (255, 255, 255) if hsv_or_rgb else (0, 0, 255)
    # if event == cv.EVENT_LBUTTONDBLCLK:
    #     cv.circle(img, (x, y), 100, (255, 0, 0), -1)
    if event == cv.EVENT_LBUTTONDOWN:
        is_mouse = True

        ix, iy = x, y
        if len(contours) == 1:  # 轮廓的起点
            contours[0] = (ix, iy)
        else:
            contours.append((ix, iy))

        pixel_value = img[y, x]
        display = ''
        if not hsv_or_rgb:
            for i in range(3):
                display += rgb[i] + str(pixel_value[2-i])+' '
        else:
            for i in range(3):
                display += hsv[i] + str(pixel_value[i])+' '
            print(pixel_value)
        img[:100, :cols//2] = Text_clear
        cv.putText(img, display, (0, 50), cv.FONT_HERSHEY_COMPLEX, 1, color, 1, cv.LINE_AA)
    elif event == cv.EVENT_MOUSEMOVE:
        if is_mouse:
            cv.circle(img, (x, y), 3, color, -1)
            contours.append((x, y))
    elif event == cv.EVENT_LBUTTONUP:
        is_mouse = False
        contour = np.array([contours])
        # 下面计算面积和坐标以及显示
        area = cv.contourArea(contour)
        length = cv.arcLength(contour, False)
        display = 'Area:' + str(int(area*100)/100.0) + ' Length:' + str(int(length*100)/100.0)
        cv.putText(img, display, (0, 80), cv.FONT_HERSHEY_COMPLEX, 1, color, 1, cv.LINE_AA)
        contours.clear()

cv.namedWindow('image')
cv.setMouseCallback('image', detect_and_measure)
while 1:
    cv.imshow('image', img)
    key = cv.waitKey(1)
    key = key & 0xFF
    if key == 27:
        # esc退出
        break
    elif key == ord('q'):
        # 切换rgb与hsv,开始为rgb
        img = all_clear.copy()
        if not hsv_or_rgb:
            img = cv.cvtColor(img, cv.COLOR_BGR2HSV)
            Text_clear = cp.deepcopy(img[:100, :cols // 2])
            hsv_or_rgb = True
        else:
            img = cv.cvtColor(img, cv.COLOR_HSV2BGR)
            Text_clear = cp.deepcopy(img[:100, :cols // 2])
            hsv_or_rgb = False
        all_clear = img.copy()
    elif key == ord('e'):
        img = all_clear.copy()

cv.destroyAllWindows()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值