python人口普查数据显示_人口普查变换在python openCV中

我开始在与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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值