uint8 image solarization issue

7 views (last 30 days)
Aaron Connell
Aaron Connell on 25 Feb 2018
Commented: DGM on 18 Jun 2024
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

Answers (1)

Walter Roberson
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
Aaron Connell
Aaron Connell on 26 Feb 2018
this did not work unfortunately Sir. This is how I got it to work, but I thank you for the answer either way
the_image=imread('sample_image.png'); % read in the desired image
y=uint8(the_image); % make sure image is uint8 before performing solarization
subplot(2,2,1)
imshow(y)
title('Original UINT8 Image')
subplot(2,2,2)
imshow(255-y)
title('Completely Complemented Image')
mask = y < 128; %only complement the pixels with grayscales less than 128, or the darker pixels
convert_mask=uint8(mask);
image=convert_mask.*y + (1-convert_mask).*(255-y);
subplot(2,2,3)
imshow(image)
title('Darker Pixels Are Complemented')
mask1=y > 128; %only complement the lighter pixels
convert_mask1=uint8(mask1);
image1=convert_mask1.*y + (1-convert_mask1).*(255-y);
subplot(2,2,4)
imshow(image1)
title('Lighter Pixels Are Complemented')
%%Question 1 Part C: Use the solarization functions on the bergen image.
the_image=imread('bergen.jpg'); % read in the desired image
y=uint8(the_image); % make sure image is uint8 before performing solarization
subplot(2,2,1)
imshow(y)
title('Original UINT8 Image')
subplot(2,2,2)
imshow(255-y)
title('Completely Complemented Image')
mask2 = y < 128; %only complement the pixels with grayscales less than 128, or the darker pixels
convert_mask2=uint8(mask2);
image2=convert_mask2.*y + (1-convert_mask2).*(255-y);
subplot(2,2,3)
imshow(image2)
title('Darker Pixels Are Complemented')
mask3=y > 128; %only complement the lighter pixels
convert_mask3=uint8(mask3);
image3=convert_mask3.*y + (1-convert_mask3).*(255-y);
subplot(2,2,4)
imshow(image3)
title('Lighter Pixels Are Complemented')
DGM
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)
Of course, there are existing tools to do this.
% 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)

Sign in to comment.

Categories

Find more on Images 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!