首页 > 解决方案 > 如何将绘图中的图片用于进一步的代码

问题描述

我尝试使用绘图作为我进一步代码的图像。我在我的预训练模型中加载了一个图像,我的输出是一个张量变量。在下一步中,我用Image(img_hr).show(figsize=(18,15)). 在此之后,我想使用图中的图片来转换颜色。但问题是,我不能使用变量img_hr,因为类型是张量。

我的想法是在倒数第三行阅读情节。的输入imagehsv = cv2.cvtColor(img_hr, cv2.COLOR_BGR2HSV)需要是一个数组,我不知道如何转换绘图。

这是错误:

imagehsv = cv2.cvtColor(img_hr, cv2.COLOR_BGR2HSV) 错误:OpenCV(4.5.2) :-1: 错误:(-5:Bad argument) in function 'cvtColor' 重载分辨率失败:

  • src 不是 numpy 数组,也不是标量
  • 参数“src”的预期 Ptr<cv::UMat>

我是 python 新手,所以请原谅我任何不好的描述或错误的词汇。我希望一切都清楚,否则请随时询问。

这是情节的图片:阴谋

有人有想法吗?非常感谢

   from fastai import *
from fastai.vision import *
from fastai.callbacks.hooks import *
from fastai.utils.mem import *
import numpy as np
import cv2 as cv2
import matplotlib as mpl
import matplotlib.pyplot as plt 



def acc_camvid(input, target):
        target = target.squeeze(1)
        mask = target != void_code
        return (input.argmax(dim=1)[mask]==target[mask]).float().mean()

learn=load_learner(r'C:\pretrained_model')


image= r"C:\image.png"

img = open_image(image); img.shape
_,img_hr,b = learn.predict(img)
Image(img_hr).show(figsize=(18,15))    

#image = cv2.imread(Image(img_hr))

imagehsv = cv2.cvtColor(img_hr, cv2.COLOR_BGR2HSV)
plt.imshow(fixColor(imagehsv))

标签: pythonopencvdeep-learningneural-networkimread

解决方案


fastai适用于 PIL 图像类型。所以你的变量img_hr是一个Image.

OpenCV 使用 NumPyndarray类型。您需要将您的转换Imagendarray

_, img_hr, b = learn.predict(img)
img_hr = np.array(img_hr)

# PIL uses RGB channel order, not BGR like OpenCV default
imagehsv = cv2.cvtColor(img_hr, cv2.COLOR_RGB2HSV)

# Display your result in HSV color space
cv2.imshow("Image HSV", imagehsv)
cv2.waitKey()

推荐阅读