CT/MCT/RMCT算法的学习和实现
123 | 127 | 129 |
126 | 128 | 129 |
127 | 131 | 130 |
1 | 1 | 0 |
1 | 0 | |
1 | 0 | 0 |
然后,把此窗口结果组成一个序列:11010100,以此二进制序列表示的值来代替原图像窗口中心点的像素。如此下去,等到窗口滑动完整幅图像,我们就得到原图像做统计变换(CT)之后的图像。
- % *************************************************************************
- % Title: Function-Census Transform of a 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 a 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 C at the borders)
- for i=C+1:1:nr-C % Go through all the rows in an image (minus C 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==0 && b==0)) % Exclude the centre pixel from the calculation,原来是(C+1),现改为0
- 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;
1 | 1 | 0 |
1 | 0 | 0 |
1 | 0 | 0 |
对应的二进制序列为:110100100。然后以此作为中心像素点的像素值,循环完毕之后便得到MCT之后的图像。需要注意的一点是,如果要使变换之后的图像得到显示,应该对像素值做一下归一化,使其在0——255之间。
- #include "stdafx.h"
- #include "MCT.h"
- #include "highgui.h"
- MCT::MCT()
- {
- window_size = 0;
- }
- MCT::~MCT()
- {
- }
- voidMCT::ModifiedCensusTransform(IplImage *input_image, IplImage *mct_image,constintwindow_size,constintdelta )
- {
- CvSize image_size = cvGetSize(input_image);
- intimage_width = image_size.width;
- intimage_height = image_size.height;
- IplImage *gray_image = cvCreateImage(cvGetSize(input_image), input_image->depth, 1);
- cvSetZero(gray_image);
- if(input_image->nChannels != 1)
- {
- cvCvtColor(input_image, gray_image, CV_RGB2GRAY);
- }
- else
- {
- cvCopy(input_image, gray_image);
- }
- IplImage *modified_image = NULL;
- switch(window_size)
- {
- case3:
- modified_image = cvCreateImage(image_size, IPL_DEPTH_16U, 1);
- cvSetZero(modified_image);
- break;
- case5:
- modified_image = cvCreateImage(image_size, IPL_DEPTH_32S, 1);
- cvSetZero(modified_image);
- break;
- default:
- printf("window size must be 3 or 5!\n");
- exit(EXIT_FAILURE);
- }
- CvMat window;
- for(inti = 0; i < image_height - window_size; i++)
- {
- for(intj = 0; j < image_width - window_size; j++)
- {
- unsigned longcensus = 0;
- CvRect roi = cvRect(j, i, window_size, window_size);
- cvGetSubRect(gray_image, &window, roi);
- CvScalar m = cvAvg(&window, NULL);
- for(intw = 0; w < window_size; w++)
- {
- for(inth = 0; h < window_size; h++)
- {
- census = census << 1; //左移1位
- doubletempvalue = cvGetReal2D(&window, w, h);
- if(tempvalue < m.val[0] + delta)
- census += 1;
- }
- }
- cvSetReal2D(modified_image, i, j, census);
- }
- }
- //cvConvertScaleAbs(modified_image, mct_image, 1, 0);
- Normalize(modified_image, mct_image);
- cvReleaseImage(&gray_image);
- cvReleaseImage(&modified_image);
- }
- voidMCT::Normalize(IplImage *mct_image, IplImage *nor_image)
- {
- doubleminv, maxv;
- cvMinMaxLoc(mct_image, &minv, &maxv);
- for(inti = 0; i < mct_image->height; i++)
- {
- for(intj = 0; j < mct_image->width; j++)
- {
- doubletempv = cvGetReal2D(mct_image, i, j);
- tempv = (tempv - minv) / (maxv - minv) * 255;
- cvSetReal2D(nor_image, i, j, tempv);
- }
- }
- }
由于算法比较简单,所以没有写注释,应该比较容易理解。
CT/MCT/RMCT算法的学习和实现相关推荐
- 关于统计变换(CT/MCT/RMCT)算法的学习和实现
原文地址http://blog.sina.com.cn/s/blog_684c8d630100turx.html 刚开会每周的例会,最讨厌开会了,不过为了能顺利毕业,只能忍了.闲话不多说了,下面把上周 ...
- 计算机视觉算法——Transformer学习笔记
算机视觉算法--Transformer学习笔记 计算机视觉算法--Transformer学习笔记 1. Vision Transformer 1.1 网络结构 1.2 关键知识点 1.2.1 Self ...
- matlab中存档算法代码,MATLAB 智能算法超级学习手册中程序代码
[实例简介] MATLAB 智能算法超级学习手册中程序代码 [实例截图] [核心代码] dc90ef43-7920-434e-bdb8-0636c31c0b44 └── MATLAB 智能算法超级学习 ...
- KMP算法的学习经验
KMP算法的学习经验 (欢迎指正错误, 欢迎喷) 什么是kmp(完) kmp的额外知识(完) 暴力匹配的缺点,和代码实现(完) next[]数组的预先知识,了解前后缀,相同前后缀.(完) kmp的关键 ...
- 数据结构与算法深入学习_我最喜欢的免费课程,用于深入学习数据结构和算法...
数据结构与算法深入学习 by javinpaul 由javinpaul Data structures and algorithms are some of the most essential to ...
- leetcode 刷500道题,笔试/面试稳吗?谈谈算法的学习
来源公众号:苦逼的码农 作者:帅地 想要学习算法.应付笔试或者应付面试手撕算法题,相信大部分人都会去刷 Leetcode,有读者问?如果我在 leetcode 坚持刷它个 500 道题,以后笔试/面试 ...
- 从这十大算法开始学习机器学习与建模
本文介绍了机器学习新手需要了解的 10 大算法,包括线性回归.Logistic 回归.朴素贝叶斯.K 近邻算法等. 在机器学习中,有一种叫做「没有免费的午餐」的定理.简而言之,它指出没有任何一种算法对 ...
- 【Python成长之路】机器学习:10+分类算法汇总学习
一直都说python是人工智能.机器学习等算法的良配,很多python大神除了常规的大数据爬虫.网站开发等代码能力外,人工智能/机器学习也都是手到擒来.因此我也"跳坑"来看看 . ...
- KMP算法代学习之(二)代码深入学习
前段时间学习了KMP算法,感觉略懂略懂!但算法这个东西理解一定要深入,不是略懂略懂就能敷衍过去的!真真做题的时候,才发现仅仅套模板是根本没用的,必须自己写得来代码,这样的话才能从细节,从根本上了解KM ...
最新文章
- 关于鼠标、键盘的几个例子
- 知识产权创业的比赛结果
- Flutter:Navigator2.0介绍及使用
- 一分钟了解阿里云产品:RDS概述
- python路线选择试题_python例题练习
- Spring高级之注解@PropertySource详解(超详细)
- 开发者编程时应该围着“程序”转吗?
- Unix网络编程 chart
- JavaScript:工具库MyTools.js(自用不断填充····)
- 如何输入多组字符串c语言,求教大侠:如何输入一组字符串
- ECshop 模板制作教程
- mac 安装commitizen插件报错Parsing JSON at /Users/lin/.czrc for commitizen config failed
- 庆祝鸿蒙指的是哪个生肖,12月中头彩,苦难转幸福,3生肖,鸿蒙紫气,运走上坡路,想啥就有啥...
- HTML5隐藏图片代码,HTML5终极备忘大全(图片版+文字版)
- windows虚机环境下,如何快速有效的删除大文件夹?
- Win10桌面图标无法拖动
- 蒸妙熏蒸,疏通身体的“堵”
- ajax无线遥控器,利用python+tkinter做一个简单的智能电视遥控器
- 「C#」Bitmap/Image.Save()报错“GDI+ 中发生一般性错误”的一个案例总结
- LM321 低功耗单运算放大器 1MHZ增益带宽积 用于充电器 适配器