博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python OpenCV实例:马赛克效果
阅读量:6278 次
发布时间:2019-06-22

本文共 2124 字,大约阅读时间需要 7 分钟。

  hot3.png

#coding:utf-8'''马赛克效果'''import cv2import numpy as nppoint_start = {'x':0,'y':0}point_end = {'x':0,'y':0}rows = 0cols = 0selected = Falsedef mosaic(selected_image,nsize=9):    rows,cols,_ = selected_image.shape    dist = selected_image.copy()    # 划分小方块,每个小方块填充随机颜色    for y in range(0,rows,nsize):        for x in range(0,cols,nsize):            dist[y:y+nsize,x:x+nsize] = (np.random.randint(0,255),np.random.randint(0,255),np.random.randint(0,255))    return distdef mouse_handler(event,x,y,flag,params):    global point_lft,point_rgt,selected        if event == cv2.EVENT_LBUTTONDOWN:        print('button down')        point_start['x'] = x        point_start['y'] = y    if event == cv2.EVENT_LBUTTONUP:        print('button up')        point_end['x'] = x        point_end['y'] = y        selected = True    src = cv2.imread('datas/images/f2.jpg')rows,cols,_ = src.shapesrc_cpy = src.copy()cv2.namedWindow("src")cv2.setMouseCallback("src",mouse_handler)while not selected:    cv2.imshow('src',src)    key = cv2.waitKey(10)# 处理选择的矩形rect = {}if point_start['x'] < point_end['x']:    rect['x'] = point_start['x']    rect['y'] = point_start['y']    rect['width'] = np.abs(point_end['x'] - point_start['x'])    rect['height'] = np.abs(point_end['y'] - point_start['y'])    cv2.rectangle(src,(point_start['x'],point_start['y']),(point_end['x'],point_end['y']),(255,0,0),3)else:    rect['x'] = point_end['x']    rect['y'] = point_end['y']    rect['width'] = np.abs(point_end['x'] - point_start['x'])    rect['height'] = np.abs(point_end['y'] - point_start['y'])    cv2.rectangle(src,(point_end['x'],point_end['y']),(point_start['x'],point_start['y']),(255,0,0),3)cv2.imshow('src',src)print('selected rect = ',rect)# 选择图像select_image = src_cpy[rect['y']:rect['y']+rect['height'],                    rect['x']:rect['x']+rect['height']]result = mosaic(select_image)# 将处理完成的区域合并回原图像src_cpy[rect['y']:rect['y']+rect['height'],        rect['x']:rect['x']+rect['height']] = cv2.addWeighted(result,0.65,select_image,0.35,2.0)cv2.imshow('result',src_cpy)cv2.waitKey()cv2.destroyAllWindows()

014853_fyLR_106657.png

 

 

转载于:https://my.oschina.net/wujux/blog/1797579

你可能感兴趣的文章
聊天界面图文混排
查看>>
控件的拖动
查看>>
svn eclipse unable to load default svn client的解决办法
查看>>
Android.mk 文件语法详解
查看>>
QT liunx 工具下载
查看>>
内核源码树
查看>>
Java 5 特性 Instrumentation 实践
查看>>
AppScan使用
查看>>
Java NIO框架Netty教程(三) 字符串消息收发(转)
查看>>
Ucenter 会员同步登录通讯原理
查看>>
php--------获取当前时间、时间戳
查看>>
Spring MVC中文文档翻译发布
查看>>
docker centos环境部署tomcat
查看>>
JavaScript 基础(九): 条件 语句
查看>>
Linux系统固定IP配置
查看>>
配置Quartz
查看>>
Linux 线程实现机制分析
查看>>
继承自ActionBarActivity的activity的activity theme问题
查看>>
设计模式01:简单工厂模式
查看>>
项目经理笔记一
查看>>