gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:50:22 25 4
gpt4 key购买 nike

我有一个如下所示的图像,并且我已经为该图像创建了蒙版(红点是从蒙版生成的)。理想情况下,蒙版中的点应覆盖图像上的整个黑点,但由于每个黑 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com