uint8 image solarization issue
2 views (last 30 days)
Show older comments
Good afternoon I am trying to take a uint8 image and take the complement of all pixels with a grayscale less then 128 and again with all pixels with a grayscale greater than 128. This is an example of my attempt but I don't think it is working properly, if anyone could please give me a hand. I am supposed to create the two functions and then create a small test image to test the functions before applying them to an image but I don't know how to do that.
b=imread('blocks.jpg');
class(b)
comp_light_pixels=b<128 %only complement the lighter pixels of b
subplot(2,2,1)
imshow(comp_light_pixels) %show image with lighter pixels complemented
comp_dark_pixels=b>128 %only complement the darker pixels
subplot(2,2,2)
imshow(comp_dark_pixels) % show image with dark pixels complemented
subplot(2,2,3)
imshow(b) % show original image
subplot(2,2,4)
imshow(255-b) % show completely complemented image
0 Comments
Answers (1)
Walter Roberson
on 25 Feb 2018
comp_light_pixels = b;
mask = comp_light_pixels < 128;
comp_light_pixels(mask) = 255 - comp_light_pixels(mask);
2 Comments
DGM
on 18 Jun 2024
Granted, it's not like "solarize" is really a well-defined transformation. That said, the given example still doesn't do what's probably expected.
% the image can only be uint8 class
% otherwise, everything will fail
x = uint8(0:255);
% only complement the pixels with grayscales less than 128, or the darker pixels
mask = x < 128;
convert_mask = uint8(mask);
y1 = convert_mask.*x + (1-convert_mask).*(255-x);
% only complement the lighter pixels
mask1 = x > 128;
convert_mask1 = uint8(mask1);
y2 = convert_mask1.*x + (1-convert_mask1).*(255-x);
% plot the curves
plot(x,[y1;y2])
xlim([0 255])
ylim([0 255])
As is common, it's a hard vee curve, but neither case is full swing. That does mean that the contrast is preserved on either side of 50% gray, but I've never seen such an implementation, and have no idea why that would be a desirable interpretation.
This is a far simpler way to do the work. It's a more typical curve, and it's not blindly dependent on the input image class.
% some image in any class
x0 = uint8(0:255);
% put it in a consistent and convenient class and scale
x = im2double(x0);
% interpolate
tf = [0 1 0]; % hard vee curve
%tf = [1 0 1]; % inverted hard vee
y = interp1([0 0.5 1],tf,x,'linear');
% cast the output if you want it in some particular class
y = im2uint8(y);
% plot the curve
plot(x0,y)
xlim([0 255])
ylim([0 255])
The output curve is full swing, as is more typical. The input image can be of any typical numeric class.
% some image in any class
inpict = imread('peppers.png');
% put it in a consistent and convenient class and scale
inpict = im2double(inpict);
% interpolate
tf = [0 1 0]; % hard vee curve
%tf = [1 0 1]; % inverted hard vee
outpict = interp1([0 0.5 1],tf,inpict,'linear');
% cast the output if you want it in some particular class
outpict = im2uint8(outpict);
% display the result
imshow(outpict)
% some image in any class
inpict = imread('peppers.png');
% solarize it (MIMT solarize())
%outpict = solarize(inpict,'vee'); % simple vee curve
outpict = solarize(inpict); % a nonlinear curve
% display the result
imshow(outpict)

See Also
Categories
Find more on Convert Image Type in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!