MATLAB Answers

Nagaveni S A

Image smoothing & sharpening of a particular region in an image

Asked by Nagaveni S A
on 2 Feb 2013
 How to apply image smoothing & sharpening to only a particular region of an image?
I'm supposed to take a corrupted and noisy image. Select a particular region of interest. Then apply smoothing and/or sharpening only to that region using MATLAB codes. The code should be generalized i.e i should be able to apply that code to any image. Won't there be problem with selecting the threshold levels for smoothing and/or sharpening for each image, if we are supposed to generalize?

Can anyone give the MATLAB code for the above problem?


2 Answers

Answer by Image Analyst
on 2 Feb 2013
 Accepted answer

I'd just smooth the whole thing and then multiply by the mask defining the regions. There are more efficient methods, but for most situations this will be fine.

blurredImage = conv2(grayImage, ones(15)/15^2, 'same');
grayImage(maskImage) = blurredImage(maskImage);

I don't know what you want to do with thresholds. I guess that might affect how your blurred or sharpened image is created, but you don't say exactly so I can't tell.


Thank you for your solution. I meant threshold frequency of the low pass and high pass filters used for smoothing and sharpening in frequency domain. When we apply smoothing or sharpening to an image, the whole image gets smoothed or sharpened. This happens by setting a cut-off frequency for the whole image using a generalized formula in the code. But when we are needed to set cut-off/threshold frequency for a particular region of an image, we will have to change that frequency in the code every time we take a different image input, as the cut-off frequency may differ for different images in different regions. I didn't know how to solve that problem.

But i also didn't know about masking. I will try that. Thank you once again!!

Well if you're going to do frequency filtering, then you need to convert to Fourier space, then filter, then you'll have your blurred image. Here's my demo for that:

% 2D FFT Demo
clc;    % Clear the command window.
close all;  % Close all figures (except those of imtool.)
imtool close all;  % Close all imtool figures.
clear;  % Erase all existing variables.
workspace;  % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
% Change the current folder to the folder of this m-file.
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
	% User does not have the toolbox installed.
	message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
	reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
	if strcmpi(reply, 'No')
		% User said No, so exit.
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
	% File doesn't exist -- didn't find it there.  Check the search path for it.
	fullFileName = baseFileName; % No path this time.
	if ~exist(fullFileName, 'file')
		% Still didn't find it.  Alert user.
		errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
% Read in image.
grayImage = imread('cameraman.tif');
[rows columns numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
	grayImage = rgb2gray(grayImage);
% Display original grayscale image.
subplot(2, 2, 1);
title('Original Gray Scale Image', 'FontSize', fontSize)
% Perform 2D FFTs
fftOriginal = fft2(double(grayImage));
shiftedFFT = fftshift(fftOriginal);
subplot(2, 2, 2);
title('Real Part of Spectrum', 'FontSize', fontSize)
subplot(2, 2, 3);
title('Imaginary Part of Spectrum', 'FontSize', fontSize)
% Display magnitude and phase of 2D FFTs
subplot(2, 2, 4);
colormap gray
title('Log Magnitude of Spectrum', 'FontSize', fontSize)
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Now convolve with a 2D rect function.
rectWidth = 10;
rectHeight = 5;
kernel = ones(rectHeight, rectWidth) / (rectHeight * rectWidth);
% Display it
subplot(2, 2, 1);
k = padarray(kernel, [3, 3]); % Just for display.
imshow(k, []);
axis on;
title('Kernel', 'FontSize', fontSize)
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Convolve kernel (box filter) with the image
filteredImage = conv2(double(grayImage), kernel, 'same');
% Display filtered image.
subplot(2, 2, 2);
title('Filtered Image', 'FontSize', fontSize)
% Perform 2D FFT on the filtered image to see its spectrum.
% We expect to see a sinc multiplication effect.
% It should look like the original but with a sinc pattern overlaid on it.
fftFiltered = fft2(double(filteredImage));
shiftedFFT = fftshift(fftFiltered);
% Display magnitude of the 2D FFT of the filtered image.
subplot(2, 2, 3);
colormap gray
title('Log Magnitude of Spectrum - Note sinc multiplication', 'FontSize', fontSize)

Answer by Nagaveni S A
on 18 Feb 2013

The code has the following error: Undefined function or method 'new' for input arguments of type 'char'


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!