Solve the arithmetic operation error in opencv: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cp

In the past few days, I have used opencv to do some image processing problems, and I got stuck when I was doing arithmetic operations. Searching on the Internet can't always solve my problems. Finally, through continuous attempts, it was finally solved!

报错提示:OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cpp:967: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

or so

OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\core\src\arithm.cpp:650: error: (-209:Sizes of input arguments do not match) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function 'cv::arithm_op'

 

 Cause of error: After n times of debugging, I got the following three solutions

1. The file reading path is wrong 

       This is a problem we often encounter. Everyone’s English level is not very high, and they are used to using Chinese as the name of the folder for easy search, but opencv is not used to it, but it will report an error if there is a little Chinese (there was no error at the time, it does not mean the nth. also after the first run). The following format is a good habit.

import cv2  as cv
import numpy as np  

#读取图像
img = cv.imread("D:\jpg\lena.jpg",0)
cv.imshow("img",img)
cv.waitKey(0)

 If not, we can try to use double slash "//" (absolute path) and backslash "\" (linux path) in the path

2. The size or format of the image is inconsistent

     This is the problem I encountered. First of all, if the sizes of the images we read are inconsistent, the images cannot be fused, because the fusion of images is equivalent to adding the corresponding pixel values ​​​​in the matrix. If the number of pixel values ​​​​is not the same , how to integrate it?

Solution :

import cv2 as cv 
import numpy as np
img11=cv.imread("D:\lenargb.tif",1)
img11 = cv.resize(img11, (400, 400), interpolation=cv.INTER_AREA)#调整图片大小
img12=cv.imread("D:\\photo\\txcl\\sleep.jpg",1)
img12 = cv.resize(img12, (400, 400), interpolation=cv.INTER_AREA)#调整图片大小
add=cv.add(img11,img12)#加:实现图像融合
cv.imshow("w",add)
cv.waitKey(0)

As long as we will add this line of code:

Just adjust the size of the two pictures to be the same.

cv.resize(img, (400, 400), interpolation=cv.INTER_AREA)

 3. Another situation is that the format of the picture is different, the same reason:

For example: Suppose the data type of image 1 is unit8, and the data type of image 2 is float32. When the data types of the two images are inconsistent, what type of image should be returned by arithmetic operations such as add, subtract, multiply, and divide?

So we need to declare its data type. Add a dtype parameter in the arithmetic operation add function , as follows:

img = cv.add(img1,img2,dtype=cv2.CV_8UC3) #声明生成新的img的数据类型

Some people may be curious, what type of image is the final generated image?

Then, we can use:

img.dtype #查看img的类型

See what type of picture is finally generated!

Friends can also reply to other questions encountered in the process in the comment area, let's discuss together!

Finally attach my code:

# 图像的基本运算加,减,逻辑运算  opencv 是大于255取255,numpy是取模
import cv2 as cv
image1=cv.imread("D:/opencv/lenargb.jpg",1)
image1 = cv.resize(image1, (400, 400), interpolation=cv.INTER_AREA)
image2=cv.imread("D:/opencv/\girl.jpg",1)
image2 = cv.resize(imgage2, (400, 400), interpolation=cv.INTER_AREA)
add=cv.add(image1,image2)#加法实现图像融合
sub=cv.subtract(image1,image2)#减法可实现图像目标运行的检测
cheng=cv.multiply(image1,image2)#乘法
chu=cv.divide(image1,image2)#除法可实现目标的位置判断
yu=cv.bitwise_and(image1,image2)#与运算
huo=cv.bitwise_or(image1,image2)#或运算
yihuo=cv.bitwise_xor(image1,image2)#异或运算
fei=cv.bitwise_not(image1)#非运算

#opencv在一个窗口显示多幅图像不方便
cv.imshow("image1",image1)
cv.imshow("image2",image2)
cv.imshow("add",add)
cv.imshow("sub",sub)
cv.imshow("yu",yu)
cv.imshow("huo",huo)
cv.imshow("yihuo",yihuo)
cv.imshow("fei",fei)
cv.waitKey(0)
cv.destroyAllWindows()

#matplotlib里显示多幅图像
import matplotlib.pyplot as plt
import cv2 as cv
add1=cv.cvtColor(add,cv.COLOR_BGR2RGB) #将bgr转换为rgb
sub1=sub[:,:,::-1]#将bgr转换为rgb
yu1=yu[:,:,::-1]
huo1=huo[:,:,::-1]
yihuo1=yihuo[:,:,::-1]
fei1=fei[:,:,::-1]

plt.figure()
# 解决中文显示问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.subplot(231);plt.imshow(add1);plt.title("add1")
plt.subplot(232);plt.imshow(sub1);plt.title("sub1")
plt.subplot(233);plt.imshow(yu1);plt.title("yu1")
plt.subplot(234);plt.imshow(huo1);plt.title("huo1")
plt.subplot(235);plt.imshow(yihuo1);plt.title("yihuo1")
plt.subplot(236);plt.imshow(fei1);plt.title("fei1")

Screenshot of the result:

Guess you like

Origin blog.csdn.net/m0_62026333/article/details/127638999