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实现下载网易云音乐的高清MV
Mar 16 Python
Python urllib、urllib2、httplib抓取网页代码实例
May 09 Python
Python函数可变参数定义及其参数传递方式实例详解
May 25 Python
python中zip和unzip数据的方法
May 27 Python
Python中%r和%s的详解及区别
Mar 16 Python
详解Python匿名函数(lambda函数)
Apr 19 Python
使用Python正则表达式操作文本数据的方法
May 14 Python
在pytorch中为Module和Tensor指定GPU的例子
Aug 19 Python
python实现跨excel sheet复制代码实例
Mar 03 Python
python操作toml文件的示例代码
Nov 27 Python
python 基于selectors库实现文件上传与下载
Dec 31 Python
如何使用pdb进行Python调试
Jun 30 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 debug 安装技巧
2011/04/30 PHP
搭建基于Docker的PHP开发环境的详细教程
2015/07/01 PHP
Jquery.LazyLoad.js修正版下载,实现图片延迟加载插件
2011/03/12 Javascript
密码强度检测效果实现原理与代码
2013/01/04 Javascript
JavaScript检测鼠标移动方向的方法
2015/05/22 Javascript
纯javascript实现的小游戏《Flappy Pig》实例
2015/07/27 Javascript
javascript中数组(Array)对象和字符串(String)对象的常用方法总结
2016/12/15 Javascript
JavaScript的六种继承方式(推荐)
2017/06/26 Javascript
关于TypeScript模块导入的那些事
2018/06/12 Javascript
elementUI select组件默认选中效果实现的方法
2019/03/25 Javascript
jquery实现动态创建form并提交的方法示例
2019/05/27 jQuery
vue中datepicker的使用教程实例代码详解
2019/07/08 Javascript
Vue实战教程之仿肯德基宅急送App
2019/07/19 Javascript
jQuery实现获取多选框的值示例
2020/02/07 jQuery
ES6箭头函数和扩展实例分析
2020/05/23 Javascript
Vue之封装公用变量以及实现方式
2020/07/31 Javascript
[01:00]DOTA2 store: Collection of Artisan's Wonders
2015/08/12 DOTA
[01:32]2016国际邀请赛中国区预选赛CDEC战队教练采访
2016/06/26 DOTA
python实现socket客户端和服务端简单示例
2014/02/24 Python
python实现在sqlite动态创建表的方法
2015/05/08 Python
python 3.0 模拟用户登录功能并实现三次错误锁定
2017/11/01 Python
pandas通过loc生成新的列方法
2018/11/28 Python
python操作openpyxl导出Excel 设置单元格格式及合并处理代码实例
2019/08/27 Python
Python scipy的二维图像卷积运算与图像模糊处理操作示例
2019/09/06 Python
python实现udp传输图片功能
2020/03/20 Python
UDP协议功能
2013/01/06 面试题
瀑布模型都有哪些优缺点
2014/06/23 面试题
工作表现评语
2014/01/19 职场文书
自我反省检讨书
2014/01/23 职场文书
个人简历中的自我评价怎么写
2014/01/26 职场文书
教育科研先进个人材料
2014/01/26 职场文书
品质主管岗位职责
2014/03/16 职场文书
我的中国梦演讲稿300字
2014/08/19 职场文书
七一晚会主持词
2015/06/29 职场文书
python删除csv文件的行列
2021/04/06 Python
使用php的mail()函数实现发送邮件功能
2021/06/03 PHP