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

1 回表示 (過去 30 日間)
sara
sara 2015 年 5 月 31 日
編集済み: Walter Roberson 2018 年 1 月 29 日
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
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.
  1 件のコメント
sara
sara 2015 年 5 月 31 日
Excuse me...I change the std to var but the results of both code are different. Thanks for your attention

サインインしてコメントする。


Image Analyst
Image Analyst 2015 年 5 月 31 日
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 件のコメント
sara
sara 2015 年 6 月 3 日
I'm sorry.I thought that the file was attached.
sara
sara 2015 年 6 月 4 日
Can you guide me, please?

サインインしてコメントする。


Morteza Hajitabar Firuzjaei
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

カテゴリ

Help Center および File ExchangeImages についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by