how to average specific pixels on a 3x3 window and count the number of pixels present in it
13 views (last 30 days)
Show older comments
sir can u please send me code for this
- i am considering 3x3 size window
- i am taking the average of the pixels conditons are
- if the pixel values in the window is greater than 255 then i am averaging the pixels expect 255 value pixels
- if the pixel values are less than 255 value pixels then i am taking 255 as my answer
example lets take in a 3x3 window i have 5 number of 255 pixel value and 4 number less than 255 values then 255 > 254 or any other pixel value so i am keeping 255 as same if 255< 254 or any other pixel values then i am averaging all the pixels except 255 pixel value
3. after this i have to replace the pixel value by resultent value
anyone can write code for this please as soon as possible
sir I need to ignore high intensity pixel 255 and have to average all other pixels in the 3x3 window If all the pixels are white that is high intensity pixels 255 then I have keep it as 255 - sir here is the condition 1. While sliding a 3x3 window on image 2.if there are white(255) pixels are more than other pixels then I have to keep 255 and replace center pixel value by 255 3.if there are below (255) pixels are more inside that 3x3 window... Then I have to take average of below(255)pixels then have to take average of only below(255) pixels and replace as the center pixel value I have to take 255(white pixels) as center during sliding a 3x3 window I need a code for it please do help with loop I need because I have to take all 255(white)pixels in a image and have to change the value of each and every white pixel
Sir I have found out the pixel values using coordinates (x, y) = find(I==255) I got the each and every coorodintes and pixels of 255(white) from this now I need to apply averaging as I explained above for every pixel Please do help me with the code
8 Comments
Answers (2)
Image Analyst
on 30 May 2021
This code works:
% Demo by Image Analyst, May, 2021.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image.
folder = [];
baseFileName = 'street scene.png';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display the test image full size.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Original Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
hFig1.Name = 'Demo by Image Analyst';
%--------------------------------------------------------------------------------------------------------
% Separate image into color channels
[redChannel, greenChannel, blueChannel] = imsplit(rgbImage);
% Find the count image, sum image, and map of 255, and do the operations.
blurredImageR = AnalyzesingleColorChannel(redChannel);
blurredImageG = AnalyzesingleColorChannel(greenChannel);
blurredImageB = AnalyzesingleColorChannel(blueChannel);
% Display image.
subplot(2, 2, 2);
imshow(blurredImageR);
axis('on', 'image');
title('Processed Red Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hold on;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Display image.
subplot(2, 2, 3);
imshow(blurredImageG);
axis('on', 'image');
title('Processed Green Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hold on;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Display image.
subplot(2, 2, 4);
imshow(blurredImageB);
axis('on', 'image');
title('Processed Blue Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hold on;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
fprintf('Done running %s.m\n', mfilename);
function blurredImage = AnalyzesingleColorChannel(singleColorChannel)
% First set all 255 values to 0 so they won't be included in the count.
map255 = singleColorChannel == 255;
singleColorChannel(map255) = 0;
% Specify a blurring kernel.
windowWidth = 15; % Whatever...
kernel = ones(windowWidth, windowWidth) / windowWidth^2;
singleColorChannel = double(singleColorChannel); % Need to cast to double for conv2().
sumImage = conv2(singleColorChannel, kernel, 'same');
% Count the number of non-255 pixels at each window center location.
countImage = conv2(double(~map255), kernel, 'same');
% Divide the sum by the count to get the average.
blurredImage = sumImage ./ countImage;
% Make the 255 pixels 255.
blurredImage(map255) = 255;
% Optionally cast back to uint8
blurredImage = uint8(blurredImage);
end
5 Comments
Image Analyst
on 30 May 2021
No - they will need to be split apart but there are several color channels you could use. You could blur just the H or L channel if you convert to HSV or LAB color space.
Just concatenate them when you're done.
blurredRGBImage = cat(3, uint8(blurredImageR), uint8(blurredImageG), uint8(blurredImageB));
Image Analyst
on 30 May 2021
There is a paper we published over 30 years ago on an algorithm called Median Window Enhancement. Basically it did a sharpening filter with convolution. This caused ringing of course. So then we clipped off the ringing and kept the edges sharp by running a median window filter on the sharpened result. So now we got a sharp edge with no ringing. Then we iterated on that some number of times until we achieved the desired amount of sharpening.
See Also
Categories
Find more on Image Processing Toolbox 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!