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和php通信乱码问题解决方法
Apr 15 Python
学习python类方法与对象方法
Mar 15 Python
sublime text 3配置使用python操作方法
Jun 11 Python
python编写微信远程控制电脑的程序
Jan 05 Python
python数据批量写入ScrolledText的优化方法
Oct 11 Python
使用numba对Python运算加速的方法
Oct 15 Python
Python实现 PS 图像调整中的亮度调整
Jun 28 Python
Django 重写用户模型的实现
Jul 29 Python
python实现静态web服务器
Sep 03 Python
Pytorch十九种损失函数的使用详解
Apr 29 Python
完美解决ARIMA模型中plot_acf画不出图的问题
Jun 04 Python
Python getattr()函数使用方法代码实例
Aug 10 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中用header图片地址 简单隐藏图片源地址
2008/04/09 PHP
php更改目录及子目录下所有的文件后缀的代码
2010/09/24 PHP
微信公众号开发之通过接口删除菜单
2017/02/20 PHP
thinkPHP框架中执行事务的方法示例
2018/05/31 PHP
laravel框架 api自定义全局异常处理方法
2019/10/11 PHP
javascript options属性集合操作代码
2009/12/28 Javascript
javascript获取下拉列表框当中的文本值示例代码
2013/07/31 Javascript
js控制表单操作的常用代码小结
2013/08/15 Javascript
jquery遍历筛选数组的几种方法和遍历解析json对象
2013/12/13 Javascript
JS小游戏之宇宙战机源码详解
2014/09/25 Javascript
封装属于自己的JS组件
2016/01/27 Javascript
Extjs表单输入框异步校验的插件实现方法
2017/03/20 Javascript
jQuery插件FusionCharts绘制的2D条状图效果【附demo源码】
2017/05/13 jQuery
ExtJs异步无法向外传值和赋值的完美解决办法
2017/06/14 Javascript
Node.js利用js-xlsx处理Excel文件的方法详解
2017/07/05 Javascript
vue axios请求超时的正确处理方法
2018/04/02 Javascript
jQuery使用动画队列自定义动画操作示例
2018/06/16 jQuery
Bootstrap 模态框自定义点击和关闭事件详解
2018/08/10 Javascript
jQuery实现基本动画效果的方法详解
2018/09/06 jQuery
javascript实现的字符串转换成数组操作示例
2019/06/13 Javascript
使用typescript构建Vue应用的实现
2019/08/26 Javascript
简化Python的Django框架代码的一些示例
2015/04/20 Python
Python使用win32com实现的模拟浏览器功能示例
2017/07/13 Python
python 类对象和实例对象动态添加方法(分享)
2017/12/31 Python
使用Python更换外网IP的方法
2018/07/09 Python
django框架实现模板中获取request 的各种信息示例
2019/07/01 Python
python cv2在验证码识别中应用实例解析
2019/12/25 Python
Pytoch之torchvision.transforms图像变换实例
2019/12/30 Python
德国机车企业:FC-Moto
2017/10/27 全球购物
2013年入党人员的自我鉴定
2013/10/25 职场文书
化妆品店促销方案
2014/02/24 职场文书
技校毕业生自荐书
2014/05/23 职场文书
团代会开幕词
2015/01/28 职场文书
软件项目经理岗位职责
2015/04/01 职场文书
四年级作文之植物
2019/09/20 职场文书
解决Golang中ResponseWriter的一个坑
2021/04/27 Golang