[英]Python OpenCV TypeError: Expected Ptr<cv::UMat> for argument 'lowerb', and color problems
Disclaimer: I'm a beginner in OpenCv, so there is a possibility of existence of very simple mistakes So I've prepared a code that counts pixels in given boundaries, and then save sit's count in an array, to make it possible to calculate pcc using them.免责声明:我是 OpenCv 的初学者,所以可能存在非常简单的错误所以我准备了一个代码来计算给定边界中的像素,然后将坐的计数保存在一个数组中,以便可以计算pcc 使用它们。
def PCCprepare():
print("[LOG] reporting!")
tot_pixel = 0
pixels = []
old_b = 0
old_g = 0
dir = os.listdir("res")
for filename in dir:
i = 0
img = cv2.imread("res/" + filename)
output = cv2.bitwise_and(img, img)
tot_pixel += output.size
for b in range(4):
for g in range(4):
for r in range(4):
lower = [b * 63.75, g * 63.75, r * 63.75]
print(lower)
if b > old_b:
upper = ([(b + 1) * 63.75, g * 63.75, r * 63.75])
elif g > old_g:
upper = ([b * 63.75, (g + 1) * 63.75, r * 63.75])
else:
upper = ([b * 63.75, g * 63.75, (r + 1) * 63.75])
print(upper)
old_g = g
old_b = b
mask = cv2.inRange(img, lower, upper)
output = cv2.bitwise_and(img, img, mask=mask)
try:
pixels[i] += np.count_nonzero(output)
except:
pixels.append(np.count_nonzero(output))
i += 1
It used to work for fixed bounderies, but as for now it always gives back the following exception:它曾经适用于固定边界,但就目前而言,它总是返回以下异常:
Ignoring exception in command imgSetup:
Traceback (most recent call last):
File "C:\Users\user\Downloads\Mash_The_CharacterCreator\venv\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:/Users/user/Downloads/Mash_The_CharacterCreator/bot.py", line 727, in waifuSet
await ctx.send("<@!" + str(ctx.message.author.id) + "> " + imgWorks.PCCprepare())
File "C:\Users\user\Downloads\Mash_The_CharacterCreator\WaifuMeterPNG.py", line 53, in PCCprepare
mask = cv2.inRange(img, lower, upper)
TypeError: Expected Ptr<cv::UMat> for argument 'lowerb'
Also if there is anyone who could help me with boundareis, cause I wold like to count every possible colour, and previously I used sth like this另外,如果有人可以帮助我解决边界问题,因为我想计算所有可能的颜色,而以前我像这样使用过
lower = [b * 63.75, g * 63.75, r * 63.75]
print(lower)
upper = [(b + 1) * 63.75, (g + 1) * 63.75, (r + 1) * 63.75]
print(upper)
but it leads to overlapping and inaccurate boundaries, so I would be glad if anyone more familiar with this topic would suggest some better solution for them但它会导致重叠和不准确的界限,所以如果有人更熟悉这个主题会为他们提出一些更好的解决方案,我会很高兴
In Python, the lowerb
and upperb
parameters of function cv2.inRange(src, lowerb, upperb, dst)
does not accept lists, it only support tuples. Python中,function
cv2.inRange(src, lowerb, upperb, dst)
的lowerb
和upperb
参数不接受列表,只支持元组。 So instead of using brackets, simply change them to parentheses:因此,不要使用括号,只需将它们更改为括号:
lower = (b * 63.75, g * 63.75, r * 63.75)
I have tested your code with the updated lower
and upper
variables on my machine and it works.我已经在我的机器上使用更新的
lower
和upper
变量测试了你的代码,它可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.