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 "
回答 (3 件)
Walter Roberson
2015 年 5 月 31 日
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.
Image Analyst
2015 年 5 月 31 日
0 投票
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 件のコメント
Walter Roberson
2015 年 5 月 31 日
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.)
sara
2015 年 5 月 31 日
Walter Roberson
2015 年 5 月 31 日
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.
Walter Roberson
2015 年 5 月 31 日
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.
Image Analyst
2015 年 5 月 31 日
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?
sara
2015 年 6 月 1 日
Image Analyst
2015 年 6 月 1 日
OK, so it's a regular uint8 gray scale image. Please attach your full test script so we can run it.
sara
2015 年 6 月 1 日
編集済み: Image Analyst
2015 年 6 月 1 日
Image Analyst
2015 年 6 月 1 日
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.
sara
2015 年 6 月 2 日
Image Analyst
2015 年 6 月 2 日
I didn't see anything glaringly obvious at first glance, that's why we need your m-files to investigate further.
sara
2015 年 6 月 2 日
Image Analyst
2015 年 6 月 2 日
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?
sara
2015 年 6 月 3 日
sara
2015 年 6 月 4 日
Morteza Hajitabar Firuzjaei
2018 年 1 月 29 日
編集済み: Walter Roberson
2018 年 1 月 29 日
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
カテゴリ
ヘルプ センター および File Exchange で Blocked Images についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!