3

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?

UJIN
  • 1,648
  • 13
  • 28

3 Answers3

2

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

Ozcan
  • 726
  • 4
  • 13
1

you can use nearest neighbor interpolation when resizing:

L_big = imresize(L, [size(im_orig,1), size(im_orig,2)],'nearest'); % Upsample L
user2999345
  • 4,195
  • 1
  • 13
  • 20
0

Normally when we resize images we star fro the destination, iterate over x, y, and find the best matching pixel in the source. Here you want to do the reverse. Iterate over the source in x, y and write to the destination buffer, with 0 taking priority (so initialise to 0xFF, then don't overwrite any zeroes with other values),

There's unlikely to be a function that does this on the toolkit, you;ll hav e to roll your own.

Malcolm McLean
  • 6,258
  • 1
  • 17
  • 18