Search code examples
matlabimage-processingimage-resizingwatershed

Upscale whatershed output to match original image size


Introduction

Background: I am segmenting images using the watershed algorithm in MATLAB. For memory and time constraints, I prefer to perform this segmentation on subsampled images, let's say with a resize factor of 0.45.

The problem: I can't properly re-scale the output of the segmentation to the original image scale, both for visualization purposes and other post processing steps.


Minimal Working Example

For example, I have this image:

enter image description here

I run this minimal script and I get a watershed segmentation output L that consists in a label image, where each connected component is addressed with a natural number and the borders between the connected components are zero-valued:

im_orig = imread('kitty.jpg'); % Load image [530x530]
im_res = imresize(im_orig, 0.45); % Resize image [239x239]
im_res = rgb2gray(im_res); % Convert to grayscale

im_blur = imgaussfilt(im_res, 5); % Gaussian filtering

L = watershed(im_blur); % Watershed aglorithm

Now I have L that has the same dimension of im_res. How can I use the result stored in L to actually segment the original im_orig image?


Wrong solution

The first approach I tried was to resize L to the original scale by using imresize.

L_big = imresize(L, [size(im_orig,1), size(im_orig,2)]); % Upsample L

Unfortunately the upsampling of L produces a series of unwanted artifacts. It especially loses some of the fundamental zeros that represent the boundaries between the image segments. Here is what I mean:

figure; imagesc(imfuse(im_res, L == 0)); axis auto equal;
figure; imagesc(imfuse(im_orig, L_big == 0)); axis auto equal;

enter image description here enter image description here

I know that this is due to the blurring caused by the upscaling process, but for now I couldn't think about anything else that could succeed.

The only other approach I thought about involve the use of Mathematical Morphology to "enlarge" the boundaries of the resized image and then upsample, but this would still lead to some unwanted artifacts.


TL;DR (or recap)

Is there a way to perform watershed on a downscaled image in MATLAB and then upscale the result to the original image, keeping the crisp region boundaries outputted by the algorithm? Is what I am looking for a completely absurd thing to ask?


Solution

  • If you only need the watershed segment borders after upsizing the image, then just make these little changes:

    L_big = ~imresize(L==0, [size(im_orig,1), size(im_orig,2)]); % Upsample L
    

    and here the results:

    enter image description here enter image description here