Ask Your Question
1

Why am I getting an error when adding 2 images?

asked Feb 26 '18

Chirag gravatar image

I am trying to add the laplacian of the image to the original image to enhance the details in the image, but I am getting the following error.

cv2.error: /io/opencv/modules/core/src/arithm.cpp:683: error: (-5) When the input arrays in add/subtract/multiply/divide functions have different types, the output array type must be explicitly specified in function arithm_op

Pls help I'm a begginer in image processing.

from pylab import *
from PIL import Image
import cv2
import numpy as np
from matplotlib import pyplot as plt

image=cv2.imread('./Desktop/whale train/train/0a0b2a01.jpg',0)
laplacian = cv2.Laplacian(image,cv2.CV_64F)
tu=cv2.add(laplacian,image)
plt.subplot(1,1,1),plt.imshow(tu,cmap = 'gray')
plt.title('Laplacian'), plt.xticks([]), plt.yticks([])
plt.show()
Preview: (hide)

3 answers

Sort by » oldest newest most voted
-1

answered Feb 26 '18

berak gravatar image

updated Feb 26 '18

the image you read from disc is CV_8U, and you try to add it to a CV_64F image

either convert it, before the addition:

image = np.asarray(image, np.float64)

or specify a type flag in the add call:

add(laplacian, image, dtype=cv2.CV_64F)
Preview: (hide)
0

answered Oct 11 '19

tjwmmd2 gravatar image

updated Oct 11 '19

On a related note...

Personally I needed to find the absdiff between a float64 (Array "a") and a uint8 (Array "b"). The cv2.absdiff function sadly does not take a data output type, so it complained with the same error as in your post, saying that the two input arrays were different types.

One solution, a BAD solution, is to write: cv2.absdiff(a, b.astype(np.float64))... But this astype call causes Numpy to allocate brand new array memory and copy all values there. Very wasteful.

The final solution, which is almost 50% faster, was to stop using OpenCV's nerfed "absdiff" and instead achieving the result as follows: foo = cv2.subtract(a, b, dtype=cv2.CV_64F) which forces the result-array to be float64, and successfully handles subtracting floats from ints or vice versa. Next, you simply do np.abs(foo, out=foo) to tell Numpy to do an in-place overwriting of each array value with their absolute value, which avoids having to create a brand new array for the abs output.

In short: If OpenCV complains about comparing different types, you have to first of all specify the "greatest common denominator" type in your call to cv2.subtract (or cv2.add or similar) via the dtype= parameter. And secondly, if you want absolute values, you simply use Numpy's built-in function and give it the same output as the input to make it do a very fast in-place overwrite.

I hope this helps someone else who chases maximum performance!

Preview: (hide)
-1

answered Jan 19 '19

Olá, seu problema, provavelmente, ocorre devido suas imagens serem de tamanhos diferentes. O cv2 disponibiliza de um método que pode lhe ajudar à ajustar automaticamente uma imagem usando a outra como referência, o método é resize(). Então você deveria fazer assim, escolher uma imagem que será alterada, escolha a img2, e em seguida usar a img1 como parâmetro para img2 ficar com o mesmo tamanho.

Resposta: img2_resized = cv2.resize(img2, (img1.shape[1], img1.shape[0]))

e seu código ficaria assim:

import numpy as np import cv2

img1 = cv2.imread('ml.png') img2 = cv2.imread('opencv.png')

img2_resized = cv2.resize(img2, (img1.shape[1], img1.shape[0]))

dst = cv2.addWeighted(img1,0.7,img2_resized,0.3,0)

cv2.imshow('dst', dst)

cv2.waitKey(0) cv2.destroyAllWindows()

É claro que existem outras possibilidades, como utilizar um outro software para fazer o redimensionamento ou usar, realmente, duas imagens iguais...

in your google translator (português of Brazi or PT)

Preview: (hide)

Question Tools

1 follower

Stats

Asked: Feb 26 '18

Seen: 30,669 times

Last updated: Oct 11 '19