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 相关文章推荐
Python3基础之基本数据类型概述
Aug 13 Python
python通过get,post方式发送http请求和接收http响应的方法
May 26 Python
Python利用pandas计算多个CSV文件数据值的实例
Apr 19 Python
基于Python实现用户管理系统
Feb 26 Python
Python3+OpenCV2实现图像的几何变换(平移、镜像、缩放、旋转、仿射)
May 13 Python
Python3中的最大整数和最大浮点数实例
Jul 09 Python
Django使用Jinja2模板引擎的示例代码
Aug 09 Python
Django之模板层的实现代码
Sep 09 Python
python元组的概念知识点
Nov 19 Python
如何使用Python发送HTML格式的邮件
Feb 11 Python
Python学习笔记之装饰器
Aug 06 Python
Python 实现二叉查找树的示例代码
Dec 21 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中支持多种编码的中文字符串截取函数!
2007/03/20 PHP
利用discuz实现PHP大文件上传应用实例代码
2008/11/14 PHP
解析用PHP实现var_export的详细介绍
2013/06/20 PHP
异步加载技术实现当滚动条到最底部的瀑布流效果
2014/09/16 PHP
phpStorm+XDebug+chrome 配置详解
2019/04/01 PHP
用js自动判断浏览器分辨率的代码
2007/01/28 Javascript
Jquery+ajax请求data显示在GridView上(asp.net)
2010/08/27 Javascript
Javascript 面试题随笔
2011/03/31 Javascript
javascript dom追加内容实现示例
2013/09/21 Javascript
jquery无法设置checkbox选中即没有变成选中状态
2014/03/27 Javascript
Javascript基础教程之变量
2015/01/18 Javascript
vue父子组件的数据传递示例
2017/03/07 Javascript
JavaScript实现单例模式实例分享
2017/12/22 Javascript
vue.js实现的幻灯片功能示例
2019/01/18 Javascript
新年快乐! javascript实现超级炫酷的3D烟花特效
2019/01/30 Javascript
webpack + vue 打包生成公共配置文件(域名) 方便动态修改
2019/08/29 Javascript
将RGB值转换为灰度值的简单算法
2019/10/09 Javascript
解决vue v-for src 图片路径问题 404
2019/11/12 Javascript
如何利用node.js开发一个生成逐帧动画的小工具
2019/12/01 Javascript
[46:27]DOTA2上海特级锦标赛主赛事日 - 1 胜者组第一轮#2LGD VS MVP.Phx第一局
2016/03/02 DOTA
[42:52]IG vs VGJ.T 2018国际邀请赛小组赛BO2 第二场 8.18
2018/08/19 DOTA
python+mongodb数据抓取详细介绍
2017/10/25 Python
Python设计模式之门面模式简单示例
2018/01/09 Python
python模糊图片过滤的方法
2018/12/14 Python
Python搭建代理IP池实现检测IP的方法
2019/10/27 Python
tensorflow 限制显存大小的实现
2020/02/03 Python
TensorFLow 数学运算的示例代码
2020/04/21 Python
python 如何把docker-compose.yaml导入到数据库相关条目里
2021/01/15 Python
HTML5中微数据概述及在搜索引擎中的使用举例
2013/02/07 HTML / CSS
Amaze UI 文件选择域的示例代码
2020/08/26 HTML / CSS
来自美国主售篮球鞋的零售商店:KICKSUSA
2017/11/28 全球购物
波兰家居饰品和厨房配件网上商店:Maleomi
2020/12/15 全球购物
工厂厂长岗位职责
2013/11/08 职场文书
初中生期末评语大全
2014/04/24 职场文书
创业计划书之寿司
2019/07/19 职场文书
如何使JavaScript休眠或等待
2021/04/27 Javascript