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 相关文章推荐
flask中使用SQLAlchemy进行辅助开发的代码
Feb 10 Python
Python提取Linux内核源代码的目录结构实现方法
Jun 24 Python
使用python遍历指定城市的一周气温
Mar 31 Python
用Python实现随机森林算法的示例
Aug 24 Python
Django model序列化为json的方法示例
Oct 16 Python
将pip源更换到国内镜像的详细步骤
Apr 07 Python
django admin 自定义替换change页面模板的方法
Aug 23 Python
Matplotlib scatter绘制散点图的方法实现
Jan 02 Python
python词云库wordcloud的使用方法与实例详解
Feb 17 Python
详细分析Python collections工具库
Jul 16 Python
详解pytorch tensor和ndarray转换相关总结
Sep 03 Python
解决virtualenv -p python3 venv报错的问题
Feb 05 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代码
2011/11/27 PHP
php4与php5的区别小结(配置异同)
2011/12/20 PHP
php使用socket post数据到其它web服务器的方法
2015/06/02 PHP
PHP读取汉字的点阵数据
2015/06/22 PHP
php判断当前操作系统类型
2015/10/28 PHP
PHP载入图像imagecreatefrom_gif_jpeg_png系列函数用法分析
2016/11/14 PHP
JavaScript 监听textarea中按键事件
2009/10/08 Javascript
无阻塞加载脚本分析[全]
2011/01/20 Javascript
JavaScript设计模式之外观模式实例
2014/10/10 Javascript
在JavaScript中用getMinutes()方法返回指定的分时刻
2015/06/10 Javascript
BootstrapTable refresh 方法使用实例简单介绍
2017/02/20 Javascript
vue弹窗组件的实现示例代码
2018/09/10 Javascript
vue 的点击事件获取当前点击的元素方法
2018/09/15 Javascript
Vue使用lodop实现打印小结
2019/07/06 Javascript
Vue指令实现OutClick的示例
2020/11/16 Javascript
详解Vue3.0 + TypeScript + Vite初体验
2021/02/22 Vue.js
[02:40]DOTA2英雄基础教程 巨牙海民
2013/12/23 DOTA
python操作xml文件示例
2014/04/07 Python
Python Socket编程入门教程
2014/07/11 Python
浅谈Python用QQ邮箱发送邮件时授权码的问题
2018/01/29 Python
Python+selenium实现自动循环扔QQ邮箱漂流瓶
2018/05/29 Python
Python求一批字符串的最长公共前缀算法示例
2019/03/02 Python
python字符串中匹配数字的正则表达式
2019/07/03 Python
Pytorch的mean和std调查实例
2020/01/02 Python
通过实例解析Python return运行原理
2020/03/04 Python
keras中的History对象用法
2020/06/19 Python
M1芯片安装python3.9.1的实现
2021/02/02 Python
极简的HTML5模版
2015/07/09 HTML / CSS
日本著名的平价时尚女性购物网站:Fifth
2016/08/24 全球购物
奥巴马开学演讲稿
2014/05/15 职场文书
毕业实习自我鉴定范文2014
2014/09/26 职场文书
拾金不昧表扬信
2015/01/16 职场文书
安阳殷墟导游词
2015/02/10 职场文书
2015年银行员工工作总结
2015/04/24 职场文书
pytorch实现线性回归以及多元回归
2021/04/11 Python
uniapp 微信小程序 自定义tabBar 导航
2022/04/22 Javascript