图像的线性组合给出m

2024-10-17 06:28:00 发布

您现在位置:Python中文网/ 问答频道 /正文

让我们考虑以下代码

import  numpy as np

import cv2 as cv

imag1 =cv.imread('logos.jpg')
imag1 =cv.cvtColor(imag1,cv.COLOR_BGR2GRAY)
shape1 =imag1.shape
print(shape1)
imag2 =cv.imread('arduino.jpg')
imag2 =cv.cvtColor(imag2,cv.COLOR_BGR2GRAY)
shape2 =imag2.shape
print(shape2)

imag2 =cv.resize(imag2,(shape1[1],shape1[0]))

cv.imshow('logos',imag1)

cv.imshow('arduino',imag2)
result1 =cv.addWeighted(imag1,0.2,imag2,0.8,0)
cv.imshow('result1',result1)
alpha =0.5
result =alpha *imag1 + (1-alpha)*imag2
cv.imshow('result',result)
cv.waitKey(0)

python-cv.addWeighted的内置函数运行良好,但当我尝试实现第二个函数时,它会给我以下错误

Traceback (most recent call last):
  File "C:/Users/Dato/Downloads/python_environment/weighted.py", line 23, in <module>
    cv.imshow('result',result)
cv2.error: OpenCV(4.0.0) c:\projects\opencv-python\opencv\modules\imgproc\src\color.hpp:261: error: (-2:Unspecified error) in function '__cdecl cv::CvtHelper<struct cv::Set<1,-1,-1>,struct cv::Set<3,4,-1>,struct cv::Set<0,2,5>,2>::CvtHelper(const class cv::_InputArray &,const class cv::_OutputArray &,int)'
> Unsupported depth of input image:
>     'VDepth::contains(depth)'
> where
>     'depth' is 6 (CV_64F)

图像的大小是(223400),(223403),这就是为什么我应用了resize函数使图像相等,但我不明白错误的原因是什么?提前谢谢


Tags: 函数importalphaaserrorresultcvstruct
1条回答
网友
1楼 · 发布于 2024-10-17 06:28:00

第二种方法产生的值可能是零值。由于强度值是介于0和255之间的整数值,因此需要对线性组合的结果值进行舍入,然后将数据类型设置为uint8

result = alpha * imag1 + (1 - alpha) * imag2
result = np.around(result).astype(np.uint8)

相关问题 更多 >