python - 我如何知道Python中某些坐标(存储在列表中)的二进制图像中是否有白色像素?

标签 python image-processing numpy scikit-image mahotas

我有一个numpy array binary (黑白)元组列表中的图像和坐标,例如:

coordlist =[(110, 110), (110, 111), (110, 112), (110, 113), (110, 114), (110, 115), (110, 116), (110, 117), (110, 118), (110, 119), (110, 120), (100, 110), (101, 111), (102, 112), (103, 113), (104, 114), (105, 115), (106, 116), (107, 117), (108, 118), (109, 119), (110, 120)]

或如:

coordx = [110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110]
coordy = [110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120]

如何检查具有该坐标列表的图像中是否存在“白色”像素? 我还想检查距离该坐标列表大约 3 个像素范围的白色像素。

即:

for i, j in coordx, coordy:
    for k in a range (k-3, k + 3)
        for l in a range (l-3, l + 3)
            #checking white pixels also for pixel near coordinates list

我想到了“where”函数。

from skimage import morphology
import numpy as np

path = 'image/a.jpg'
col = mh.imread(path)
bn0 = col[:,:,0]
bn = (bn0 < 127)
bnsk = morphology.skeletonize(bn)
bnskInt = np.array(bnsk, dtype=np.uint8)

#finding if there are white pixel in the coord list and around that in a 5 pixel range
for i in coordlist:
np.where(?)

更新

我尝试使用形状 (128, 128) 而不是 (128, 128, 3),因为我的图像具有以下形状:(a,b) 但现在它找不到白色像素! 为什么这样就能找到东西呢?

    white_pixel = np.array([255, 255])
    img = np.random.randint(0, 256, (128, 128))
    print(img[150])
    print(img.shape)
    img[110, 110] = 255
    img[109, 110] = 255

    mask = np.zeros((128, 128), dtype=bool)
    mask[coordx, coordy] = 1
    #structure = np.ones((3, 3, 1))
    #mask = scipy.ndimage.morphology.binary_dilation(mask, structure)

    is_white = np.all((img * mask) == white_pixel, axis=-1)

    # This will tell you which pixels are white
    print np.where(is_white)

    # This will tell you if any pixels are white
    print np.any(is_white)

输出:

(array([], dtype=int32),)
False

最佳答案

更新,我已经更新了使用二进制或灰度图像的答案。请注意,图像强度现在只是标量而不是(R、G、B)值,并且所有图像、掩模和结构元素都是 2d 数组而不是 3d 数组。您可能需要调整 white_pixel 的值(或修改此代码以满足您的需要)。

import numpy as np
from skimage.morphology import binary_dilation
# Setup
coordx = [110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 100, 101, 102,
          103, 104, 105, 106, 107, 108, 109, 110]
coordy = [110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 110, 111, 112,
          113, 114, 115, 116, 117, 118, 119, 120]
img = np.random.random((128, 128))
img[110, 110] = 1.
img[109, 110] = 1.


# values grater than white_pixel will get detected as white pixels
white_pixel = 1

mask = np.zeros((128, 128), dtype=bool)
mask[coordx, coordy] = 1

structure = np.ones((7, 7))
mask = binary_dilation(mask, structure)

is_white = (img * mask) >= white_pixel

# This will tell you which pixels are white
print np.where(is_white)

# This will tell you if any pixels are white
print np.any(is_white)

原答案:

如果您想知道哪些像素是白色,则只需使用numpy.where。我只需将图像乘以掩码并使用 np.any,如下所示:

# Setup
coordx = [110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 100, 101, 102,
          103, 104, 105, 106, 107, 108, 109, 110]
coordy = [110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 110, 111, 112,
          113, 114, 115, 116, 117, 118, 119, 120]
white_pixel = np.array([255, 255, 255])
img = np.random.randint(0, 256, (128, 128, 3))
img[110, 110, :] = 255
img[109, 110, :] = 255

mask = np.zeros((128, 128, 1), dtype=bool)
mask[coordx, coordy] = 1

structure = np.ones((7, 7, 1))
mask = binary_dilation(mask, structure)

is_white = np.all((img * mask) == white_pixel, axis=-1)

# This will tell you which pixels are white
print np.where(is_white)

# This will tell you if any pixels are white
print np.any(is_white)

关于python - 我如何知道Python中某些坐标(存储在列表中)的二进制图像中是否有白色像素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19619996/

相关文章:

Python 多处理 - 从池中动态重新分配作业 - 无需异步?

image - 将真实图像卡通化

python - 如何用点而不是条形图创建直方图

python - numpy.loadtxt 不读取包含复数的文件

python - PySide 导入错误 Mac OS X El Capitan, Library not loaded : @rpath/libpyside. cpython-34m.1.2.dylib

python - Django ValidationError View 中?!不形成

python - OpenCV 错误 : bitwise_and throws error that mask and image are not same size

OpenCV锐化边缘(没有孔的边缘)

python - 如何使用 View (numpy)将二维数组转换为结构化数组?

python - 我可以使用 sqlalchemy 在 RowProxy 中赋值吗?