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读取txt文件数据的方法(用于接口自动化参数化数据)
Jun 27 Python
Python基于SMTP协议实现发送邮件功能详解
Aug 14 Python
Python 实现域名解析为ip的方法
Feb 14 Python
Django文件存储 默认存储系统解析
Aug 02 Python
python实现最大优先队列
Aug 29 Python
Python网络编程之使用TCP方式传输文件操作示例
Nov 01 Python
python pygame实现球球大作战
Nov 25 Python
Python气泡提示与标签的实现
Apr 01 Python
解决Jupyter因卸载重装导致的问题修复
Apr 10 Python
pandas DataFrame 数据选取,修改,切片的实现
Apr 24 Python
PyTorch预训练Bert模型的示例
Nov 17 Python
Python时间操作之pytz模块使用详解
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中使用临时表查询数据的一个例子
2013/02/03 PHP
php多文件上传实现代码
2014/02/20 PHP
PHP For循环字母A-Z当超过26个字母时输出AA,AB,AC
2020/02/16 PHP
文字幻灯片
2006/06/26 Javascript
NiftyCube——轻松实现圆角边框
2007/02/20 Javascript
jquery $("#variable") 循环改变variable的值示例
2014/02/23 Javascript
手机开发必备技巧:javascript及CSS功能代码分享
2015/05/25 Javascript
JavaScript缓冲运动实现方法(2则示例)
2016/01/08 Javascript
jQuery插件开发汇总
2016/05/15 Javascript
jQuery插件FusionCharts绘制2D环饼图效果示例【附demo源码】
2017/04/10 jQuery
如何通过非数字与字符的方式实现PHP WebShell详解
2017/07/02 Javascript
JavaScript模块模式实例详解
2017/10/25 Javascript
Webpack优化配置缩小文件搜索范围
2017/12/25 Javascript
php 解压zip压缩包内容到指定目录的实例
2018/01/23 Javascript
jQuery创建及操作xml格式数据示例
2018/05/26 jQuery
vue指令之表单控件绑定v-model v-model与v-bind结合使用
2019/04/17 Javascript
简单使用webpack打包文件的实现
2019/10/29 Javascript
js+css实现扇形导航效果
2020/08/18 Javascript
如何使用VSCode愉快的写Python于调试配置步骤
2018/04/06 Python
在IPython中进行Python程序执行时间的测量方法
2018/11/01 Python
python3 pillow模块实现简单验证码
2019/10/31 Python
flask框架json数据的拿取和返回操作示例
2019/11/28 Python
Python+OpenCV实现图像的全景拼接
2020/03/05 Python
Python实现Excel自动分组合并单元格
2021/02/22 Python
用gpu训练好的神经网络,用tensorflow-cpu跑出错的原因及解决方案
2021/03/03 Python
联想韩国官网:Lenovo Korea
2018/05/10 全球购物
美国女士时尚珠宝及配饰购物网站:Icing
2018/07/02 全球购物
生产主管岗位职责
2013/11/10 职场文书
翻译学院毕业生自荐书
2014/02/02 职场文书
学生检讨书范文
2015/01/27 职场文书
个人委托函范文
2015/01/29 职场文书
社区党支部承诺书
2015/04/29 职场文书
2015秋季田径运动会广播稿
2015/08/19 职场文书
python实现监听键盘
2021/04/26 Python
教你用Java在个人电脑上实现微信扫码支付
2021/06/13 Java/Android
MySQL快速插入一亿测试数据
2021/06/23 MySQL