Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
byte-dance opened this issue Apr 4, 2023 · 2 comments
Open

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

byte-dance opened this issue Apr 4, 2023 · 2 comments

Comments

@byte-dance
Copy link

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
Copy link
Owner

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
Copy link
Owner

Zhengyushan commented Apr 5, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants