【问题标题】:Rescaling the predicted rectangular boxes according to the size of the original image根据原始图像的大小重新缩放预测的矩形框
【发布时间】: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


    【解决方案1】:

    好吧,我找到了解决上述问题的方法。可以通过获取重新缩放因子=Original_image_size/512(在我的情况下)然后与我的坐标相乘来完成。

    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}')
        (H, W) = ori_img.shape[:2]
        (newW, newH) = (512, 512)
        rW = W / float(newW)
        rH = H / float(newH)
    
        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, (int(x*rW),int(y*rH)), (int((x+w)*rW),int((y+h)*rH)), (255,0,0), 1)
            
        cv2.imwrite("output.jpg",ori_img)
    

    【讨论】:

      猜你喜欢
      • 2012-01-24
      • 2020-12-21
      • 2019-05-23
      • 1970-01-01
      • 2023-01-14
      • 1970-01-01
      • 2014-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多