python实现图像随机裁剪的示例代码


Posted in Python onDecember 10, 2020

实验条件:

  1. 从1张图像随机裁剪100张图像
  2. 裁剪出图像的大小为 60 x 60
  3. IoU 大于等于 th=0.6 的裁剪框用红色标出,其它裁剪框用蓝色标出
  4. IoU 比对原始区域用绿框标出

实验代码:

import cv2 as cv 
import numpy as np

np.random.seed(0)

# get IoU overlap ratio
def iou(a, b):
	# get area of a
 area_a = (a[2] - a[0]) * (a[3] - a[1])
	# get area of b
 area_b = (b[2] - b[0]) * (b[3] - b[1])

	# get left top x of IoU
 iou_x1 = np.maximum(a[0], b[0])
	# get left top y of IoU
 iou_y1 = np.maximum(a[1], b[1])
	# get right bottom of IoU
 iou_x2 = np.minimum(a[2], b[2])
	# get right bottom of IoU
 iou_y2 = np.minimum(a[3], b[3])

	# get width of IoU
 iou_w = iou_x2 - iou_x1
	# get height of IoU
 iou_h = iou_y2 - iou_y1

	# get area of IoU
 area_iou = iou_w * iou_h
	# get overlap ratio between IoU and all area
 iou = area_iou / (area_a + area_b - area_iou)

 return iou


# crop and create database
def crop_bbox(img, gt, Crop_N=200, L=60, th=0.5):
 # get shape
 H, W, C = img.shape

 # each crop
 for i in range(Crop_N):
  # get left top x of crop bounding box
  x1 = np.random.randint(W - L)
  # get left top y of crop bounding box
  y1 = np.random.randint(H - L)
  # get right bottom x of crop bounding box
  x2 = x1 + L
  # get right bottom y of crop bounding box
  y2 = y1 + L

  # crop bounding box
  crop = np.array((x1, y1, x2, y2))

  # get IoU between crop box and gt
  _iou = iou(gt, crop)

  # assign label
  if _iou >= th:
   cv.rectangle(img, (x1, y1), (x2, y2), (0,0,255), 1)
   label = 1
  else:
   cv.rectangle(img, (x1, y1), (x2, y2), (255,0,0), 1)
   label = 0

 return img

# read image
img = cv.imread("../xiyi.jpg")
img1 = img.copy()
# gt bounding box
gt = np.array((87, 51, 169, 113), dtype=np.float32)

# get crop bounding box
img = crop_bbox(img, gt, Crop_N=100, L=60, th=0.6)

# draw gt
cv.rectangle(img, (gt[0], gt[1]), (gt[2], gt[3]), (0,255,0), 1)
cv.rectangle(img1,(gt[0], gt[1]), (gt[2], gt[3]), (0,255,0), 1)

cv.imshow("result1",img1)
cv.imshow("result", img)
cv.imwrite("out.jpg", img)
cv.waitKey(0)
cv.destroyAllWindows()

实验结果:

python实现图像随机裁剪的示例代码

以上就是python实现图像随机裁剪的示例代码的详细内容,更多关于python 图像裁剪的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
Python中使用PIPE操作Linux管道
Feb 04 Python
Python中Django框架利用url来控制登录的方法
Jul 25 Python
python使用logging模块发送邮件代码示例
Jan 18 Python
Python实现pdf文档转txt的方法示例
Jan 19 Python
人脸识别经典算法一 特征脸方法(Eigenface)
Mar 13 Python
使用Python爬了4400条淘宝商品数据,竟发现了这些“潜规则”
Mar 23 Python
python中的文件打开与关闭操作命令介绍
Apr 26 Python
python爬虫之模拟登陆csdn的实例代码
May 18 Python
Python实现输入二叉树的先序和中序遍历,再输出后序遍历操作示例
Jul 27 Python
Python3 SSH远程连接服务器的方法示例
Dec 29 Python
Python使用itchat模块实现简单的微信控制电脑功能示例
Aug 26 Python
pandas DataFrame运算的实现
Jun 14 Python
python opencv图像处理(素描、怀旧、光照、流年、滤镜 原理及实现)
Dec 10 #Python
python 实现的IP 存活扫描脚本
Dec 10 #Python
class类在python中获取金融数据的实例方法
Dec 10 #Python
Python制作简单的剪刀石头布游戏
Dec 10 #Python
python给list排序的简单方法
Dec 10 #Python
详解java调用python的几种用法(看这篇就够了)
Dec 10 #Python
Python利用imshow制作自定义渐变填充柱状图(colorbar)
Dec 10 #Python
You might like
php面向对象全攻略 (八)重载新的方法
2009/09/30 PHP
php下mysql数据库操作类(改自discuz)
2010/07/03 PHP
PHP计划任务之关闭浏览器后仍然继续执行的函数
2010/07/22 PHP
php不写闭合标签的好处
2014/03/04 PHP
php安装扩展mysqli的实现步骤及报错解决办法
2017/09/23 PHP
php+redis消息队列实现抢购功能
2018/02/08 PHP
javascript动画对象支持加速、减速、缓入、缓出的实现代码
2012/09/30 Javascript
JQuery获取样式中的background-color颜色值的问题
2013/08/20 Javascript
jQuery替换字符串(实例代码)
2013/11/13 Javascript
js数组操作常用方法
2014/05/08 Javascript
jQuery学习笔记之jQuery+CSS3的浏览器兼容性
2015/01/19 Javascript
javascript实现的字符串与十六进制表示字符串相互转换方法
2015/07/17 Javascript
Vue.js学习教程之列表渲染详解
2017/05/17 Javascript
underscore之Chaining_动力节点Java学院整理
2017/07/10 Javascript
JavaScript数组push方法使用注意事项
2017/10/30 Javascript
vue 自定义组件 v-model双向绑定、 父子组件同步通信的多种写法
2017/11/27 Javascript
Vue唯一可以更改vuex实例中state数据状态的属性对象Mutation的讲解
2019/01/18 Javascript
Vue2(三)实现子菜单展开收缩,带动画效果实现方法
2019/04/28 Javascript
JavaScript仿京东秒杀倒计时
2020/03/17 Javascript
原生js实现自定义难度的扫雷游戏
2021/01/22 Javascript
使用python解析xml成对应的html示例分享
2014/04/02 Python
详解Django项目中模板标签及模板的继承与引用(网站中快速布置广告)
2019/03/27 Python
PyQt5 QTable插入图片并动态更新的实例
2019/06/18 Python
Pytorch Tensor的统计属性实例讲解
2019/12/30 Python
CSS3 input框的实现代码类似Google登录的动画效果
2020/08/04 HTML / CSS
全球知名鞋履品牌授权零售商:Journeys
2016/09/17 全球购物
eBay澳大利亚站:eBay.com.au
2018/02/02 全球购物
酒店保安领班职务说明书
2014/03/04 职场文书
初中教师业务学习材料
2014/05/12 职场文书
小学优秀班干部事迹材料
2014/05/25 职场文书
北京颐和园导游词
2015/01/30 职场文书
篮球比赛通讯稿
2015/07/18 职场文书
早恋主题班会
2015/08/14 职场文书
孩子满月酒答谢词
2015/09/30 职场文书
浅谈mysql返回Boolean类型的几种情况
2021/06/04 MySQL
Redis+AOP+自定义注解实现限流
2022/06/28 Redis