【发布时间】:2020-11-14 11:26:46
【问题描述】:
我正在尝试预测文档图像中文本行周围的矩形框。为此,我正在使用语义分割。得到矩形框后,我想以适合原始图像的方式调整它们的大小。对于做预测,我将它们调整为 500 x 500 大小。
def draw_boxes(filename):
img=cv2.imread(f'{filename}',0)
ret,img=cv2.threshold(img,150,255,cv2.THRESH_BINARY_INV)
img=cv2.resize(img,(512,512))
img= np.expand_dims(img,axis=-1)
img=np.expand_dims(img,axis=0)
pred=model.predict(img)
pred=np.squeeze(np.squeeze(pred,axis=0),axis=-1)
plt.imsave('test_img_mask.JPG',pred)
coordinates=[]
img = cv2.imread('test_img_mask.JPG',0)
cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU,img)
ori_img=cv2.imread(f'{filename}')
ori_img=cv2.resize(ori_img,(512,512))
contours, hier = cv2.findContours(img, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
for c in contours:
# get the bounding rect
x, y, w, h = cv2.boundingRect(c)
# draw a white rectangle to visualize the bounding rect
cv2.rectangle(ori_img, (x, y), (x+w,y+h), 255, 1)
coordinates.append([x,y,(x+w),(y+h)])
cv2.imwrite("output.jpg",ori_img)
【问题讨论】:
标签: python-3.x machine-learning image-processing deep-learning computer-vision