gpt4 book ai didi

python - 如何解决此堆栈溢出错误(python)?

转载 作者:行者123 更新时间:2023-12-02 17:28:14 25 4
gpt4 key购买 nike

我正在尝试计算图像中补丁中类似颜色的像素的数量。我正在尝试使用openCV并创建一个函数来执行此操作。
后来我发现openCV中有一个可用的函数可以执行此操作,但我很好奇它为什么显示此错误。

import cv2
img = cv2.imread('drops.jpg')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

gray = cv2.resize(gray, (256, 256))


for i in range (256):
for j in range(256):
gray[i,j] = gray[i,j]//10


def flood(img, i, j, color):
m = len(img[0])
n = len(img)

if (i<0 or i>=m or j<0 or j>=n):
return 0

if (img[i,j]!=color):
return 0

if (img[i,j]==255): #Error here
return 0

img[i,j] = 255

return (1+flood(img, i+1, j, color) #Error here
+flood(img, i, j+1, color)
+flood(img, i-1, j, color)
+flood(img, i, j-1, color)
+flood(img, i-1, j-1, color)
+flood(img, i+1, j-1, color)
+flood(img, i-1, j+1, color)
+flood(img, i+1, j+1, color))

def count(img):
for i in range(256):
for j in range(256):
if(img[i,j]!=255):
print(flood(img,i,j,img[i,j]))

count(gray)

Error:
Fatal Python error: Cannot recover from stack overflow.

Current thread 0x00005294 (most recent call first):
File "E:\Coding\CBIR\images\CCV.py", line 24 in flood
File "E:\Coding\CBIR\images\CCV.py", line 36 in flood
File "E:\Coding\CBIR\images\CCV.py", line 36 in flood
File "E:\Coding\CBIR\images\CCV.py", line 36 in flood
File "E:\Coding\CBIR\images\CCV.py", line 36 in flood
File "E:\Coding\CBIR\images\CCV.py", line 36 in flood
File "E:\Coding\CBIR\images\CCV.py", line 36 in flood
File "E:\Coding\CBIR\images\CCV.py", line 36 in flood

最佳答案

import cv2 as cv
from collections import Counter
from pathlib import Path
import os
from matplotlib import pyplot as plt
from sklearn.feature_extraction import image
from collections import Counter
p = Path("/content/gdrive/My Drive/Colab Notebooks/images/cats")
new_p = os.path.join(p,'cat.1.jpg')
im = cv.imread(new_p,cv.COLOR_RGB2BGR)
im = cv.resize(im,(150,150))
patches = image.extract_patches_2d(im, (50,50)) #you can add your own patches
new_list = patches[5].reshape(-1,3).tolist()
res = [str(i) for i in new_list]
print(Counter(res))

下面给出的图像输出
plt.imshow(im)
plt.imshow(patches[5])

full image
patch image

关于python - 如何解决此堆栈溢出错误(python)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57678866/

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