Jetson Nano 学习 opencv 5 (关于ROI)

B站视频学习

【AI on the Jetson Nano】国外大神超超超简单明了的人工智能入门课程(英文字幕)_哔哩哔哩_bilibili

1.基础操作

选取图片ROI区域并单独显示,并创建ROI区域的灰度图,并使得原图ROI区域也变成灰度 

import cv2

def roi_camera():
# you can see connected USB cameras by running : ls /dev/video* on the terminal
# for usb camera /dev/video2, the device_id will be 2
    dispH=480
    dispW=640
    # Create the Camera instance
    cam = cv2.VideoCapture('/dev/video0')   #直接用0可能错误
    cam.set(cv2.CAP_PROP_FRAME_WIDTH,dispW)
    cam.set(cv2.CAP_PROP_FRAME_HEIGHT,dispH)
    print('USB Camera is now ready')
    while True:
        try:
            # read the camera image
            ret,frame = cam.read()
            # ROI = 图像[startY:endY,startX,endX]------>浅拷贝,访问原图,修改感兴趣区域的数据,原图也会改变
            roi=frame[50:250,200:400].copy()                  # 设定ROI区域,使用copy---》深拷贝,ROI不影响原图
            roiGray=cv2.cvtColor(roi,cv2.COLOR_BGR2GRAY)      # 将ROI区域变成灰度图
            roiGray=cv2.cvtColor(roiGray,cv2.COLOR_GRAY2BGR)  # 将原图中的ROI区域灰度,解决灰度图与原图像素通道不同
            frame[50:250,200:400]=roiGray
            
            # display the frameQ
            cv2.imshow('ROI',roi)           # 显示ROI区域原始画面
            cv2.imshow('nanoCam', frame)    # 全图画面
            cv2.imshow('GRAY',roiGray)      # 显示ROI区域的灰度图
            # set position
            cv2.moveWindow('ROI',705,0)      
            cv2.moveWindow('nanoCam',0,0)    
            cv2.moveWindow('GRAY',705,250)   

            if cv2.waitKey(25) & 0xFF == ord('q'):
                break
        except KeyboardInterrupt:
            break

    cam.release()
    cv2.destroyAllWindows()

if __name__=="__main__":
    roi_camera()

效果图

 2.将ROI与之前的移动矩形块结合

实现全图灰度,但是矩形块经过的地方就会变成彩色

import cv2

def roi_camera():
    # you can see connected USB cameras by running : ls /dev/video* on the terminal
    # for usb camera /dev/video2, the device_id will be 2
    dispW=640
    dispH=480
    BW=int(.2*dispW)
    BH=int(.2*dispH)
    posX=10
    posY=270
    dx=2
    dy=2
    # Create the Camera instance
    cam = cv2.VideoCapture('/dev/video0')   #直接用0可能错误
    cam.set(cv2.CAP_PROP_FRAME_WIDTH,dispW)
    cam.set(cv2.CAP_PROP_FRAME_HEIGHT,dispH)
    print('USB Camera is now ready')
    while True:
        try:
            # read the camera image
            ret,frame = cam.read()
            # display the frame
            roi=frame[posY:posY+BH,posX:posX+BW].copy()   # write  before last two sentence
            frame=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
            frame=cv2.cvtColor(frame,cv2.COLOR_GRAY2BGR)
            frame[posY:posY+BH,posX:posX+BW]=roi
            
            # 绘制移动矩形
            cv2.rectangle(frame,(posX,posY),(posX+BW,posY+BH),(255,0,0),2)
            cv2.imshow('nanoCam', frame)
            cv2.moveWindow('nanoCam',0,0)
            posX=posX+dx
            posY=posY+dy

            if posX<=0 or posX+BW>dispW:
                dx=dx*(-1)
            if posY<=0 or posY+BH>dispH:
                dy=dy*(-1)
            if cv2.waitKey(25) & 0xFF == ord('q'):
                break
        except KeyboardInterrupt:
            break

    cam.release()
    cv2.destroyAllWindows()

if __name__=="__main__":
    roi_camera()

效果图

3.ROI和鼠标事件结合

实现使用鼠标任意选取ROI区域并在新窗口显示 

import cv2

goFlag=0

def mouse_click(event,x,y,flags,params):
        global x1,y1,x2,y2                    # global variable
        global goFlag
        if event==cv2.EVENT_LBUTTONDOWN:      # 点击左键得到(x1,y1)
            x1=x
            y1=y
            goFlag=0
        if event==cv2.EVENT_LBUTTONUP:        # 松开左键得到(x2,y2)
            x2=x
            y2=y
            goFlag=1

cv2.namedWindow('nanoCam')                    # 全局声明
cv2.setMouseCallback('nanoCam',mouse_click)

if __name__ == '__main__':
    # you can see connected USB cameras by running : ls /dev/video* on the terminal
    # for usb camera /dev/video2, the device_id will be 2
    dispW=640
    dispH=480
    # Create the Camera instance
    cam = cv2.VideoCapture('/dev/video0')   #直接用0可能错误
    cam.set(cv2.CAP_PROP_FRAME_WIDTH,dispW)
    cam.set(cv2.CAP_PROP_FRAME_HEIGHT,dispH)
    print('USB Camera is now ready')
    while True:
        try:
            # read the camera image
            ret,frame = cam.read()
            
            # display the frame           
            cv2.imshow('nanoCam', frame)
            if goFlag==1:
                # 绘制矩形
                frame=cv2.rectangle(frame,(x1,y1),(x2,y2),(0,255,0),3)
                roi=frame[y1:y2,x1:x2]
                cv2.imshow('copy ROI',roi)
                cv2.moveWindow('cpoy ROI',705,0)
            cv2.moveWindow('nanoCam',0,0)

            if cv2.waitKey(25) & 0xFF == ord('q'):
                break
        except KeyboardInterrupt:
            break

    cam.release()
    cv2.destroyAllWindows()

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值