python - 如何识别图像中具有相似强度的附近像素?

标签 python image image-processing graphics deep-learning

我有一个如下所示的图像,并且我已经为该图像创建了蒙版(红点是从蒙版生成的)。理想情况下,蒙版中的点应覆盖图像上的整个黑点,但由于每个黑 block 只有一个坐标,因此生成的图像如下所示。

enter image description here

如何识别周围的像素(灰色和浅黑色)并对其进行标记?有没有什么方法或方法我可以查找和实现。

最佳答案

如果我理解正确,您希望将周围的所有灰色和深色像素转换为红色。如果是这样,这里有一个使用 OpenCV 的方法。想法是加载图像,转换为 grayscale ,然后 Otsu's threshold获得1 channel 二值图像。这将为我们提供一个可以使用的掩码 np.where将蒙版上有白色像素的像素着色为红色。结果如下:

二进制掩码

enter image description here

结果

enter image description here

import cv2
import numpy as np

# Load image, grayscale, Otsu's threshold
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY_INV)[1]

# Color pixels red where there are white pixels on the mask
image[np.where(thresh==255)] = [0,0,255]

# Display
cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.waitKey()

关于python - 如何识别图像中具有相似强度的附近像素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60570837/

相关文章:

python - 重试 <Response [404]> 直到我得到 <Response [200]>

python - 有条件地替换 Pandas 中的缺失值

image - 我可以渲染 HTML5 canvas 并将其设置为 HTML IMAGE 的源代码吗?

python - 从图像中删除字符之间的小垂直线

image - 如何组合一幅图像的相位和不同图像的大小?

python - 在 yaml 中构建 Lambda 函数 - 问题

python - 来自 django 教程 was_published_recently.admin_order_field = 'pub_date'

Javascript简单图像 slider

Javascript 如何更改图像 src 的高度和宽度

python - 使用 PIL 将 numpy 数组保存为 TIFF 序列