Tensorflow2——图像定位


给定一副图片,我们要输出四个数字(x,y,w,h),图像中某一个点的坐标(x,y),以及图像的宽度和高度,有了这四个数字,我们可以很容易的找到物体的边框。

1、单张图片图像定位

import tensorflow as tf
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
from lxml import etree
import glob
from matplotlib.patches import Rectangle

img=tf.io.read_file("./location/images/Abyssinian_1.jpg")
img=tf.image.decode_jpeg(img)
plt.imshow(img)

在这里插入图片描述
#读取xml文件

xml=open("./location/annotations/xmls/Abyssinian_1.xml").read()
#解析
sel=etree.HTML(xml) #建立好选择器
width=int(sel.xpath("//size/width/text()")[0])
height=int(sel.xpath("//size/height/text()")[0])
xmin=int(sel.xpath("//bndbox/xmin/text()")[0])
xmax=int(sel.xpath("//bndbox/xmax/text()")[0])
ymin=int(sel.xpath("//bndbox/ymin/text()")[0])
ymax=int(sel.xpath("//bndbox/ymax/text()")[0])
#根目录下的size里的width,取出text文本
#这样解析出来的是一个列表,列表里面放置的有文本
## width,height,xmin,xmax,ymin,ymax
#(600, 400, 333, 425, 72, 158)

plt.imshow(img)
rec=Rectangle((xmin,ymin),(xmax-xmin),(ymax-ymin),fill=False,color="red")  #最下角的值就是xmin,ymin
ax=plt.gca()  #获取当前图像
ax.axes.add_patch(rec)

在这里插入图片描述

2、随意尺度图片定位

(代码紧接上)

img=tf.image.resize(img,(224,224))
img=img/255
plt.imshow(img)

在这里插入图片描述

xmin=(xmin/width)*224
xmax=(xmax/width)*224
ymin=(ymin/height)*224
ymax=(ymax/height)*224

plt.imshow
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值