How to apply intensity difference of different level on the grayscale image?

I created an image of a single alphabet at the middle(in white) with its background in black.I changed its font size to 5 different sizes,which are 58 to 66, increment by 2 each time.Next, I need to apply specific intensity difference level on the grayscale image,which is 20%,40%,60%,80%,100%.I am stucked at here, because it needs to apply the pixel value to the alphabet and its background with the corresponding intensity difference level.The formula used is shown as follow:
The code and images are attached as below:
%create blank image
w = 150;
h = 150;
blankImage= 255*ones(w,h,3,'uint8');
%position of the letter in the empty cell
position_x = (w+1)/2;
position_y = (h+1)/2;
% varying the font size, start from 10 to 16
font_start = 58;
font_end = 66;
num_fontsA = font_start:2:font_end;
% get the number of fonts
numImagesA = length(num_fontsA);
% create a cell array with number of fonts to fill in the image in next step
A = cell(1, numImagesA);
noise_start = 0.01;
noise_end = 0.05;
num_noiseimg = noise_start:0.01:noise_end
Total_noiseimg = length(num_noiseimg);
noiseimg_collection = cell(1,Total_noiseimg);
% for loop to create letter 'A'
% grayscale
% store into the cell array
for i=1:numImagesA
for font_size = num_fontsA(i)
image= insertText(blankImage,[position_x position_y],'A','Font','Times New Roman','FontSize',font_size,'TextColor','black','BoxColor','w','BoxOpacity',0,'AnchorPoint','Center');
grayImage= rgb2gray(image);
BWImage = imcomplement(grayImage);
background = BWImage == 0;
foreground = ~background;
Newforegnd = foreground;
% figure('Name','Background and Object');
subplot(5,1,i);
imshow(BWImage);
axis on;
title(sprintf('Font size of %d',font_size));
% Apply noise on the image
% Apply noise on the alphabet, using 0.01 standard deviation
pos = 1
for i=0.01:0.01:0.05
noiseimg_collection{pos} = imnoise(BWImage, 'gaussian',0, i);
pos=pos+1
end
end
end
Hope that anyone can guide me. Thank you!

 Accepted Answer

Isn't it just
intensityDifferencePercent = (100/255) * (max(grayImage(:) - min(grayImage(:)))
or am I missing something?

7 Comments

Hi, but if like this,I could not set the Intensity difference in percentage that I want right? For example 20%.It is because my image now only consist of 255 grayscale level and 0 ,which are white and black color respectively.
Then your contrast will be 100%. As your letter gets less resolution, if it's anti-aliased then the edge pixels will start to become less than 255. So you might take the average over all non-background pixels.
backgroundGrayLevel = min(grayImage(:))
backgroundImage = backgroundGrayLevel * ones(size(grayImage));
foregroundMask = grayImage ~= backgroundImage;
meanForegroundGrayLevel = mean(grayImage(foregroundMask))
intensityDifferencePercent = (100/255) * (meanForegroundGrayLevel - backgroundGrayLevel)
Could you explain in details the coding you write?Now I write on what I understand from it to check with my understnading.
backgroundGrayLevel = min(grayImage(:))
This line means pick the minimum pixel value from the grayscale image,which will be background?
backgroundImage = backgroundGrayLevel * ones(size(grayImage));
Next, the pixel value will apply with matrix of 1 with the same size of the grayscale image and stores in backgroundImage variable.
foregroundMask = grayImage ~= backgroundImage;
This complement the image matrix in backgroundImage and store in foregroundMask variable.Is this mean backgroundImage is the image matrix for whole background, but you complement it to get the foreground pixels?
meanForegroundGrayLevel = mean(grayImage(foregroundMask))
This is what you explain right? apply the mean value from the foreground image matrix(foregroundMask) of grayscaleimage.
intensityDifferencePercent = (100/255) * (meanForegroundGrayLevel - backgroundGrayLevel)
Lastly,you put the mean value from previous step into the equation along with the minimum pixel value of the background.
But, how do I alter the mean of grayscale image in foreground such that it could be 20%,40%,60%,80%,100% of intensity difference percentage?I am sorry because I am not understand about intensity difference so much.What do you mean by "get less resolution", because noise will be added after the intensity difference percentage applied.Could you explain more on both your code and how to get less resolution? Thank you.
With comments:
% Find the min gray level in the image containing the A and the background.
% The minimum gray level is assumed to be the background.
backgroundGrayLevel = min(grayImage(:))
% Create an image that is the same size as the original image but
% has the background gray level everywhere.
backgroundImage = backgroundGrayLevel * ones(size(grayImage));
% Find the letter. This will be pixels that have a gray level brighter
% than the background. This will be an logical image that is the same size
% as the original image but is true where there is a letter, and false
% where there is background.
foregroundMask = (grayImage ~= backgroundImage);
% Get a 1-D list of pixel values by passing in the mask as a logical
% matrix. Even though the mask is 2-D the list will be a 1-D vector. Then
% take the mean of all those pixels to get the mean gray level.
meanForegroundGrayLevel = mean(grayImage(foregroundMask))
% Compute the formula based on the mean of the non-background pixels, and
% the background pixels.
intensityDifferencePercent = (100/255) * (meanForegroundGrayLevel - backgroundGrayLevel)
The percentage is what it is. You can't force it to be something, unless you change the original input image of course.
If the image is just black and white, then the contrast will be 100%. If however the letter is not pure white, but has some pixels that are less than 255 due to anti-aliasing, then the mean gray level of the image will be less than 255. As the letter gets smaller, the proportion of pixels that are less than 255 will be greater, and as the image gets more resolution, the proportion of anti-aliased edge pixels will be a smaller proportion of the total letter and so the mean gray level will be closer to 255.
So that's it, in excrutiating detail.
Hi, Image Analyst. It is meant to alter the original input image pixels(for both background and foreground) such that it would be intensity difference percentage of 20% to 100%.
It can be done as long as the condition that foreground is brighter than background, just need to keep intensity difference as what I wish it to. It is not like what you done which is calculate the intensity difference percentage from the input image.
I am work in opposite way, which I set the intensity level and the image could convert to that intensity difference that I want it to.Thank you.Hope you could understand.
The input is the intensity difference percentage, while the output will the image. Is there any way to alter the related pixel inside the image,since it has two values currently which are 0 and 255.Sorry for being vague previously.
You can use rescale(). Just figure out what the upper limit intensity should be and call rescale. For example
highIntensity = 0.2 * 255; % 20%
outputImage = uint8(rescale(inputImage, 0, highIntensity));
Wow,I didn't think of this method, it is amazing.Thank you for your guidance.

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing and Computer Vision 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!