python - 如何将绘图中的图片用于进一步的代码
问题描述
我尝试使用绘图作为我进一步代码的图像。我在我的预训练模型中加载了一个图像,我的输出是一个张量变量。在下一步中,我用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))
解决方案
fastai
适用于 PIL 图像类型。所以你的变量img_hr
是一个Image
.
OpenCV 使用 NumPyndarray
类型。您需要将您的转换Image
为ndarray
:
_, 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()
推荐阅读
- tableau-api - 如何在数据集B中填写缺失的日期,然后通过Tableau混合数据集A和B?
- pgadmin-4 - 用于 postgreSQL 的 pgAdmin 工具无法连接到 Windows 10 本地计算机上的 PostgreSQL
- asp.net-core - ASP.NET Core 中的 Request.CurrentExecutionFilePath
- php - Moodle 更改徽章图像大小
- java - 如何按 5 个数字的部分对该数组进行排序?
- javascript - 以纯文本和 html 格式显示 ajax 响应
- java - COMPILE_TIME_ASSERTs 都无法将 dlib 引用为共享库
- exception-handling - 如何处理基于 spring-webflux 的 REST 服务中的异常
- c++ - 升级 MacOS 后 C++ 程序在 cstdint 中找不到整数类型
- jquery - 语义ui中的居中搜索框