I am translating an image im by tx and ty pixels.
def translate(im, tx, ty):
height, width = im.shape[:2]
transform_matrix = np.array([[1, 0, tx], [0, 1, ty]])
translation = cv2.warpAffine(src=im, M=transform_matrix, dsize=(width, height))
However, it fails with the following error,
cv2.error: OpenCV(4.6.0) /Users/runner/work/opencv-python/opencv-python/opencv/modules/imgproc/src/imgwarp.cpp:2604: error: (-215:Assertion failed) (M0.type() == CV_32F || M0.type() == CV_64F) && M0.rows == 2 && M0.cols == 3 in function 'warpAffine'
I did double check that my image is in int type and has the correct dimensions, but couldn't figure out which part is the problem.
Related
I have 800 images stored in numpy array of size (800, 3,256, 256), which I want to convert to (800, 1,256, 256).
I tried using cv2 library to achieve this but I am getting an error. What the easiest way to implement this?
train_img[i]= cv2.cvtColor(train_img[i], cv2.COLOR_BGR2RGB)
train_img[i]= cv2.cvtColor(train_img[i], cv2.COLOR_BGR2GRAY)
Traceback (most recent call last):
File "<ipython-input-82-e993ce67611f>", line 3, in <module>
train_img[i]= cv2.cvtColor(train_img[i], cv2.COLOR_BGR2RGB)
error: C:\ci\opencv_1512688052760\work\modules\imgproc\src\color.cpp:11010: error: (-215) depth == 0 || depth == 2 || depth == 5 in function cv::cvtColor
OpenCV Error: Assertion failed (depth == 0 || depth == 2 || depth == 5) in cv::cvtColor, file C:\ci\opencv_1512688052760\work\modules\imgproc\src\color.cpp, line 11010
OpenCV Error: Assertion failed (depth == 0 || depth == 2 || depth == 5) in cv::cvtColor, file C:\ci\opencv_1512688052760\work\modules\imgproc\src\color.cpp, line 11010
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv::cvtColor, file C:\ci\opencv_1512688052760\work\modules\imgproc\src\color.cpp, line 11016
OpenCV Error: Bad number of channels (Source image must have 1, 3 or 4 channels) in cvConvertImage, file C:\ci\opencv_1512688052760\work\modules\imgcodecs\src\utils.cpp, line 622
OpenCV Error: Bad number of channels (Source image must have 1, 3 or 4 channels) in cvConvertImage, file C:\ci\opencv_1512688052760\work\modules\imgcodecs\src\utils.cpp, line 622
OpenCV Error: Bad number of channels (Source image must have 1, 3 or 4 channels) in cvConvertImage, file C:\ci\opencv_1512688052760\work\modules\imgcodecs\src\utils.cpp, line 622
OpenCV Error: Bad number of channels (Source image must have 1, 3 or 4 channels) in cvConvertImage, file C:\ci\opencv_1512688052760\work\modules\imgcodecs\src\utils.cpp, line 622
OpenCV Error: Assertion failed (depth == 0 || depth == 2 || depth == 5) in cv::cvtColor, file C:\ci\opencv_1512688052760\work\modules\imgproc\src\color.cpp, line 11010
OpenCV Error: Assertion failed (depth == 0 || depth == 2 || depth == 5) in cv::cvtColor, file C:\ci\opencv_1512688052760\work\modules\imgproc\src\color.cpp, line 11010
OpenCV Error: Assertion failed (depth == 0 || depth == 2 || depth == 5) in cv::cvtColor, file C:\ci\opencv_1512688052760\work\modules\imgproc\src\color.cpp, line 11010
OpenCV Error: Assertion failed (depth == 0 || depth == 2 || depth == 5) in cv::cvtColor, file C:\ci\opencv_1512688052760\work\modules\imgproc\src\color.cpp, line 11010
OpenCV Error: Assertion failed (depth == 0 || depth == 2 || depth == 5) in cv::cvtColor, file C:\ci\opencv_1512688052760\work\modules\imgproc\src\color.cpp, line 11010
OpenCV Error: Assertion failed (depth == 0 || depth == 2 || depth == 5) in cv::cvtColor, file C:\ci\opencv_1512688052760\work\modules\imgproc\src\color.cpp, line 11010
2020-11-19 05:55:59.003947: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2020-11-19 05:56:13.086709: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
2020-11-19 05:56:13.088369: E tensorflow/stream_executor/cuda/cuda_driver.cc:351] failed call to cuInit: UNKNOWN ERROR (303)
2020-11-19 05:56:13.108395: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: DESKTOP-FAJP7DN
2020-11-19 05:56:13.109348: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: DESKTOP-FAJP7DN
2020-11-19 05:56:13.125396: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
OpenCV Error: Assertion failed (depth == 0 || depth == 2 || depth == 5) in cv::cvtColor, file C:\ci\opencv_1512688052760\work\modules\imgproc\src\color.cpp, line 11010
OpenCV Error: Assertion failed (depth == 0 || depth == 2 || depth == 5) in cv::cvtColor, file C:\ci\opencv_1512688052760\work\modules\imgproc\src\color.cpp, line 11010
OpenCV Error: Assertion failed (depth == 0 || depth == 2 || depth == 5) in cv::cvtColor, file C:\ci\opencv_1512688052760\work\modules\imgproc\src\color.cpp, line 11010
As pointed by Elyas Karimi cv2.cvtColor expects the input images to be 256x256x3 and not 3x256x256. You can transpose the images back and forth.
However, since converting from RGB to gray is a rather simple operation:
0.2989 * R + 0.5870 * G + 0.1140 * B
You can explicitly and efficiently do the conversion:
# assuming your input is in BGR order
train_img = 0.289 * train_img[:, 2, ...] + 0.587 * train_img[:, 1, ...] + 0.114 * train_img[:, 0, ...]
In many cases the exact weight of each channel is not too critical, and a simple mean over channels can do just fine:
train_img = train_img.mean(axis=1)
Correct me if i am wrong, you have images of 256*256 and they have 3 channels making it 256*256*3
Now you want to make them one channeled as 256*26*1.
By losing channel information, you are converting RGB/BGR image into grey scale. Now to do that with OpenCV you will need to make them 256*256*3, it wont work on 3*256*256
So to do that lets take
from numpy import moveaxis
#image[i].shape=3*256*256
data = moveaxis(image[i], 2, 0)
#data.shape=256*256*3
Now pass this into OpenCv cvt.color
data= cv2.cvtColor(data, cv2.COLOR_BGR2RGB)
data= cv2.cvtColor(data, cv2.COLOR_BGR2GRAY)
OR Use this
data = 0.289 * data[:, 2, ...] + 0.587 * data[:, 1, ...] + 0.114 * data[:, 0, ...]
This question already has an answer here:
Python-OpenCV cv2 OpenCV Error: Assertion failed
(1 answer)
Closed 2 years ago.
I use openCV(4.1.2) in python 3.7 to process my photos to MNIST format in windows 7.
First I want to resize my photo to 28*28, and then convert it to grayscale.
Finally, I want to store the converted photos.
I use the following code:
def resize(img):
img = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
h = img.shape[0]
w = img.shape[1]
p = max(h,w)/28
if h > w:
resize_h = 28
resize_w = w/p
else:
resize_w = 28
resize_h = h/p
img_resized = cv.resize(img, (int(resize_h), int(resize_w)), interpolation = cv.INTER_AREA)
img_resized = cv.resize(img, (28, 28), interpolation = cv.INTER_AREA)
return img_resized
def load_data(path):
idx = 0
total_imgs = len([img_name for img_name in os.listdir(path) if img_name.endswith('.PNG')])
data = np.zeros((total_imgs,28,28), dtype=np.uint8)
for img_name in os.listdir(path):
if not img_name.endswith('.PNG'):
continue
img_path = os.path.join(path, img_name)
img = cv.imread(img_path)
resized_img = resize(img)
data[idx, :]=resized_img
idx+=1
return data
data = load_data('D:\\EPS_projects\\AI\\2_CV\\cifar-10\\img\\0')
cv.imwrite("D:\\EPS_projects\\AI\\2_CV\\MNIST\\work200306\\0\\1_im.PNG", data);
But when I run this piece of code, error occurs at the last line when I tried to use cv.imwrite to save the converted photos.
The error is: error: OpenCV(4.1.2) C:\projects\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:668: error: (-215:Assertion failed) image.channels() == 1 || image.channels() == 3 || image.channels() == 4 in function 'cv::imwrite_'
How to solve my problem?
(-215:Assertion failed) image.channels() == 1 || image.channels() == 3 || image.channels() == 4
The expression after the brackets is the assertion expression -- i.e. an expression that must be true to be able to proceed.
This specific expression says that the data that you are passing must have the shape of either a monochrome image (1 channel), a color image (3 channels) or a color image with alpha-channel (4 channels). So fix the shape of the data that you are passing accordingly.
There is a bug on mask bounds in cv2.seamlessClone. It produces on mask filled with 255 only on edges.
Does anyone new how to solve it?
Now I ignore images when I need to blend only edges.
import cv2
import numpy as np
background = np.zeros((100, 100, 3)).astype(np.uint8)
target_object = np.ones_like(background).astype(np.uint8) * 255
mask = np.zeros_like(target_object)
bound = -1
mask[:, bound:] = 255
center = (mask.shape[0] // 2, mask.shape[1] // 2)
cv2.seamlessClone(target_object, background, mask, center, flags = cv2.NORMAL_CLONE)
# bound:
# -1, -2 : "terminate called after throwing an instance of 'std::length_error'
# what(): vector::_M_default_append"
# -3 : "cv2.error: OpenCV(4.0.0) /io/opencv/modules/core/src/matrix_wrap.cpp:1669: error: (-215:Assertion failed) !fixedSize() in function 'release'"
# <= -4 : works
I've opened issue for this bug in opencv
https://github.com/opencv/opencv/issues/15294
As workaround for your issue i suggest manually add 1 pixel on borders.
I am trying to subtract the background from an image using a mask and OpenCV's bitwise_and. However, I get the following error:
error:
C:\ci\opencv_1512684736357\work\modules\core\src\arithm.cpp:241:
error: (-215) (mtype == 0 || mtype == 1) && _mask.sameSize(*psrc1) in
function cv::binary_op
My code looks like this:
mask = get_mask() #function that returns a mask (boolean)
#conversion of the mask
mask = mask.astype('int')
mask[mask == 0] = 255
mask[mask == 1] = 0
fg_masked = cv2.bitwise_and(img, img, mask=mask)
A question here on StackOverflow (OpenCV Python Error: error: (-215) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1) in function cv::binary_op) that addresses the same error indicates an issue with a potential shape mismatch. However, checking both the shape of my mask and my image it appears to me that they match, yielding:
mask.shape
OUT: (100, 83)
img.shape
OUT: (100, 83, 3)
I am using Python v3 and OpenCV v2
The problem is not a shape mismatch... it is failing in the first part of the assert:
mtype == 0 || mtype == 1
It says that the type of the mask (mtype) should be either 0 or 1, i.e. CV_8U and CV_8S respectively.
You are using:
mask = mask.astype('int')
That means type CV_32S or 4 in the enum value.
Solution:
You can use np.uint8 or np.int8 assuming that you have done import numpy as np
I wish to use ORB (http://docs.opencv.org/3.1.0/d1/d89/tutorial_py_orb.html#gsc.tab=0) on a 28*28 grayscale image (handwritten digits), where each pixel has a number from 0 to 255.
This is the code I used:
# image = {load the array of 754 numbers}
orb = cv2.ORB_create()
image = image.reshape(28, 28))
kp = orb.detect(image, None)
But I keep getting this error:
OpenCV Error: Assertion failed (depth == CV_8U || depth == CV_16U || depth == CV_32F) in cvtColor, file /home/yahya/Documents/_other_downloaded_apps/opencv/modules/imgproc/src/color.cpp, line 7935
Traceback (most recent call last):
File "/home/yahya/Documents/hello.py", line 118, in <module>
kp = orb.detect(image, None)
cv2.error: /home/yahya/Documents/_other_downloaded_apps/opencv/modules/imgproc/src/color.cpp:7935: error: (-215) depth == CV_8U || depth == CV_16U || depth == CV_32F in function cvtColor
How can I do this and why am I getting this error?
UPDATE
I seemed to have solved part of this problem. It turns out that orb accepts float32 numbers (not 64).
Therefore I updated my code as follows:
orb = cv2.ORB_create()
image = feature_x[0].reshape(28, 28).astype('float32')
kp = orb.detect(image, None)
But now I have the following error:
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in ipp_cvtColor, file /home/yahya/Documents/_other_downloaded_apps/opencv/modules/imgproc/src/color.cpp, line 7456
Traceback (most recent call last):
File "/home/yahya/Documents/hello.py", line 188, in <module>
kp = orb.detect(image, None)
cv2.error: /home/yahya/Documents/_other_downloaded_apps/opencv/modules/imgproc/src/color.cpp:7456: error: (-215) scn == 3 || scn == 4 in function ipp_cvtColor
The image you are trying to load isn't compatible type for the orb. You should convert it first before using it. Also you don't need reshape if you are loading it into numpy array
orb = cv2.ORB_create()
image = image.astype(np.uint8, copy=False)
kp = orb.detect(image, None)