无法将尺寸2的序列复制到尺寸3(numpy)的阵列轴

发布于 2025-02-08 00:32:51 字数 1076 浏览 0 评论 0原文

使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过去的过去 2025-02-15 00:32:51

没关系,答案很简单 - 即使只有第一个值,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文