无法将尺寸2的序列复制到尺寸3(numpy)的阵列轴
使用OpenCV和Numpy浏览图像时,我会出现此错误。 这是我的代码:
import math
import cv2
import numpy as np
palletes = [[255,85,85],[80,250,123],[255,184,108],[40,42,54],[68,71,90],[248,248,242],[98,114,164]]
def fcp(pixel, palletes): #Prototype for now
ans = math.inf #Just a filler
pal = [0,0,0] # Yet another filler
for pallete in palletes:
#Pythagorean Theorem (but it's 3D)
result = math.sqrt(sum(list(map(lambda x,y: (x-y)**2, pixel, pallete))))
if result < ans: ans = result; pal = pallete
return pal, ans
# read the wallpaper.png image with opencv
img = cv2.imread('wallpaper.png')
# convert the image to rgb lists for every pixel
img_rgb = img.reshape((img.shape[0]*img.shape[1],3))
for p, pixel in enumerate(img_rgb):
img_rgb[p] = fcp(list(pixel), palletes)
# undo the reshape
img_rgb = img_rgb.reshape(img.shape[0],img.shape[1],3)
# save the image to wallpaper_new.png
cv2.imwrite('wallpaper_new.png', img_rgb)
尝试执行img_rgb [p] = fcp(list(pixel),托盘)
时会遇到错误。
I'm having this error when going through an image with OpenCV and NumPy.
Here's my code:
import math
import cv2
import numpy as np
palletes = [[255,85,85],[80,250,123],[255,184,108],[40,42,54],[68,71,90],[248,248,242],[98,114,164]]
def fcp(pixel, palletes): #Prototype for now
ans = math.inf #Just a filler
pal = [0,0,0] # Yet another filler
for pallete in palletes:
#Pythagorean Theorem (but it's 3D)
result = math.sqrt(sum(list(map(lambda x,y: (x-y)**2, pixel, pallete))))
if result < ans: ans = result; pal = pallete
return pal, ans
# read the wallpaper.png image with opencv
img = cv2.imread('wallpaper.png')
# convert the image to rgb lists for every pixel
img_rgb = img.reshape((img.shape[0]*img.shape[1],3))
for p, pixel in enumerate(img_rgb):
img_rgb[p] = fcp(list(pixel), palletes)
# undo the reshape
img_rgb = img_rgb.reshape(img.shape[0],img.shape[1],3)
# save the image to wallpaper_new.png
cv2.imwrite('wallpaper_new.png', img_rgb)
I get the error when trying to perform the img_rgb[p] = fcp(list(pixel),palletes)
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没关系,答案很简单 - 即使只有第一个值,FCP函数也返回两个值。 Python需要追溯。
Never mind, the answer was quite simple - the fcp function returns two values, even though only the first one was expected. Python needs tracebacks.