cv2.line() 绘图操作失败

问题描述

cv2.VideoCapture() 捕捉到的图像使用cv2.line()cv2.rectangle() 等函数执行绘图操作时,原始 np.ndarray 格式的图像被转为 cv2.UMat 格式,且绘图失败。

问题背景

调用cv2.VideoCapture()实时拉取摄像头媒体流并进行显示,涉及到两种情况:

  • 直接在原视频帧上进行绘图而后调用 cv2.imshow() 显示,没有报错;
  • 读取到原始视频帧之后,调用神经网络模型进行处理(其间经历了 np.ndarray → torch.Tensor → np.ndarray 的格式转换),在处理后的视频帧上进行绘图操作,出现了前述问题。

解决方案

  • 通过对变量的跟踪发现,原始 np.ndarray 格式的图像是内存连续的,而经神经网络模型处理后的图像为内存非连续,具体可通过对应变量的 flags 属性查看该张量是否连续:
    print(img_in.flags)
    # C_CONTIGUOUS : True
    # F_CONTIGUOUS : False
    # OWNDATA : False
    # WRITEABLE : True
    # ALIGNED : True
    # WRITEBACKIFCOPY : False
    # UPDATEIFCOPY : False
    
    其中, C_CONTIGUOUS : False 表示行不连续, F_CONTIGUOUS : False 则表示列不连续。
  • 调用 np.ascontiguousarray() 函数,将经过神经网络模型处理后的图像强制转化为内存连续,再执行绘图操作,则该问题解决:
    img_out = np.ascontiguousarray(img_out)  # img_out 为经网络处理后的图像
    
  • 调用 copy() 函数,通过显式复制,原变量也能变为内存连续,但推荐使用 np.ascontiguousarray() 函数,因为 copy() 函数执行了深拷贝,增加了资源占用。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
import cv2 import mediapipe as mp import time class FaceDetector(): def __init__(self, minDetectionCon=0.5): self.minDetectionCon = minDetectionCon self.mpFaceDetection = mp.solutions.face_detection self.mpDraw = mp.solutions.drawing_utils self.faceDetection = self.mpFaceDetection.FaceDetection(self.minDetectionCon) def findFaces(self, img, draw=True): imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) self.results = self.faceDetection.process(imgRGB) # print(self.results) bboxs = [] if self.results.detections: for id, detection in enumerate(self.results.detections): bboxC = detection.location_data.relative_bounding_box ih, iw, ic = img.shape bbox = int(bboxC.xmin * iw), int(bboxC.ymin * ih), \ int(bboxC.width * iw), int(bboxC.height * ih) bboxs.append([id, bbox, detection.score]) if draw: img = self.fancyDraw(img,bbox) cv2.putText(img, f'{int(detection.score[0] * 100)}%', (bbox[0], bbox[1] - 20), cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 255), 2) return img, bboxs def fancyDraw(self, img, bbox, l=30, t=5, rt= 1): x, y, w, h = bbox x1, y1 = x + w, y + h cv2.rectangle(img, bbox, (255, 0, 255), rt) # Top Left x,y cv2.line(img, (x, y), (x + l, y), (255, 0, 255), t) cv2.line(img, (x, y), (x, y+l), (255, 0, 255), t) # Top Right x1,y cv2.line(img, (x1, y), (x1 - l, y), (255, 0, 255), t) cv2.line(img, (x1, y), (x1, y+l), (255, 0, 255), t) # Bottom Left x,y1 cv2.line(img, (x, y1), (x + l, y1), (255, 0, 255), t) cv2.line(img, (x, y1), (x, y1 - l), (255, 0, 255), t) # Bottom Right x1,y1 cv2.line(img, (x1, y1), (x1 - l, y1), (255, 0, 255), t) cv2.line(img, (x1, y1), (x1, y1 - l), (255, 0, 255), t) return img def main(): cap = cv2.VideoCapture("Videos/6.mp4") pTime = 0 detector = FaceDetector() while True: success, img = cap.read() img, bboxs = detector.findFaces(img) print(bboxs) cTime = time.time() fps = 1 / (cTime - pTime) pTime = cTime cv2.putText(img, f'FPS: {int(fps)}', (20, 70), cv2.FONT_HERSHEY_PLAIN, 3, (0, 255, 0), 2) cv2.imshow("Image", img) cv2.waitKey(1) if __name__ == "__main__": main() 给以上代码进行解析讲解,并告诉我代码的亮点和难点
05-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_湘江夜话_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值