import cv2
import glob
# box folder path
path = "/box"
# original image path
#img_path = "/run/media/syc/sakamoto/dianjing/bad_particles/HBsAg_tif"
#img_path = "/box"
save_path = "/box_cls/"#保存路径
files = glob.glob(path+'/*.box')#读取目录下的所有.box文件
for file in files:
filename = file.split('/')[-1]#读取文件名字
img = cv2.imread(img_path+"/"+filename[:len(filename)-3]+"tif")#读取图片
H,W = img.shape[:2]#读取高,宽
with open(file) as f:
for line in f.readlines():#读取行
x, y, h, w = [int(x) for x in line.split('\t')[:4]]#读取.box格式的文件
x, y = x, H - y#计算box右下角坐标
cv2.rectangle(img, (x, y), (x + h, y - w), (50, 200, 50), 5)#画bounding-box
cv2.imwrite(save_path+"/"+filename+".jpg",img)#存为jpg格式图片
# new image saving path (err when not exist)
save_path = "/box_cls/"
files = glob.glob(path+'/*.box')#读取目录下的所有.box文件
for file in files:
filename = file.split('/')[-1]
img = cv2.imread(img_path+"/"+filename[:len(filename)-3]+"tif")
H,W = img.shape[:2]
with open(file) as f:
for line in f.readlines():
x, y, h, w = [int(x) for x in line.split('\t')[:4]]
x, y = x, H - y
cv2.rectangle(img, (x, y), (x + h, y - w), (50, 200, 50), 5)
cv2.imwrite(save_path+"/"+filename+".jpg",img)
【看代码学Python】图像基本处理
最新推荐文章于 2024-05-16 06:13:35 发布