def img_cut_roi_resize_to_target_black(img_txt_path,result_path): img_total = [] txt_total = [] file = os.listdir(img_txt_path) for filename in file: first, last = os.path.splitext(filename) if last == ".bmp": # 图片的后缀名 img_total.append(first) # print(img_total) else: txt_total.append(first) for img_ in img_total: if img_ in txt_total: filename_img = img_ + ".bmp" # 图片的后缀名 # print('filename_img:', filename_img) path1 = os.path.join(img_txt_path, filename_img) img = cv2.imread(path1) h, w = img.shape[0], img.shape[1] # 直接读取原图的长宽不会失真 img = cv2.resize(img, (w, h), interpolation=cv2.INTER_CUBIC) # resize 图像大小,否则roi区域可能会报错 # plt.imshow('resized_img',img) # 会报错,之后再次查看resize后的图片(已解决) # plt.show() filename_txt = img_ + ".txt" # print('filename_txt:', filename_txt) n = 1 with open(os.path.join(img_txt_path, filename_txt), "r+", encoding="utf-8", errors="ignore") as f: for line in f: aa = line.split(" ") x_center = w * float(aa[1]) # aa[1]左上点的x坐标 y_center = h * float(aa[2]) # aa[2]左上点的y坐标 width = int(w * float(aa[3])) # aa[3]图片width height = int(h * float(aa[4])) # aa[4]图片height lefttopx = int(x_center - width / 2.0) lefttopy = int(y_center - height / 2.0) # roi = img[lefttopy+1:lefttopy+height+3,lefttopx+1:lefttopx+width+1] # [左上y:右下y,左上x:右下x] (y1:y2,x1:x2)需要调参,否则裁剪出来的小图可能不太好 roi = img[lefttopy:lefttopy + height, lefttopx:lefttopx + width] # 目前没有看出差距 roi = img_resize_to_target_black(roi) # roi = cv2.copyMakeBorder(roi, 50, 50, 50, 50, cv2.BORDER_CONSTANT, value=[255, 255, 255]) # 是将原图长宽各个
时间: 2023-12-11 14:04:50 浏览: 61
image-resize.rar_image resize matlab_resize image matlab
方向上添加50个像素的白边,但是这里注释掉了,可能是因为不需要这个步骤了。接下来,将裁剪好并调整大小的图片保存到指定路径中:
```python
# 保存裁剪好的图片到指定路径
cv2.imwrite(os.path.join(result_path, img_ + "_" + str(n) + ".bmp"), roi)
n += 1
```
最后,该函数返回一个空值。
阅读全文