which way is better to calculate var of image with blockproc?

hi everyone I have an image and I want to use var,mean,skewnes,... of this image in a 3*3 block.I thought this code is correct:
function [ varianceImage ] = var3dar3( Img )
blockSize = [3 3];
varFilterFunction = @(theBlockStructure)var(double(theBlockStructure.data(:)));
blockyImagevar = blockproc(Img, blockSize, varFilterFunction);
varianceImage=blockyImagevar (:);
end
but when I want to test this code I decide to compare var with this code:
function [varianceImage] = GetvarianceGL(Img)
blockSize = [3 3];
varFilterFunction = @(theBlockStructure) varimg(double(theBlockStructure.data(:)));
blockyImagevar = blockproc(Img, blockSize, varFilterFunction);
varianceImage=blockyImagevar(:);
end
that in this code varimage is this:
function [varianceGL] = varimg(Img)
[pixelCounts,GLs] = imhist(Img); %Gray Levels (the intensity value) and the count of pixels in the image having the gray level.
imshow(imhist(Img))
% Get the number of pixels in the histogram.
numberOfPixels = sum(pixelCounts)
% Get the mean gray lavel.
meanGL = sum(GLs .* pixelCounts) / numberOfPixels
% Get the variance, which is the second central moment.
varianceGL = sum((GLs - meanGL) .^ 2 .* pixelCounts) / (numberOfPixels-1);
end
which one is correct var3dar3 or GetvarianceGL? why the result of var is diffrent between manual way and ready way in matlab toolbox? I need manual way for my future works. Your help would be appreciated "

Answers (3)

In your first code you have
StDevFilterFunction = @(theBlockStructure) var(double(theBlockStructure.data(:)));
but notice you are taking var() there not std(). The rest of your code also thinks it is dealing with std, such as squaring the result to get the variance.

1 Comment

Excuse me...I change the std to var but the results of both code are different. Thanks for your attention

Sign in to comment.

The only reason I can see is if Img is not uint8, but maybe double or something. In that case, imhist() will not use 256 bins when creating the histogram and so the stats you get from varimg will be less accurate. Set a breakpoint there and check what class it is - uint8 or double.

16 Comments

That was something I thought of, but when I checked the default is 256 for grayscale and 2 for binary. (The function does not handle RGB images at all.)
thanks Image Analyst and Walter Roberson so you mean that if my image is rgb I should do this code on one of the channels? for example: Img=Img(:,:,1)
No, you are using
varimg(double(theBlockStructure.data(:)))
so you are passing in a vector of pixel components, not an 8 x 8 x 3 array. imhist() will just look to see whether vector is binary (such a logical()) and will default to 256 bins when it sees that it is not. But it would not hurt to specifically tell imhist() to use 256 bins.
But doing an histogram and then multiplying out by the level and summing the result: when you are using quantized values such as uint8, instead of continuous values, you can show that the result is the same as just summing the values.
Doing the histogram and multiplying by the value is like taking 5+2+2+5+5+5+5+5+2+2 and saying "okay you have four 2's and six 5's so the total is 4*2+6*5 = 8+30 = 38". You get exactly the same result by just adding them all together.
Using a histogram is much less efficient and should be reserved for the case where you need to quantize the values because the inputs were continuous, like if you had 5.03+1.99+2+5+4.87 and so on, and wanted to establish ranges of values that were each to be treated as if they were the nominal value. And even then, if you are quantizing to integers you should consider using sum(round(x)) or sum(floor(x)) instead of going through the histogram. When the bin width is W,
sum(W*floor(x/W))
instead of going through a histogram. Or sum(W*round(x/W)) if you want to deal with bin centers rather than bin edges.
sara, is your image RGB or grayscale - it's not clear to me. Why would you want to do this on an RGB image anyway? What is the class and dimension/size of the image you're using?
my orignal image is 512*512*3 image.it is ct scan. but after some pre proccesing that I did it is Name Size Bytes Class Attributes
maskedImage 299x443 132457 uint8
I want to do my feature extraction on this image...thanks
OK, so it's a regular uint8 gray scale image. Please attach your full test script so we can run it.
var3dar3 and GetvarianceGL and varimg are my functions... if you mean something else please tell me...thanks
If those are the one, or three, m-files needed to run your program, then yes, those are the m-files that I mean for you to attach.
so do you think there is any differences between manual way and matlab toolbox way for obtain var?if no do you know why when I use these codes for one grayscale imag the results are different? thanks
I didn't see anything glaringly obvious at first glance, that's why we need your m-files to investigate further.
ok thanks, I attached it.I want to do feature extraction. for run this code you can do this: first run ImgCrop --->
enter an image--->demo\slicewithnud\84.tiff
please enter the number of pixeles that are center of noudle in this slice--->1
please enter Xnoudle--->355
please enter Ynoudle--->153
....... you can see in Imgcrop that var3dar3(aInput) and GetvarianceGL(aInput) have differences.I want to extract just var in 3 dar 3 block. thanks
And where did you attach them? I don't see them on your original post, or on your comment here. Are you sure you remembered to click the "Attach file" button after you clicked the "Choose file" button?
ok thanks, I attached it.I want to do feature extraction. for run this code you can do this: first run ImgCrop --->
enter an image--->demo\slicewithnud\84.tiff
please enter the number of pixeles that are center of noudle in this slice--->1
please enter Xnoudle--->355
please enter Ynoudle--->153
....... you can see in Imgcrop that var3dar3(aInput) and GetvarianceGL(aInput) have different result.I want to extract just var in 3 dar 3 block. thanks
I'm sorry.I thought that the file was attached.
Can you guide me, please?

Sign in to comment.

This code calculates the variance of a RGB image but it's not standard variance, see below:
%------------------------------------------
close all;
clear all;
clc;
I = imread('b.png');
blockSize = [6 6];
varFilterFunction = @(theBlockStructure) var(double(theBlockStructure.data(:)));
blockyImagevar = blockproc(I, blockSize, varFilterFunction);
varianceImage=blockyImagevar(:);
display(varianceImage);
%---------------------------------------
Morteza Hajitabar Firuzjaei

Asked:

on 31 May 2015

Edited:

on 29 Jan 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!