图像处理中的census变换

    Census变换是一种非参数的局部变换(即依赖于局部强度值的相对顺序,而不是强度值本身)。census变换用于图像处理中,将方形窗口内的像素的强度值映射到一个比特串,从而捕捉图像的结构。中心像素的强度值将被这个比特串所代替,这个比特串由一组布尔值的比较得到,例如,在一个正方形窗口中,从左边移动到右边。

    if(当前像素强度值<中心像素强度值)布尔型 bit=0

    else 布尔型 bit=1

对于每一个比较,比特串被移动到左边。对于3*3大小的census窗口,形成一个8位的比特的串;对于5*5大小的census窗口,形成一个32位的比特串。


优势:

   减少由相机的增益和偏置引起的变化的影响。

    对于接近深度不连续区域的外点,提高其鲁棒性。

    编码局部空间结构。

    宽容的派性(如果局部领域的少数像素和大多数的像素比较,有一个非常不同的强度分布, 只有涉及到这些少数像素的比较受影响)。

    它可以区分旋转和反射。


缺点:

   与像素相关联的信息的丢失。

    随着窗口的大小增加,变量的大小的要求也增加了。

    对于一个3*3大小的census窗口,用于存储census值的大小为23,或者8位;对于一个5*5大小的census窗口,用于存储census值的大小为25,或者32位。


例子:


[转载]zhuan <wbr>census变换           

Sample Image-Cones

[转载]zhuan <wbr>census变换      

Census Transform (3×3)

[转载]zhuan <wbr>census变换

Census Transform (5×5)

MATLAB Code
*************************************************************************
Title: Function-Census Transform of given Image
Author: Siddhant Ahuja
Created: May 2008
Copyright Siddhant Ahuja, 2008
Inputs: Image (var: inputImage), Window size assuming square window (var:
windowSize) of 3x3 or 5x5 only.
Outputs: Census Tranformed Image (var: censusTransformedImage), 
Time taken (var: timeTaken)
Example Usage of Function: [a,b]=funcCensusOneImage('Img.png', 3)
*************************************************************************
function [censusTransformedImage, timeTaken] funcCensusOneImage(inputImage, windowSize)
Grab the image information (metadata) using the function imfinfo
try 
    imageInfo=imfinfo(inputImage);
    Since Census Transform is applied on grayscale image, determine if the
    input image is already in grayscale or color
    if(getfield(imageInfo,'ColorType')=='truecolor')
    Read an image using imread function, convert from RGB color space to
    grayscale using rgb2gray function and assign it to variable inputImage
        inputImage=rgb2gray(imread(inputImage));
    else if(getfield(imageInfo,'ColorType')=='grayscale')
    If the image is already in grayscale, then just read it.        
            inputImage=imread(inputImage);
        else
            error('The Color Type of Input Image is not acceptable. Acceptable color types are truecolor or grayscale.');
        end
    end
catch
    inputImage=inputImage;
end
Find the size (columns and rows) of the image and assign the rows to
variable nr, and columns to variable nc
[nr,nc] size(inputImage);
Check the size of window to see if it is an odd number.
if (mod(windowSize,2)==0)
    error('The window size must be an odd number.');
end
if (windowSize==3)
    bits=uint8(0);
Create an image of size nr and nc, fill it with zeros and assign
it to variable censusTransformedImage of type uint8
    censusTransformedImage=uint8(zeros(nr,nc));
else if (windowSize==5)
        bits=uint32(0);
Create an image of size nr and nc, fill it with zeros and assign
it to variable censusTransformedImage of type uint32        
        censusTransformedImage=uint32(zeros(nr,nc));
    else
        error('The size of the window is not acceptable. Just 3x3 and 5x5 windows are acceptable.');
    end
end
Initialize the timer to calculate the time consumed.
tic;
Find out how many rows and columns are to the left/right/up/down of the
central pixel
C= (windowSize-1)/2;
for(j=C+1:1:nc-C) Go through all the columns in an image (minus at the borders)
    for(i=C+1:1:nr-C) Go through all the rows in an image (minus at the borders)  
       census 0; Initialize default census to 0
        for (a=-C:1:C) Within the square window, go through all the rows
            for (b=-C:1:C) Within the square window, go through all the columns
                if (~(a==C+1 && b==C+1)) Exclude the centre pixel from the calculation
                    census=bitshift(census,1); %Shift the bits to the left  by 1
                    If the intensity of the neighboring pixel is less than
                    that of the central pixel, then add one to the bit
                    string
                    if (inputImage(i+a,j+b) inputImage(i,j))
                        census=census+1;
                    end
                end
            end
        end
        Assign the census bit string value to the pixel in imgTemp
        censusTransformedImage(i,j) census;
    end
end
Stop the timer to calculate the time consumed.
timeTaken=toc;

In order to run the above code, you should have the image processing toolbox installed in MATLAB.



[转载]zhuan <wbr>census变换           

Sample Image-Cones

[转载]zhuan <wbr>census变换      

Census Transform (3×3)

[转载]zhuan <wbr>census变换

Census Transform (5×5)

MATLAB Code

*************************************************************************
Title: Function-Census Transform of given Image
Author: Siddhant Ahuja
Created: May 2008
Copyright Siddhant Ahuja, 2008
Inputs: Image (var: inputImage), Window size assuming square window (var:
windowSize) of 3x3 or 5x5 only.
Outputs: Census Tranformed Image (var: censusTransformedImage), 
Time taken (var: timeTaken)
Example Usage of Function: [a,b]=funcCensusOneImage('Img.png', 3)
*************************************************************************
function [censusTransformedImage, timeTaken] funcCensusOneImage(inputImage, windowSize)
Grab the image information (metadata) using the function imfinfo
try 
    imageInfo=imfinfo(inputImage);
    Since Census Transform is applied on grayscale image, determine if the
    input image is already in grayscale or color
    if(getfield(imageInfo,'ColorType')=='truecolor')
    Read an image using imread function, convert from RGB color space to
    grayscale using rgb2gray function and assign it to variable inputImage
        inputImage=rgb2gray(imread(inputImage));
    else if(getfield(imageInfo,'ColorType')=='grayscale')
    If the image is already in grayscale, then just read it.        
            inputImage=imread(inputImage);
        else
            error('The Color Type of Input Image is not acceptable. Acceptable color types are truecolor or grayscale.');
        end
    end
catch
    inputImage=inputImage;
end
Find the size (columns and rows) of the image and assign the rows to
variable nr, and columns to variable nc
[nr,nc] size(inputImage);
Check the size of window to see if it is an odd number.
if (mod(windowSize,2)==0)
    error('The window size must be an odd number.');
end
if (windowSize==3)
    bits=uint8(0);
Create an image of size nr and nc, fill it with zeros and assign
it to variable censusTransformedImage of type uint8
    censusTransformedImage=uint8(zeros(nr,nc));
else if (windowSize==5)
        bits=uint32(0);
Create an image of size nr and nc, fill it with zeros and assign
it to variable censusTransformedImage of type uint32        
        censusTransformedImage=uint32(zeros(nr,nc));
    else
        error('The size of the window is not acceptable. Just 3x3 and 5x5 windows are acceptable.');
    end
end
Initialize the timer to calculate the time consumed.
tic;
Find out how many rows and columns are to the left/right/up/down of the
central pixel
C= (windowSize-1)/2;
for(j=C+1:1:nc-C) Go through all the columns in an image (minus at the borders)
    for(i=C+1:1:nr-C) Go through all the rows in an image (minus at the borders)  
       census 0; Initialize default census to 0
        for (a=-C:1:C) Within the square window, go through all the rows
            for (b=-C:1:C) Within the square window, go through all the columns
                if (~(a==C+1 && b==C+1)) Exclude the centre pixel from the calculation
                    census=bitshift(census,1); %Shift the bits to the left  by 1
                    If the intensity of the neighboring pixel is less than
                    that of the central pixel, then add one to the bit
                    string
                    if (inputImage(i+a,j+b) inputImage(i,j))
                        census=census+1;
                    end
                end
            end
        end
        Assign the census bit string value to the pixel in imgTemp
        censusTransformedImage(i,j) census;
    end
end
Stop the timer to calculate the time consumed.
timeTaken=toc;

In order to run the above code, you should have the image processing toolbox installed in MATLAB.

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值