代码之家  ›  专栏  ›  技术社区  ›  lucians

通过保留原始图像大小来调整图像中的ROI

  •  -1
  • lucians  · 技术社区  · 7 年前

    我有这个测试图像:

    src

    我想实现的是减少内部ROI(文本区域),但保留原始图像大小。

    像这样:

    src_dest

    如果比较这两个图像,它们的尺寸是相同的。 此外,投资回报率也应该居中。

    我使用opencv和python。

    谢谢

    1 回复  |  直到 7 年前
        1
  •  2
  •   Ha Bom    7 年前

    我将ROI调整一半,然后附加到图像上,有关详细信息,请参见代码:

    import numpy as np
    import cv2
    
    image = cv2.imread("1.png")
    
    print(image.shape)
    h = image.shape[0]
    w = image.shape[1]
    
    center_x = int(w/2)
    center_y = int(h/2)
    
    #get the roi, suppose the roi is in the center of the image
    roi = image[center_y-50:center_y+50,center_x-140:center_x+140,:].copy() 
    roi_h = roi.shape[0]
    roi_w = roi.shape[1]
    
    resize_roi = cv2.resize(roi,(int(roi_w/2),int(roi_h/2)))
    print(resize_roi.shape)
    
    #delete the old roi
    image[center_y-50:center_y+50,center_x-140:center_x+140,:] = 255
    
    #append the resize_roi
    image[center_y-int(50/2):center_y+int(50/2),center_x-int(140/2):center_x+int(140/2),:] = resize_roi
    
    cv2.imshow("Image", image)
    cv2.imshow("roi", roi)
    cv2.imshow("resize_roi", resize_roi)
    cv2.waitKey(0)
    

    投资回报率

    enter image description here

    调整ROI

    enter image description here

    具有调整ROI大小的图像

    enter image description here