Zhengyushan / kat

The code for Kernel attention transformer (KAT)
MIT License
28 stars 5 forks source link

can't understand the function 'extract_tile' in loader.py #3

Open byte-dance opened 1 year ago

byte-dance commented 1 year ago

def extract_tile(image_dir, tile_size, x, y, width, height): x_start_tile = x // tile_size y_start_tile = y // tile_size x_end_tile = (x + width) // tile_size y_end_tile = (y + height) // tile_size

tmp_image = np.ones(
    ((y_end_tile - y_start_tile + 1) * tile_size, (x_end_tile - x_start_tile + 1) * tile_size, 3),
    np.uint8) * 240

for y_id, col in enumerate(range(x_start_tile, x_end_tile + 1)):
    for x_id, row in enumerate(range(y_start_tile, y_end_tile + 1)):
        img_path = os.path.join(image_dir, '{:04d}_{:04d}.jpg'.format(row, col))
        if not os.path.exists(img_path):
            continue
        img = cv2.imread(img_path)
        h, w, _ = img.shape
        tmp_image[(x_id * tile_size):(x_id * tile_size + h), (y_id * tile_size):(y_id * tile_size + w), :] = img

x_off = x % tile_size
y_off = y % tile_size
output = tmp_image[y_off:y_off + height, x_off:x_off + width]

return output

what's the meaning of 'pos[1] step' in the below function? why you regard it as x and pass it to function extract_file? def extract_and_save_tiles(image_dir, slide_save_dir, position_list, tile_size, imsize, step, invert_rgb=False): for pos in position_list: img = extract_tile(image_dir, tile_size, pos[1] step, pos[0] * step, imsize, imsize)

    if len(img) > 0:
        if invert_rgb:
            img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
        cv2.imwrite(
            os.path.join(slide_save_dir, '{:04d}_{:04d}.jpg'.format(pos[1], pos[0])), img)
Zhengyushan commented 1 year ago

The array 'pos' stores the row-column postions of the patches that are generated by a sliding window with a step length of 'step'. So, 'pos * step' indicates the x-y coordinate of the patch on the WSI.

The function 'extract_tile' is used to load a patch in the given position and given size. It is specific for our file struture where the WSI is stored in separated tiles. You may rewrite the function to fit your file structure.

Zhengyushan commented 1 year ago

The array 'pos' stores the row-column positions of the patches that are generated by a sliding window with a step length of 'step'. So, 'pos * step' indicates the x-y coordinate of the patch on the WSI.

The function 'extract_tile' is used to load a patch in the given position and given size. It is specific for our file structure where the WSI is stored in separated tiles. You may rewrite the function to fit your file structure.

发件人: @. @.> 代表 bytedance 发送时间: 2023年4月4日 22:33 收件人: Zhengyushan/kat @.> 抄送: Subscribed @.> 主题: [Zhengyushan/kat] can't understand the function 'extract_tile' in loader.py (Issue #3)

def extract_tile(image_dir, tile_size, x, y, width, height): x_start_tile = x // tile_size y_start_tile = y // tile_size x_end_tile = (x + width) // tile_size y_end_tile = (y + height) // tile_size

tmp_image = np.ones( ((y_end_tile - y_start_tile + 1) tile_size, (x_end_tile - x_start_tile + 1) tile_size, 3), np.uint8) * 240

for y_id, col in enumerate(range(x_start_tile, x_end_tile + 1)): for x_id, row in enumerate(range(y_start_tile, y_end_tile + 1)): img_path = os.path.join(imagedir, '{:04d}{:04d}.jpg'.format(row, col)) if not os.path.exists(img_path): continue img = cv2.imread(imgpath) h, w, = img.shape tmp_image[(x_id tile_size):(x_id tile_size + h), (y_id tile_size):(y_id tile_size + w), :] = img

x_off = x % tile_size y_off = y % tile_size output = tmp_image[y_off:y_off + height, x_off:x_off + width]

return output

what's the meaning of 'pos[1] step' in the below function? why you regard it as x and pass it to function extract_file? def extract_and_save_tiles(image_dir, slide_save_dir, position_list, tile_size, imsize, step, invert_rgb=False): for pos in position_list: img = extract_tile(image_dir, tile_size, pos[1] step, pos[0] * step, imsize, imsize)

if len(img) > 0:
    if invert_rgb:
        img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
    cv2.imwrite(
        os.path.join(slide_save_dir, '{:04d}_{:04d}.jpg'.format(pos[1], pos[0])), img)

— Reply to this email directly, view it on GitHub https://github.com/Zhengyushan/kat/issues/3 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AG6JCPATTRLXEUPGI2XDFTTW7QWKTANCNFSM6AAAAAAWSZXRPY . You are receiving this because you are subscribed to this thread. https://github.com/notifications/beacon/AG6JCPGZOO7WCPO7S57IWBLW7QWKTA5CNFSM6AAAAAAWSZXRP2WGG33NNVSW45C7OR4XAZNFJFZXG5LFVJRW63LNMVXHIX3JMTHGFFR6FA.gif Message ID: @. @.> >