gpt4 book ai didi

python - 用for循环遍历二值化图像可能吗?

转载 作者:行者123 更新时间:2023-12-02 16:53:01 24 4
gpt4 key购买 nike

这是我的python代码:

import cv2
img = cv2.imread("foo.jpg")

#here I can iterate trough each pixel since I have a 2D array
for x in range(img.shape[0]):
for y in range(img.shape[1]):
pass #maipulate each pixel

gary = cv2.cvtColor(img, COLOR_BGR2GRAY)
bin = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)

#here I can not iterate trough each pixel since I have a 1D array
for x in range(img.shape[0]):
for y in range(img.shape[1]):
pass

我的问题:
如何遍历二进制图像的每个像素?
我想使用滑动窗口搜索算法。

最佳答案

您的代码无效,因为threshold()返回的元组包含2个值:您设置的阈值(127)和二进制图像。如果将它们分开,则可以使用相同的双循环访问每个值/像素。
我已经修改了您的代码,因为那里也有一些错字。

import cv2
img = cv2.imread("foo.jpg")

#here I can iterate trough each pixel since I have a 2D array
for x in range(img.shape[0]):
for y in range(img.shape[1]):
pass #maipulate each pixel

gray= cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh, bin_img = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)

for x in range(bin_img.shape[0]):
for y in range(bin_img.shape[1]):
pass

关于python - 用for循环遍历二值化图像可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55904738/

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