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

Thread Subject:
Image filtering_help_request

Subject: Image filtering_help_request

From: doug kopet

Date: 21 Aug, 2010 00:00:22

Message: 1 of 29

Good day folks,

I have to create an image filter, I want to do the following tasks but have no clue how to even start , any help to at least start these 2 tasks are more than welcome:

1)First divide the input image into neighbouring square blocks of user-
defined size in order to calculate local statistics i.e. mean and standard deviation).These blocks are small blocks, for example, 5x5 pixels in size.

2) The mean and standard deviation of the unfiltered, original image are
calculated for each individual block based on pixel grey values, the resulting values being then assigned to the central cell in each block.


So my question is how to do this 'Image Division into Blocks' and also the computation of mean & standard deviation with assignment to the cental cell of the 5 x 5 block.

I have a test matrix I created (not sure if this is right type of image array example to create the image filter algorithm):

A = [105,109,104,108,107,101,107,110,105,105,106,102;106,100,104,106,100,105,106,103,105,102,108,106;...
       107,103,108,109,105,108,109,100,108,108,105,107;103,106,106,103,205,209,210,200,200,201,106,103;...
       105,105,110,100,103,202,210,202,205,208,108,105;105,102,108,102,100,208,203,208,205,210,109,105;....
       100,108,110,101,105,209,207,203,209,207,108,105;102,108,106,101,107,207,210,210,201,207,108,105;....
       109,107,103,101,100,104,107,107,101,105,104,104;108,104,110,103,107,109,101,101,107,106,100,107;....
       101,108,101,109,100,107,101,101,104,106,108,104;107,109,100,106,108,103,104,106,101,106,108, 210];

appreciate it
dougy

Subject: Image filtering_help_request

From: ImageAnalyst

Date: 21 Aug, 2010 00:33:01

Message: 2 of 29

dougy:
I think you're looking for the "blockproc()" function.

But just for fun, here's a version (which I've posted before) that
calculates the mean, standard deviation, and variance of images where
the sliding window moves along by one pixel instead of in jumps of
multiple pixels like blockproc:


% Demo to take the local mean, variance, and standard deviation
% of a gray scale image.
% userImage, if passed in, is used as the image.
% If userImage is not passed in, user is asked to use a demo image.
% Code written by ImageAnalyst
function local_variance(userImage)
% Clean up.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.

% Change the current folder to the folder of this m-file.
% (The line of code below is from Brett Shoelson of The Mathworks.)
% Don't use these lines if you're calling this from another m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end

% Initialize.
fontSize = 20;
if nargin == 0
    % No image passed in on the command line.
    % Read in one of the standard MATLAB demo images
    % as our original gray scale image and display it.
    promptMessage = sprintf('Which image do you want to use.\nThe
coins or the cameraman?');
    button = questdlg(promptMessage, 'Select Image', 'Coins',
'Cameraman', 'Coins');
    if strcmp(button, 'Coins')
        grayImage = double(imread('coins.png')); % Cast to double.
    else
        grayImage = double(imread('cameraman.tif')); % Cast to double.
    end
else
    % Use the image array passed in on the command line.
    grayImage = double(userImage); % Cast to double.
end

% Start timing.
startTime = tic;

subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.

% Blur the image with a 5 by 5 averaging (box filter) window.
blurredImage = conv2(grayImage, ones(5,5)/25);
subplot(2, 2, 2);
imshow(blurredImage, []);
title('Blurred Image', 'FontSize', fontSize);

% Perform a variance filter.
% Output image is the variance of the input image in a 3 by 3 sliding
window.
VarianceFilterFunction = @(x) var(x(:));
varianceImage = nlfilter(grayImage, [3 3], VarianceFilterFunction);
% An alternate way of doing the variance filter is on the next line:
% varianceImage = reshape(std(im2col(originalImage,[3 3],'sliding')),
size(originalImage)-2);
subplot(2, 2, 3);
imshow(varianceImage, [])
title('Variance Image', 'FontSize', fontSize);

% Compute the square root of the variance image to get the standard
deviation.
standardDeviationImage = sqrt(varianceImage);
subplot(2, 2, 4);
imshow(standardDeviationImage, [])
title('Standard Deviation Image', 'FontSize', fontSize);
elapsedTime = toc(startTime);

message = sprintf('Done!\n\nElapsed time = %.2f seconds.',
elapsedTime);
msgbox(message);
return; % End of local_variance() function.

Subject: Image filtering_help_request

From: doug kopet

Date: 21 Aug, 2010 00:44:04

Message: 3 of 29

thanks for the start.
doug

Subject: Image filtering_help_request

From: doug kopet

Date: 21 Aug, 2010 03:26:05

Message: 4 of 29

ImageAnalyst,

I ask for some more help my friend.

The mean and standard deviation of the unfiltered,original imagel are
calculated for each individual block based on pixel grey values. I used a 3x3 block.

The next part asks the resulting values be assigned to the central cell in each block. And the corresponding values for all other cells in the block are then calculated from this central cell by bilinear interpolation.

In this way, each individual pixel is assigned its own initial local mean and standard deviation based on surrounding pixel values.

Send a hint my way if you can.

Below is what I have so far

%==============================================
clc;
clear all;
close all;

A = [ 1 3 4 5 4 7 ;
         4 7 8 10 11 13;
         6 9 2 1 5 8]
     
  fun1 = @(block_struct) mean2(block_struct.data) * ones(size(block_struct.data));
 I1 = blockproc(A,[3 3],fun1)

  fun2 = @(block_struct) std2(block_struct.data) * ones(size(block_struct.data));
 I2 = blockproc(A,[3 3],fun2)

%==============================================

doug

Subject: Image filtering_help_request

From: doug kopet

Date: 21 Aug, 2010 15:08:04

Message: 5 of 29

Hey, can anyone suggest how to use the bilinear interplation as described in my last post? I only see its use in matlab w/ imresize

dougy

Subject: Image filtering_help_request

From: ImageAnalyst

Date: 21 Aug, 2010 22:34:59

Message: 6 of 29

On Aug 21, 11:08 am, "doug kopet" <onedo...@gmail.com> wrote:
> Hey, can anyone suggest how to use the bilinear interplation as described in my last post? I only see its use in matlab w/ imresize
>
> dougy
-------------------------------------------------------------------------------------------

dougy,
Seems like a lot of trouble to go to when you could have just used
imfilter or nlfilter and done it at every pixel instead of doing it
every Nth pixel and then having to come up with the "in between"
values by interpolating. But yes, one way to interpolate is to
subsample the full-sized, yet blocky looking image returned by
blobkproc and then run it through imresize to get back up to the full
size. This, by the way, will not be as accurate as just running
nlfilter on your original image since the interpolations are just
guesses, not the actual true values that would be there.

A second way is to use interp2().

A third way is to just use regular math and the intuitive formulas for
bilinear interpolation, like I'm sure you could come up with if you
thought of it a bit. Basically, figure out what percentage of the way
you are from left to right, and then get the weighted average of the
left pixels and the right pixels and figure out what percentage you
need to take in between them. Then basically do it again in the
vertical direction. I'm sure you can get the formulas quickly in a
web search (http://en.wikipedia.org/wiki/Bilinear_interpolation) if
you don't want to think up the intuitive way yourself.

ImageAnalyst

Subject: Image filtering_help_request

From: doug kopet

Date: 22 Aug, 2010 03:16:05

Message: 7 of 29

ImageAnalyst ,

In the code you posted for pixel by pixel local statistics, What part of the code computes the mean? Also, is the blurring computation in the code needed for my application?

thank
dougy

Subject: Image filtering_help_request

From: doug kopet

Date: 22 Aug, 2010 03:19:04

Message: 8 of 29

I have a further question to add , in the code it states:

% Perform a variance filter.
% Output image is the variance of the input image in a 3 by 3 sliding
% window.


If I want to do pixel by pixel will it mean that I'll have to employ a 1 by 1 sliding?

dougy

Subject: Image filtering_help_request

From: ImageAnalyst

Date: 22 Aug, 2010 03:19:55

Message: 9 of 29

This is where the mean is calculated:
% Blur the image with a 5 by 5 averaging (box filter) window.
blurredImage = conv2(grayImage, ones(5,5)/25);
This slides along a 5 by 5 window, one pixel at a time, and sets the
output image's pixel at the center of the window equal to the mean of
the 25 pixels in the window in the input image.

The code I gave you does what you asked, as far as I can tell.

Subject: Image filtering_help_request

From: ImageAnalyst

Date: 22 Aug, 2010 03:26:18

Message: 10 of 29

On Aug 21, 11:19 pm, "doug kopet" <onedo...@gmail.com> wrote:
> I have a further question to add , in the code it states:
>
> % Perform a variance filter.
> % Output image is the variance of the input image in a 3 by 3 sliding
> % window.
>
> If I want to do pixel by pixel will it mean that I'll have to employ a 1 by 1 sliding?
>
> dougy

---------------------------------------------------------------------
I have no idea what this means. You move a window of some size, like
3x3 or 5x5, by an increment, say 1 pixel. If your window size is 1
(if that is what you mean by "do pixel by pixel" then the mean is
simply the pixel value and the variance and standard deviation are
both zero.

However, blockproc moves in "jumps" of the window size, and assigns
THE WHOLE WINDOW of the output image with the value calculated at that
position. That's why it looks blocky as compared to other routines
that slide along a pixel at a time, such as conv2(), imfilter(), and
nlfilter().

If I still haven't answered your question, try to rephrase your
question.

Subject: Image filtering_help_request

From: doug kopet

Date: 22 Aug, 2010 03:44:06

Message: 11 of 29


Please have alook at the 'MeanFilterFunction' in the code below, can I use this instead of the blurring?

Thanks for the code. I think for every 3x3 block in image array A the mean and std dev. is computed for each individual pixel in that block.

Just confirm with me

thank you
dougy.

%==============================================
clc;
clear all;
close all;


 
A = [ 1 3 4 5 4 7 ;
         4 7 8 10 11 13;
         6 9 2 1 5 8]
     
VarianceFilterFunction = @(x) var(x(:));
varianceImage = nlfilter(A, [3 3], VarianceFilterFunction);
standardDeviationImage = sqrt(varianceImage)

MeanFilterFunction = @(x) mean2(x(:));
meanImage = nlfilter(A, [3 3], MeanFilterFunction)
%==============================================

Subject: Image filtering_help_request

From: ImageAnalyst

Date: 22 Aug, 2010 13:31:12

Message: 12 of 29

Yes, you can do it that way, although for the mean, it certainly is
not as efficient as conv2() because it doesn't take into account the
extra efficiency built in to conv2(), where, when you slide over the
window by one column, the sum stays largely the same except for the
first and last columns which got shifted out and in, respectively, so
you have to just add/subtract only those pixels and not ALL of them.

Subject: Image filtering_help_request

From: doug kopet

Date: 22 Aug, 2010 15:17:05

Message: 13 of 29

Hi, yes.thanks for the advice.

With the code,and a block of say [400 400] and an image that can be found at:

http://img690.imageshack.us/i/dougyspic.png/

Please have a look and suggest how can I make the code faster.

thanks
dougy

Subject: Image filtering_help_request

From: Image Analyst

Date: 22 Aug, 2010 15:43:05

Message: 14 of 29

I thought I already did that.

Anyway for normal sized images, these should take just a fraction of a second or maybe a few seconds at most.

What kinds of speeds (processing times) are you getting, and what kinds of speed do you require, and why do you require those speeds?

Subject: Image filtering_help_request

From: doug kopet

Date: 22 Aug, 2010 16:18:05

Message: 15 of 29

"Image Analyst" <imageanalyst@mailinator.com> wrote in message
> What kinds of speeds (processing times) are you getting, and what kinds of speed do you require, and why do you require those speeds?

Speeds are in excess of 20mins on my pc. Reasonable speeds around 2-5min max are desired. i require these speeds for doing practically-live demos at research group meetings.


thanks for helping
doug

Subject: Image filtering_help_request

From: ImageAnalyst

Date: 22 Aug, 2010 17:05:26

Message: 16 of 29

Strange. I can do it on a fairly large image (4000 pixels by 5000
pixels) in about 2 seconds using the efficient conv2():
tic
a=uint8(randi(255, [4000,5000]));
b=conv2(a, ones(3,3)/9, 'same');
toc

Elapsed time is 2.137655 seconds.

And I can do it with your image in 0.2 seconds:
tic
rgbImage=imread('C:\Documents and Settings\user\My Documents\Temporary
stuff\dougyspic.png');
grayImage = rgb2gray(rgbImage);
b=conv2(grayImage, ones(3,3)/9, 'same');
toc
Elapsed time is 0.229556 seconds.

With your less efficient nlfilter code, I'm getting times of 14
seconds and 7.5 seconds, quite a bit slower but still reasonable.

Your image is only 457 by 493 so doing it in block sizes or window
sizes of 400 doesn't make sense. How large are your images really?
And why would you need such a large window size? The window is so
large that it's essentially the same as the mean or variance of the
whole image.

Subject: Image filtering_help_request

From: dale

Date: 22 Aug, 2010 17:34:05

Message: 17 of 29

These are the actual images to be honest with you.
Are you familiar with he Wallis filter? this is what I'm trying to create. If you're aware of any available code implementation for this, send some help my way.

I tried your conv2 suggestion on a smaller image.
So , I can see conv2 is equivalent to the MeanFilterfunction.

Is there an equivalent version of conv2 for the StandardDeviationFunction?

dougy

Subject: Image filtering_help_request

From: ImageAnalyst

Date: 22 Aug, 2010 18:09:05

Message: 18 of 29

dougy:
I don't know if there's a comparable function built in to MATLAB, but
I don't think so. If those are your actual images, I don't know why
(1) it's taking 20 minutes for you while only a few seconds for me,
and (2) why you're using such a huge window - almost as big as the
image itself.

I do know what the Wallis filter is. For those who don't, it's
basically an edge detection filter and how it works is by scanning the
image with a window to make each window have the same mean and
standard deviation, with some constraints. This has the effect of
enhancing small details in the image.

I do have code for the Wallis filter that was pretty efficient. It's
written in Java (for ImageJ) and is based on Hierarchical Discrete
Correlation (HDC). I haven't ported it over to MATLAB code. Maybe I
should because the Wallis Filter can be useful in some situations.

Subject: Image filtering_help_request

From: doug kopet

Date: 22 Aug, 2010 18:32:06

Message: 19 of 29

I'm not familiar with Java. Anyhelp with it, even if you can send me the java code would help. hope its not difficult to port.

doug

Subject: Image filtering_help_request

From: ImageAnalyst

Date: 22 Aug, 2010 19:08:14

Message: 20 of 29

On Aug 22, 2:32 pm, "doug kopet" <onedo...@gmail.com> wrote:
> I'm not familiar with Java. Anyhelp with it, even if you can send me the java code would help. hope its not difficult to port.
>
> doug

I used http://www.sendthisfile.com to send it to your email address.

Subject: Image filtering_help_request

From: doug kopet

Date: 22 Aug, 2010 19:38:05

Message: 21 of 29

Imaganalyst,

can you send it to :

persad34@gmail.com

thanks again
dougy

Subject: Image filtering_help_request

From: doug kopet

Date: 22 Aug, 2010 22:21:05

Message: 22 of 29


You can you send it to :
 http://www.sendthisfile.com

to this email address:

                           persad34@gmail.com

> thanks
> dougy

Subject: Image filtering_help_request

From: doug kopet

Date: 22 Aug, 2010 22:53:07

Message: 23 of 29


Hi,

you can send it at this link

https://www.sendthisfile.com/f.jsp?id=SoL7XMSvhcrwyQ8AsHSu2qoM

.....
dougy

Subject: Image filtering_help_request

From: ImageAnalyst

Date: 22 Aug, 2010 23:10:16

Message: 24 of 29

Why. Can't you get it at your first email?

Subject: Image filtering_help_request

From: doug kopet

Date: 23 Aug, 2010 02:05:24

Message: 25 of 29

my email 'onedougk' is messed up and unreliable. Its been full to the brink for several months now as I store my research work there and when i attempt clear something space to send stuff or receive stuff, sometimes send ,sometimes don't.



For instance, i didn't receive the email from sendthisfile.com, so please ,take a minute and send it to my other email directly, i.e. persad34@gmail.com.

or use sendthisfile.com with that email.PS: I've never used sendthisfile.com
before, I just registered. Hope this isn't an issue.

thanks mate
dougy

Subject: Image filtering_help_request

From: doug kopet

Date: 23 Aug, 2010 16:03:06

Message: 26 of 29

Hello ImageAnalyst,

Will you send me it or not? I can send you the filter I have created and you can have a look if you want since you're aware of how the filter works. At this stage I'm not clear if my filter is the correct and complete thing. It works fast on 200 x 200 images.

let me you if you're willing to help me out

thanks
dougy

Subject: Image filtering_help_request

From: ImageAnalyst

Date: 23 Aug, 2010 19:47:45

Message: 27 of 29

On Aug 23, 12:03 pm, "doug kopet" <onedougk@gmail.com> wrote:
> Hello ImageAnalyst,
>
> Will you send me it or not? I can send you the filter I have created and you can have a look if you want since you're aware of how the filter works. At this stage I'm not clear if my filter is the correct and complete thing. It works fast on 200 x 200 images.
>
> let me you if you're willing to help me out
>
> thanks
> dougy
-----------------------------------------------------
I sent you the Wallis code to your second email.

Subject: Image filtering_help_request

From: mahdi Esfahanian

Date: 9 Oct, 2010 19:58:03

Message: 28 of 29

Dear ImageAnalyst,

I have a project about Adaptive Image Filtering. Could you introduce me several adaptive methods of adaptive filtering such as Wallis filter? I appreciate you if you could send me the Wallis codes at mahdi1982ir@yahoo.com.

Thanks a lot,

Mahdi


ImageAnalyst <imageanalyst@mailinator.com> wrote in message <64a5c9eb-0cc2-4704-bbf1-95a4b74a694b@v8g2000yqe.googlegroups.com>...
> On Aug 23, 12:03 pm, "doug kopet" <onedougk@gmail.com> wrote:
> > Hello ImageAnalyst,
> >
> > Will you send me it or not? I can send you the filter I have created and you can have a look if you want since you're aware of how the filter works. At this stage I'm not clear if my filter is the correct and complete thing. It works fast on 200 x 200 images.
> >
> > let me you if you're willing to help me out
> >
> > thanks
> > dougy
> -----------------------------------------------------
> I sent you the Wallis code to your second email.

Subject: Image filtering_help_request

From: Chi Chung

Date: 24 Feb, 2013 05:43:12

Message: 29 of 29

"mahdi Esfahanian" <mahdi1982ir@yahoo.com> wrote in message <i8qhgb$1ff$1@fred.mathworks.com>...
> Dear ImageAnalyst,
>
> I have a project about Adaptive Image Filtering. Could you introduce me several adaptive methods of adaptive filtering such as Wallis filter? I appreciate you if you could send me the Wallis codes at mahdi1982ir@yahoo.com.
>
> Thanks a lot,
>
> Mahdi
>
>
> ImageAnalyst <imageanalyst@mailinator.com> wrote in message <64a5c9eb-0cc2-4704-bbf1-95a4b74a694b@v8g2000yqe.googlegroups.com>...
> > On Aug 23, 12:03 pm, "doug kopet" <onedougk@gmail.com> wrote:
> > > Hello ImageAnalyst,
> > >
> > > Will you send me it or not? I can send you the filter I have created and you can have a look if you want since you're aware of how the filter works. At this stage I'm not clear if my filter is the correct and complete thing. It works fast on 200 x 200 images.
> > >
> > > let me you if you're willing to help me out
> > >
> > > thanks
> > > dougy
> > -----------------------------------------------------
> > I sent you the Wallis code to your second email.

Can send to my email : chichungho12@gmail.com

I need your help for my final year project :/ please.please please

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us