如何在MATLAB中实现人口普查变换
我想在MATLAB中对每个过滤器窗口的中心像素执行普查变换,如下所示: 如果未显示图像,则会显示另一个链接: 我对代码的初始尝试是:如何在MATLAB中实现人口普查变换,matlab,image-processing,binary,filtering,Matlab,Image Processing,Binary,Filtering,我想在MATLAB中对每个过滤器窗口的中心像素执行普查变换,如下所示: 如果未显示图像,则会显示另一个链接: 我对代码的初始尝试是: function output =census(img,census_size) img_gray = rgb2gray(img); [y,x]=size(img_gray); borders = floor(census_size/2); % limit to exclude image borders when filtering
function output =census(img,census_size)
img_gray = rgb2gray(img);
[y,x]=size(img_gray);
borders = floor(census_size/2); % limit to exclude image borders when filtering
for(iy = 1+borders : y-borders)
for(ix = 1+borders : x-borders)
f=img_gray(iy-borders:iy+borders,ix-borders:ix+borders);
iix=ix-borders;
iiy=iy-borders;
% shift=bitsll(img_out(iiy,iix),1);
img_out(iiy,iix)= % Must be Implemented with census
end
end
%normalised_image = img_out ./ max(max(img_out)) ;
output=img_out;
imshow(normalised_image);
end
第二个for循环处的iix和iy表示中心像素的当前位置。f是我当前的过滤器窗口
除了与窗口的其他像素进行比较操作外,我还需要将每个比较结果设置为逻辑1/0,并将总结果(通过移位)扩展为8位数据,然后将该二进制数转换为十进制数。如何在MATLAB中以实用的方式实现这一点?
我在Python中检查了这一点:
但无法在MATLAB中实现相似性 如果您有图像处理工具箱,您可以使用: 此处
blockproc
配置为3x3窗口(带重叠),用于灰度图像。功能fun
检查块的哪个部分严格大于块的中心。我们得到一个1x9
逻辑向量。然后我将这个向量乘以[128 64 32 16 0 8 4 2 1]
(二进制到十进制转换)
更新:
线性代数优化
对于随机窗口大小,您可以使用:
I = imread('https://i.stack.imgur.com/9oxaQ.png');
w = 5; % windows size, any odd number between 3 and 31.
b2d = 2.^[w^2-1:-1:ceil(w^2/2),0,floor(w^2/2):-1:1] % binary to decimal vector
cen = ceil(w/2); % center position
%Creation of the census transform function
fun = @(B) b2d*(B.data(:)>B.data(cen,cen));
%Process the image, block-by-block with overlap
I2 = blockproc(I.',[1 1],fun,'BorderSize',[cen-1,cen-1],'TrimBorder',0).'/sum(b2d)
输入:
输出:
天真的方法是在你的2个循环中添加另外2个循环,用于我们像素的邻域。尝试一下,然后你可以开始思考实用/快速的方法。我想我可以做到,我可以通过比较中心像素和相邻像素形成新的逻辑窗口。我现在在想,如何通过从最下面的行开始左移,省略中心像素,并设置中心像素的值来扩展我的二进制结果。例如,如果我的逻辑数组是[10;0中心1;1 1 0],我的中心应该等于uint8(0 1 1 1 0 0 1),谢谢!如果我使用更大的窗口大小,我应该将中心更改为(地板(行/2)+1,地板(列/2)+1)?,此外,您不认为我最好规范化输出变换图像,以便更好地计算汉明(基于0或1像素值)在图像深度中匹配块算法的距离?检查我的编辑,在那里可以选择窗口大小,并将输出标准化。窗口大小应始终为奇数。注意到,例如,当窗口大小为7时,该算法已经处理了大小为2^(7^2-1)或2^48的值。如果窗口大小过大,则会出现溢出。理论上,最大窗口大小可能为31。
I = imread('https://i.stack.imgur.com/9oxaQ.png');
w = 5; % windows size, any odd number between 3 and 31.
b2d = 2.^[w^2-1:-1:ceil(w^2/2),0,floor(w^2/2):-1:1] % binary to decimal vector
cen = ceil(w/2); % center position
%Creation of the census transform function
fun = @(B) b2d*(B.data(:)>B.data(cen,cen));
%Process the image, block-by-block with overlap
I2 = blockproc(I.',[1 1],fun,'BorderSize',[cen-1,cen-1],'TrimBorder',0).'/sum(b2d)