[PYTHON] When OpenCV hconcat gives an error for some reason

As the title says. I wrote an article because there was no page where the error message and the cause were simply linked.

If you want to concatenate one image with another (although the same image is fine), it is convenient to use OpenCV hconcate or vconcat. For example, when doing hconat (horizontal connection), it is naturally necessary to align the number of vertical pixels with each other, but sometimes even after aligning it, an error may occur for some reason.

Like this.

python


combined_image = cv2.hconcat([left_image,right_image])

output


Exception has occurred: error
OpenCV(4.1.2) C:\projects\opencv-python\opencv\modules\core\src\matrix_operations.cpp:68: error: (-215:Assertion failed) src[i].dims <= 2 && src[i].rows == src[0].rows && src[i].type() == src[0].type() in function 'cv::hconcat'

It seems that there is no size for matrix calculation. Did you make a mistake in resizing? Debug with print.

print(left_image.shape)
print(right_image.shape)

output


(256, 256)
(256, 256)

Isn't it hot!

In conclusion, it's because ** the data types don't match **. Let's match using numpy.

left_image = np.array(left_image)
left_image = out.astype('int32')
right_image = np.array(right_image)
right_image = canny.astype('int32')

Now you can connect.

bonus

Other common errors.

Different height

Let's resize. You can do it with cv2.resize (img, (x, y)).

The image cannot be loaded

It's rudimentary, but it tends to be. With ʻimread, even if you make a mistake in specifying the path, no error will occur and Nonewill be returned. Afterprint, check if it is None`.

I'm trying to combine a grayscale and a color image

This is also common. The gray image also needs to be a color image on the data. You can do it with cv2.cvtColor (img, cv2.COLOR_GRAY2BGR).

Recommended Posts

When OpenCV hconcat gives an error for some reason
I got an error when saving with OpenCV
I get an error when I put opencv in pyautoGUI
# Solution when pip install gives an error when using Anaconda on Windows 10
A memo for when pip3 is installed with python2.7 for some reason
When the Linux command $ yum -y install gives an error (yum: command not found)
When I get an error with PyInstaller
I got an error when I put opencv in python3 with Raspberry Pi [Remedy]
Rollback processing when an error occurs with fabric
python subprocess, for some reason it doesn't stdout.
An error occurs when importing japandas (problem unsolved)
For those who are in trouble with an error when pip install xg boost
[python] When pipenv install of WSL, python on Windows side is called for some reason