OpenCV-Python系列·第十九集-1:视频处理(negative film)

博客介绍了利用鼠标事件绘制矩形框,并对框内像素点进行反转的操作,涉及信息技术中图形绘制与像素处理相关内容。

Tip:应用鼠标事件画出一个矩形框,再对像素点进行反转

# -*- coding: utf-8 -*-
"""
Created on Sun Aug 26 16:30:20 2018

@author: Miracle
"""

import cv2

def drawRectangle(event,x,y,flags,params):
    global init_x,init_y,drawing,point_top_left,point_bottom_right
    #鼠标响应
    if event == cv2.EVENT_LBUTTONDOWN:
        drawing = True
        init_x,init_y = x,y
    elif event == cv2.EVENT_MOUSEMOVE:
        if drawing:
            point_top_left = (min(init_x,x),min(init_y,y))
            point_bottom_right = (max(init_x,x),max(init_y,y))
            image[init_y:y,init_x:x] = 255 - image[init_y:y,init_x:x]
    elif event == cv2.EVENT_LBUTTONUP:
        drawing = False
        point_top_left = (min(init_x,x),min(init_y,y))
        point_bottom_right = (max(init_x,x),max(init_y,y))
        image[init_y:y,init_x:x] = 255 - image[init_y:y,init_x:x]

if __name__ == '__main__':
    drawing = False
    point_bottom_right,point_top_left = (-1,-1),(-1,-1)
    
    cap = cv2.VideoCapture(0)
    
    if not cap.isOpened():
        raise IOError('Cannot open webcam!')
    
    cv2.namedWindow('webcam')
    cv2.setMouseCallback('webcam',drawRectangle)
    
    while True:
        ret,frame = cap.read()
        image = cv2.resize(frame,None,fx = 1.25,fy = 1.25,
                           interpolation = cv2.INTER_LINEAR)
        
        (x0,y0),(x1,y1) = point_top_left,point_bottom_right
        image[y0:y1,x0:x1] = 255 - image[y0:y1,x0:x1] 
        cv2.imshow('webcam',image)
        
        key = cv2.waitKey(1)
        if key == 27:
            break
    
    cap.release()
    cv2.destroyAllWindows()
    
    
            
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值