python openCV中的人口普查转换 [英] Census transform in python openCV

查看:108
本文介绍了python openCV中的人口普查转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始在与stereovision相关的项目中使用openCV和python.我找到了有关使用openCV进行C ++中的人口普查转换的文档的本页. 链接

I am starting to work with openCV and python in a project related with stereovision. I found this page of documentation about the Census Transform in C++ with openCV. link

有人知道python实现是否有类似的功能吗?

Does anyone know if there are similar functions for python implementation?

(例如功能的cv2.name)

(e.g. cv2.nameofthefunction)

谢谢大家!

PM 2Ring的出色解决方案(再次感谢您)可以与openCV一起使用,但只需一点改动:而不是使用Image.open

the excellent solution of PM 2Ring (thank you again) can work with openCV with this little change: instead of using 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

与"size"命令相比,"shape"指令似乎可以切换w和h的顺序.然后,我粘贴了PM 2Ring的其余代码,效果很好

where the "shape" instruction seems to switch the order of w and h compared to "size" command. Then I paste the rest of the code of PM 2Ring and it worked wonderfully

推荐答案

我不使用openCV,也不知道是否有人口普查转换的现有实现.但是,使用Numpy实施起来很容易.

I don't use openCV, and I don't know if there's an existing implementation of the Census Transform. However, it's easy enough to implement using Numpy.

这是一个简单的演示,使用 PIL 来处理图像加载并将数组数据转换回图像.

Here's a simple demo that uses PIL to handle loading the image and converting the array data back to an image.

#!/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 http://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创建,并且已在公共领域使用.可以在维基百科上找到.

The original full-colour Glasses image was created by Gilles Tran using POV-Ray, and is in the public domain. It may be found on Wikipedia.

这篇关于python openCV中的人口普查转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆