一、Census Transform(CT)算法的学习
    Census Transform 算法是Ramin Zabih 和 John Woodfill 于1994年在他们的论文 《Non-parametric LocalTransforms for Computing VisualCorrespondence》中提出的,正如他们在论文中所说,这是一种非参数变换,主要用来表征图像的局部结构特征,能够比较好的检测到图像中的边缘特征和角点特征,从这篇论文的470次的引用次数来看,CT算法用处还是挺广泛的。下面简要介绍一下CT算法的基本思想:用一个3*3或者5*5的滑动窗口遍历整幅图像,对于每次遍历的位置,以3*3为例,假设某个位置如下图所示


123 127 129
126 128 129
127 131 130
然后比较此窗口中每个像素值(中心除外)与中心像素值的大小,如果比中心像素值小,则比较结果为1,否则为0。由此,得到如下结果:

1 1 0
1   0
1 0 0

然后,把此窗口结果组成一个序列:11010100,以此二进制序列表示的值来代替原图像窗口中心点的像素。如此下去,等到窗口滑动完整幅图像,我们就得到原图像做统计变换(CT)之后的图像。

注意:只能作用于灰度图像,对于彩色图像,则需要转换为灰度图之后再操作;为了简单起见,我没有考虑图像的边缘像素值。
  下面给出一种用 matlab 实现的版本:

[plain]  view plain copy

在CODE上查看代码片 派生到我的代码片

  1. % *************************************************************************
  2. % Title: Function-Census Transform of a given Image
  3. % Author: Siddhant Ahuja
  4. % Created: May 2008
  5. % Copyright Siddhant Ahuja, 2008
  6. % Inputs: Image (var: inputImage), Window size assuming square window (var:
  7. % windowSize) of 3x3 or 5x5 only.
  8. % Outputs: Census Tranformed Image (var: censusTransformedImage),
  9. % Time taken (var: timeTaken)
  10. % Example Usage of Function: [a,b]=funcCensusOneImage('Img.png', 3)
  11. % *************************************************************************
  12. function [censusTransformedImage, timeTaken] = funcCensusOneImage(inputImage, windowSize)
  13. % Grab the image information (metadata) using the function imfinfo
  14. try
  15. imageInfo = imfinfo(inputImage);
  16. % Since Census Transform is applied on a grayscale image, determine if the
  17. % input image is already in grayscale or color
  18. if(getfield(imageInfo,'ColorType')=='truecolor')
  19. % Read an image using imread function, convert from RGB color space to
  20. % grayscale using rgb2gray function and assign it to variable inputImage
  21. inputImage=rgb2gray(imread(inputImage));
  22. else if(getfield(imageInfo,'ColorType')=='grayscale')
  23. % If the image is already in grayscale, then just read it.
  24. inputImage=imread(inputImage);
  25. else
  26. error('The Color Type of Input Image is not acceptable. Acceptable color types are truecolor or grayscale.');
  27. end
  28. end
  29. catch
  30. inputImage = inputImage;
  31. end
  32. % Find the size (columns and rows) of the image and assign the rows to
  33. % variable nr, and columns to variable nc
  34. [nr,nc] = size(inputImage);
  35. % Check the size of window to see if it is an odd number.
  36. if (mod(windowSize,2)==0)
  37. error('The window size must be an odd number.');
  38. end
  39. if (windowSize==3)
  40. bits=uint8(0);
  41. % Create an image of size nr and nc, fill it with zeros and assign
  42. % it to variable censusTransformedImage of type uint8
  43. censusTransformedImage=uint8(zeros(nr,nc));
  44. else if (windowSize==5)
  45. bits=uint32(0);
  46. % Create an image of size nr and nc, fill it with zeros and assign
  47. % it to variable censusTransformedImage of type uint32
  48. censusTransformedImage=uint32(zeros(nr,nc));
  49. else
  50. error('The size of the window is not acceptable. Just 3x3 and 5x5 windows are acceptable.');
  51. end
  52. end
  53. % Initialize the timer to calculate the time consumed.
  54. tic;
  55. % Find out how many rows and columns are to the left/right/up/down of the
  56. % central pixel
  57. C= (windowSize-1)/2;
  58. for j=C+1:1:nc-C % Go through all the columns in an image (minus C at the borders)
  59. for i=C+1:1:nr-C % Go through all the rows in an image (minus C at the borders)
  60. census = 0; % Initialize default census to 0
  61. for a=-C:1:C % Within the square window, go through all the rows
  62. for b=-C:1:C % Within the square window, go through all the columns
  63. if (~(a==0 && b==0)) % Exclude the centre pixel from the calculation,原来是(C+1),现改为0
  64. census=bitshift(census,1); %Shift the bits to the left  by 1
  65. % If the intensity of the neighboring pixel is less than
  66. % that of the central pixel, then add one to the bit
  67. % string
  68. if (inputImage(i+a,j+b) < inputImage(i,j))
  69. census=census+1;
  70. end
  71. end
  72. end
  73. end
  74. % Assign the census bit string value to the pixel in imgTemp
  75. censusTransformedImage(i,j) = census;
  76. end
  77. end
  78. % Stop the timer to calculate the time consumed.
  79. timeTaken=toc;
  这是我在网上找到的一个实现的版本,注释比较多,除去注释的话,真正代码没有50行,比较简单,相信大家都可以看的懂。
   之前讲了CT算法的实现,下面说一下 MCT 以及 RMCT的实现。

二、Modified Census Transform (MCT)算法
    MCT 算法是 CT 算法的一个修改版本,它是由Bernhard Froba 在做人脸检测的时候提出来的,在他2004年发表的论文《Face Detection with theModified Census Transform》中,Bernhard Froba将 CT算法中“滑动窗口中每个像素值与中心位置像素做比较”改为“滑动窗口中每个像素值与整个窗口中像素的均值做比较”,这样,原有的每个3*3的窗口可能产生256种序列(因为没有算中心像素),现在变为可能产生512种序列(其实全0和全1的序列表示的是同样的信息,可以排除一个),也就是做完MCT之后,图像的每个像素值的范围为0——511,这样就能够比较充分的利用3*3的核(至于为什么这么说,可以看看前面提到的那篇论文)。如此来计算的话,则前面例子产生的结果窗口应该为:

1 1 0
1 0 0
1 0 0

对应的二进制序列为:110100100。然后以此作为中心像素点的像素值,循环完毕之后便得到MCT之后的图像。需要注意的一点是,如果要使变换之后的图像得到显示,应该对像素值做一下归一化,使其在0——255之间。

三、Revised Modified Census Transform (RMCT)算法
    RMCT 算法其实又是对 MCT的又一次修改,它与 MCT的不同之处仅仅在于一个微小的△m,即:在滑动窗口像素均值上加上一个微小的变量△m=1或者2。其他都是完全一样的。
    下面附上这两种修改版统计变换的 C++代码,代码是我自己编的,是基于VS2008和OpenCV2.0的,仅供参考:
[cpp]  view plain copy

在CODE上查看代码片 派生到我的代码片

  1. #include "stdafx.h"
  2. #include "MCT.h"
  3. #include "highgui.h"
  4. MCT::MCT()
  5. {
  6. window_size = 0;
  7. }
  8. MCT::~MCT()
  9. {
  10. }
  11. voidMCT::ModifiedCensusTransform(IplImage *input_image, IplImage *mct_image,constintwindow_size,constintdelta )
  12. {
  13. CvSize image_size = cvGetSize(input_image);
  14. intimage_width = image_size.width;
  15. intimage_height = image_size.height;
  16. IplImage *gray_image = cvCreateImage(cvGetSize(input_image), input_image->depth, 1);
  17. cvSetZero(gray_image);
  18. if(input_image->nChannels != 1)
  19. {
  20. cvCvtColor(input_image, gray_image, CV_RGB2GRAY);
  21. }
  22. else
  23. {
  24. cvCopy(input_image, gray_image);
  25. }
  26. IplImage *modified_image = NULL;
  27. switch(window_size)
  28. {
  29. case3:
  30. modified_image = cvCreateImage(image_size, IPL_DEPTH_16U, 1);
  31. cvSetZero(modified_image);
  32. break;
  33. case5:
  34. modified_image = cvCreateImage(image_size, IPL_DEPTH_32S, 1);
  35. cvSetZero(modified_image);
  36. break;
  37. default:
  38. printf("window size must be 3 or 5!\n");
  39. exit(EXIT_FAILURE);
  40. }
  41. CvMat window;
  42. for(inti = 0; i < image_height - window_size; i++)
  43. {
  44. for(intj = 0; j < image_width - window_size; j++)
  45. {
  46. unsigned longcensus = 0;
  47. CvRect roi = cvRect(j, i, window_size, window_size);
  48. cvGetSubRect(gray_image, &window, roi);
  49. CvScalar m = cvAvg(&window, NULL);
  50. for(intw = 0; w < window_size; w++)
  51. {
  52. for(inth = 0; h < window_size; h++)
  53. {
  54. census = census << 1; //左移1位
  55. doubletempvalue = cvGetReal2D(&window, w, h);
  56. if(tempvalue < m.val[0] + delta)
  57. census += 1;
  58. }
  59. }
  60. cvSetReal2D(modified_image, i, j, census);
  61. }
  62. }
  63. //cvConvertScaleAbs(modified_image, mct_image, 1, 0);
  64. Normalize(modified_image, mct_image);
  65. cvReleaseImage(&gray_image);
  66. cvReleaseImage(&modified_image);
  67. }
  68. voidMCT::Normalize(IplImage *mct_image, IplImage *nor_image)
  69. {
  70. doubleminv, maxv;
  71. cvMinMaxLoc(mct_image, &minv, &maxv);
  72. for(inti = 0; i < mct_image->height; i++)
  73. {
  74. for(intj = 0; j < mct_image->width; j++)
  75. {
  76. doubletempv = cvGetReal2D(mct_image, i, j);
  77. tempv = (tempv - minv) / (maxv - minv) * 255;
  78. cvSetReal2D(nor_image, i, j, tempv);
  79. }
  80. }
  81. }

由于算法比较简单,所以没有写注释,应该比较容易理解。

四、CT/MCT/RMCT的应用
  他们主要用于人脸检测或者面部伪装检测,用于做图像的预处理,这可能是因为CT算法对光照不敏感,可以比较好的排除光照对图像的影响。这里给出用到该算法的几篇paper:
1、《Face Detection with the Modified CensusTransform》(前面提到的那篇)
2、《Adaboost Based Disguised Face Discrimination on EmbeddedDevices》
3、《Disguised-Face Discriminator for Embedded Systems》

CT/MCT/RMCT算法的学习和实现相关推荐

  1. 关于统计变换(CT/MCT/RMCT)算法的学习和实现

    原文地址http://blog.sina.com.cn/s/blog_684c8d630100turx.html 刚开会每周的例会,最讨厌开会了,不过为了能顺利毕业,只能忍了.闲话不多说了,下面把上周 ...

  2. 计算机视觉算法——Transformer学习笔记

    算机视觉算法--Transformer学习笔记 计算机视觉算法--Transformer学习笔记 1. Vision Transformer 1.1 网络结构 1.2 关键知识点 1.2.1 Self ...

  3. matlab中存档算法代码,MATLAB 智能算法超级学习手册中程序代码

    [实例简介] MATLAB 智能算法超级学习手册中程序代码 [实例截图] [核心代码] dc90ef43-7920-434e-bdb8-0636c31c0b44 └── MATLAB 智能算法超级学习 ...

  4. KMP算法的学习经验

    KMP算法的学习经验 (欢迎指正错误, 欢迎喷) 什么是kmp(完) kmp的额外知识(完) 暴力匹配的缺点,和代码实现(完) next[]数组的预先知识,了解前后缀,相同前后缀.(完) kmp的关键 ...

  5. 数据结构与算法深入学习_我最喜欢的免费课程,用于深入学习数据结构和算法...

    数据结构与算法深入学习 by javinpaul 由javinpaul Data structures and algorithms are some of the most essential to ...

  6. leetcode 刷500道题,笔试/面试稳吗?谈谈算法的学习

    来源公众号:苦逼的码农 作者:帅地 想要学习算法.应付笔试或者应付面试手撕算法题,相信大部分人都会去刷 Leetcode,有读者问?如果我在 leetcode 坚持刷它个 500 道题,以后笔试/面试 ...

  7. 从这十大算法开始学习机器学习与建模

    本文介绍了机器学习新手需要了解的 10 大算法,包括线性回归.Logistic 回归.朴素贝叶斯.K 近邻算法等. 在机器学习中,有一种叫做「没有免费的午餐」的定理.简而言之,它指出没有任何一种算法对 ...

  8. 【Python成长之路】机器学习:10+分类算法汇总学习

    一直都说python是人工智能.机器学习等算法的良配,很多python大神除了常规的大数据爬虫.网站开发等代码能力外,人工智能/机器学习也都是手到擒来.因此我也"跳坑"来看看 . ...

  9. KMP算法代学习之(二)代码深入学习

    前段时间学习了KMP算法,感觉略懂略懂!但算法这个东西理解一定要深入,不是略懂略懂就能敷衍过去的!真真做题的时候,才发现仅仅套模板是根本没用的,必须自己写得来代码,这样的话才能从细节,从根本上了解KM ...

最新文章

  1. 关于鼠标、键盘的几个例子
  2. 知识产权创业的比赛结果
  3. Flutter:Navigator2.0介绍及使用
  4. 一分钟了解阿里云产品:RDS概述
  5. python路线选择试题_python例题练习
  6. Spring高级之注解@PropertySource详解(超详细)
  7. 开发者编程时应该围着“程序”转吗?
  8. Unix网络编程 chart
  9. JavaScript:工具库MyTools.js(自用不断填充····)
  10. 如何输入多组字符串c语言,求教大侠:如何输入一组字符串
  11. ECshop 模板制作教程
  12. mac 安装commitizen插件报错Parsing JSON at /Users/lin/.czrc for commitizen config failed
  13. 庆祝鸿蒙指的是哪个生肖,12月中头彩,苦难转幸福,3生肖,鸿蒙紫气,运走上坡路,想啥就有啥...
  14. HTML5隐藏图片代码,HTML5终极备忘大全(图片版+文字版)
  15. windows虚机环境下,如何快速有效的删除大文件夹?
  16. Win10桌面图标无法拖动
  17. 蒸妙熏蒸,疏通身体的“堵”
  18. ajax无线遥控器,利用python+tkinter做一个简单的智能电视遥控器
  19. 「C#」Bitmap/Image.Save()报错“GDI+ 中发生一般性错误”的一个案例总结
  20. LM321 低功耗单运算放大器 1MHZ增益带宽积 用于充电器 适配器

热门文章

  1. 【Hive笔记】练习hive操作
  2. 服务器冷硬盘,浪潮SR整机柜冷存储方案助力BAT轻松实现海量存储
  3. (第二回合)回龙观大叔狂磕mysql
  4. mysql 安建分区_mysql表分区
  5. 关于无创DNA检测的真实性
  6. GPS参数中PDOP
  7. 金融统计分析与挖掘实战8.3-8.4
  8. php中的点号,PHP中逗号与点号有什么区别
  9. 2021年度友盟+ APP消息推送白皮书:工作日6-8点通勤时间消息送达率每日最高
  10. 阿里云云盘根目录扩容