我开始在与stereovision相关的项目中使用openCV和python.我在openCV中找到了关于C中的Census Transform的文档页面.
link
有谁知道python实现是否有类似的功能?
(例如cv2.nameofthefunction)
谢谢你们!
编辑:PM 2Ring的优秀解决方案(再次感谢)可以使用openCV进行这一小改动:而不是使用Image.open
img = cv2.imread(img.png)
#some minor changes I needed like select some ROI and store them in img2[j]
#then a for cycle in which I wrote
src_img = img2[j]
h, w = src_img.shape
其中“shape”指令似乎将w和h的顺序与“size”命令进行比较.然后我粘贴PM 2Ring的其余代码,它工作得非常好
解决方法:
我不使用openCV,我不知道是否有人口普查变换的现有实现.但是,使用Numpy很容易实现.
这是一个简单的演示,它使用PIL处理加载图像并将数组数据转换回图像.
#!/usr/bin/env python
''' The Census Transform
Scan an 8 bit greyscale image with a 3x3 window
At each scan position create an 8 bit number by comparing the value
of the centre pixel in the 3x3 window with that of its 8 neighbours.
The bit is set to 1 if the outer pixel >= the centre pixel
See https://stackoverflow.com/questions/38265364/census-transform-in-python-opencv
Written by PM 2Ring 2016.07.09
'''
import numpy as np
from PIL import Image
iname = 'Glasses0S.png'
oname = 'Glasses0S_census.png'
#Get the source image
src_img = Image.open(iname)
src_img.show()
w, h = src_img.size
print('image size: %d x %d = %d' % (w, h, w * h))
print('image mode:', src_img.mode)
#Convert image to Numpy array
src_bytes = np.asarray(src_img)
#Initialize output array
census = np.zeros((h-2, w-2), dtype='uint8')
#centre pixels, which are offset by (1, 1)
cp = src_bytes[1:h-1, 1:w-1]
#offsets of non-central pixels
offsets = [(u, v) for v in range(3) for u in range(3) if not u == 1 == v]
#Do the pixel comparisons
for u,v in offsets:
census = (census << 1) | (src_bytes[v:v+h-2, u:u+w-2] >= cp)
#Convert transformed data to image
out_img = Image.fromarray(census)
out_img.show()
out_img.save(oname)
资源
产量
原始的全彩眼镜图像由Gilles Tran使用POV-Ray创建,属于公共领域.它可能会在Wikipedia找到.
标签:python,opencv