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 PyQt4实现QQ抽屉效果
Apr 20 Python
Python实现正弦信号的时域波形和频谱图示例【基于matplotlib】
May 04 Python
Python 3.x 判断 dict 是否包含某键值的实例讲解
Jul 06 Python
python使用正则表达式来获取文件名的前缀方法
Oct 21 Python
pygame游戏之旅 添加碰撞效果的方法
Nov 20 Python
解决Python正则表达式匹配反斜杠''\''问题
Jul 17 Python
python global关键字的用法详解
Sep 05 Python
详解Python list和numpy array的存储和读取方法
Nov 06 Python
浅谈python输出列表元素的所有排列形式
Feb 26 Python
pycharm 2018 激活码及破解补丁激活方式
Sep 21 Python
Python利用Pillow(PIL)库实现验证码图片的全过程
Oct 04 Python
Python 虚拟环境工作原理解析
Dec 24 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中Session ID的实现原理实例分析
2019/08/17 PHP
php提高脚本性能的4个技巧
2020/08/18 PHP
PHP中SESSION过期设置
2021/03/09 PHP
最新28个很棒的jQuery 教程
2011/05/28 Javascript
各浏览器中querySelector和querySelectorAll的实现差异分析
2012/05/23 Javascript
基于JQuery的类似新浪微博展示信息效果的代码
2012/07/23 Javascript
Javascript学习笔记之 对象篇(三) : hasOwnProperty
2014/06/24 Javascript
使用变量动态设置js的属性名
2014/10/19 Javascript
angularJS中router的使用指南
2015/02/09 Javascript
Javascript中的方法和匿名方法实例详解
2015/06/13 Javascript
深入理解js函数的作用域与this指向
2016/05/28 Javascript
老生常谈js中0到底是 true 还是 false
2017/03/08 Javascript
js实现彩色条纹滚动条效果
2017/03/15 Javascript
Vue2.0用户权限控制解决方案
2017/11/29 Javascript
vue2.0实现的tab标签切换效果(内容可自定义)示例
2019/02/11 Javascript
Layer组件多个iframe弹出层打开与关闭及参数传递的方法
2019/09/25 Javascript
微信小程序监听用户登录事件的实现方法
2019/11/11 Javascript
JS端基于download.js实现图片、视频时直接下载而不是打开预览
2020/05/09 Javascript
python抓取京东商城手机列表url实例代码
2013/12/18 Python
windows下python安装paramiko模块和pycrypto模块(简单三步)
2017/07/06 Python
详解用python实现基本的学生管理系统(文件存储版)(python3)
2019/04/25 Python
python自动发微信监控报警
2019/09/06 Python
Python实现bilibili时间长度查询的示例代码
2020/01/14 Python
python实现高斯投影正反算方式
2020/01/17 Python
python Shapely使用指南详解
2020/02/18 Python
Django与pyecharts结合的实例代码
2020/05/13 Python
深入理解Python 多线程
2020/06/16 Python
有关HTML5页面在iPhoneX适配问题
2017/11/13 HTML / CSS
太阳镜仓库,售价20美元或更少:Sunglass Warehouse
2016/09/28 全球购物
德国自行车商店:Tretwerk
2019/06/21 全球购物
自1926年以来就为冰岛保持温暖:66°North
2020/11/27 全球购物
3个CCIE对一个工程师的面试题
2012/05/06 面试题
客户代表实习人员自我鉴定
2013/09/27 职场文书
敬老文明号事迹材料
2014/01/16 职场文书
企业仓管员岗位职责
2014/06/15 职场文书
SQL Server 忘记密码以及重新添加新账号
2022/04/26 SQL Server