Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中,如何知道二进制图像在某些坐标(存储在列表中)中是否有白色像素?_Python_Image Processing_Numpy_Scikit Image_Mahotas - Fatal编程技术网

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

在python中,如何知道二进制图像在某些坐标(存储在列表中)中是否有白色像素?,python,image-processing,numpy,scikit-image,mahotas,Python,Image Processing,Numpy,Scikit Image,Mahotas,我有一个黑白图像和元组列表中的坐标,如: 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), (

我有一个黑白图像和元组列表中的坐标,如:

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像素的白色像素

i、 e:

我想到了函数的位置

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(?)
输出:

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

更新,我已经更新了使用二值或灰度图像的答案。请注意,图像强度现在只是标量,而不是R、G、B值,并且所有图像、遮罩和结构元素都是二维阵列,而不是三维阵列。您可能需要调整白色像素的值,或者修改此代码以满足您的需要

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)

更新,我已经更新了使用二值或灰度图像的答案。请注意,图像强度现在只是标量,而不是R、G、B值,并且所有图像、遮罩和结构元素都是二维阵列,而不是三维阵列。您可能需要调整白色像素的值,或者修改此代码以满足您的需要

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)

缺少导入:从skimage.形态学导入二进制\扩展导入numpy,因为npIt工作!膨胀是为了我的要求,检查也在一个3像素范围内周围的坐标列表?是吗?@postgres是的,这个结构控制着射程的大小。如果你想要一个3像素范围,请使用structure=np.ones7,7,1,dtype=bool。我尝试使用128,128形状而不是128,128,3,因为我的图像有以下形状:a,b,但现在它找不到白色像素!为什么它会以这种方式找到任何东西?@postgres,很抱歉,在重新阅读您的问题后,我发现您没有使用RGB图像,我已更新了我的答案,以删除RGB内容并使用灰度或二进制图像。缺少导入:从skimage.形态学导入二进制\放大导入numpy作为npIt工作!膨胀是为了我的要求,检查也在一个3像素范围内周围的坐标列表?是吗?@postgres是的,这个结构控制着射程的大小。如果你想要一个3像素范围,请使用structure=np.ones7,7,1,dtype=bool。我尝试使用128,128形状而不是128,128,3,因为我的图像有以下形状:a,b,但现在它找不到白色像素!为什么它会以这种方式找到任何东西?@postgres,抱歉,在重新阅读您的问题后,我发现您没有使用RGB图像,我已更新了我的答案,以删除RGB内容并使用灰度或二进制图像。