Superpixel Posterization
Today's blog post was inspired by an example written by my friend and Image Processing Toolbox developer, Alex Taylor. A few years back, Alex tinkered with using toolbox algorithms to achieve a pseudo-artistic "posterization" effect, like this:
imshow('eddins-horn-1000.png')
I thought the technique was cool and worth showing. I also noticed that the example uses several Image Processing Toolbox functions that haven't been in the product very long, including:
So I realized this would be a good chance to introduce you to some recent toolbox additions that you might not know about.
rgb2lab and lab2rgb
The two functions
I have mentioned these functions in a couple of previous blog posts, including "Displaying a color gamut surface" and "Out-of-gamut colors."
boundarymask
The function
I = imread('rice.png'); bw = imbinarize(I,'adaptive'); mask = boundarymask(bw); subplot(1,2,1) imshow(I) title('Original image') subplot(1,2,2) imshow(mask) title('Boundary mask')
imoverlay
The function
I_overlay = imoverlay(I,mask,'yellow');
imshow(I_overlay)
In my first year of writing this blog (2006), I wrote my own function with this name and submitted it to the File Exchange. Now that the Image Processing Toolbox has its own version, I'll probably remove mine from the File Exchange soon.
superpixels
A "superpixel" is simply a group of connected pixels that have similar colors. Computing superpixels has found a regular place in a variety of image analysis and computer vision tasks. The Image Processing Toolbox function
A = imread('kobi.png'); L = superpixels(A,1500); mask = boundarymask(L); B = imoverlay(A,mask,'cyan'); clf imshow(B)
Superpixel Posterization
The superpixel posterization method, as implemented by Alex, starts by using
Here's how it works.
A = imread('eddins-horn.png'); imshow(A) title('Original image')
Next, compute and visualize the superpixel clusters.
[L,N] = superpixels(A,1000);
BW = boundarymask(L);
imshow(imoverlay(A,BW,'cyan'))
In the next step, I want to replace the pixels in each superpixel cluster with the mean of the cluster's colors. But I want to compute the mean in L*a*b* space, so I start by converting from RGB to L*a*b*.
Alab = rgb2lab(A);
I'll use the function
pixel_idx = label2idx(L);
For each of the
Aplab = Alab; Ln = numel(L); for k = 1:N idx = pixel_idx{k}; Aplab(idx) = mean(Alab(idx)); Aplab(idx+Ln) = mean(Alab(idx+Ln)); Aplab(idx+2*Ln) = mean(Alab(idx+2*Ln)); end
Finally, convert back to RGB space.
Ap = lab2rgb(Aplab); imshow(Ap)
Comments
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.