OpenCV Error: Assertion failed when using COLOR_BGR2GRAY function

2024/10/5 13:22:01

I'm having a weird issue with opencv. I have no issues when working in a jupyter notebook but do when trying to run this Sublime.

The error is: OpenCV Error: Assertion failed (depth == CV_8U || depth == CV_16U || depth == CV_32F) in cvtColor, file /Users/jenkins/miniconda/1/x64/conda-bld/work/opencv-3.1.0/modules/imgproc/src/color.cpp, line 7935

import numpy as np 
import cv2img = [[[150,160,170], [150,32, 199], [145, 212, 234], [145, 212, 234]], [[150,190,170], [150,32, 199], [145, 212, 234], [145, 212, 234]],[[150,160,170], [150,32, 199], [145, 212, 234], [145, 212, 234]],[[150,160,170], [150,32, 199], [145, 212, 234], [145, 212, 234]]]img = np.array(img)def grayscale(x):# plt.imshow(gray, cmap='gray')to show on screen# turns dim from (32,32,3) to (32,32)return cv2.cvtColor(x, cv2.COLOR_BGR2GRAY)img2 = grayscale(img)
Answer

You need to specify the data type when you create the array.

When I try this code here, and check the dtype of img, I see the following:

>>> img.dtype
dtype('int32')

That doesn't match the requirements of cv2.cvtColor.

The range of values you initialize your image with appears to fall into 0-255, which would correspond to data type uint8.

So, just do

img = np.array(img, dtype=np.uint8)
https://en.xdnf.cn/q/70483.html

Related Q&A

matplotlib 1.3.1 has requirement numpy=1.5, but youll have numpy 1.8.0rc1 which is incompatible

Im executing bellow command in Mac (High Sierra) as a part of getting started with pyAudioAnalysis.pip install numpy matplotlib scipy sklearn hmmlearn simplejson eyed3 pydub Im getting following error…

VS Code Debugger Immediately Exits

I use VS Code for a python project but recently whenever I launch the debugger it immediately exits. The debug UI will pop up for half a second then disappear. I cant hit a breakpoint no matter where i…

Sudoku Checker in Python

I am trying to create a sudoku checker in python:ill_formed = [[5,3,4,6,7,8,9,1,2],[6,7,2,1,9,5,3,4,8],[1,9,8,3,4,2,5,6,7],[8,5,9,7,6,1,4,2,3],[4,2,6,8,5,3,7,9], # <---[7,1,3,9,2,4,8,5,6],[9,6,1,5,…

Subclassing numpy scalar types

Im trying to subclass numpy.complex64 in order to make use of the way numpy stores the data, (contiguous, alternating real and imaginary part) but use my own __add__, __sub__, ... routines.My problem i…

Python file IO w vs wb [duplicate]

This question already has answers here:What does wb mean in this code, using Python?(5 answers)Closed 10 years ago.Wondering what the real difference is when writing files from Python. From what I can…

How to extract hour, minute and second from Series filled with datetime.time values

Data:0 09:30:38 1 13:40:27 2 18:05:24 3 04:58:08 4 09:00:09Essentially what Id like to do is split this into three columns [hour, minute, second]Ive tried the following code but none see…

Getting an error attachment_filename does not exist in my docker environment

Due to some reasons this particular code is not working in docker but it works fine in development environment. I am getting error "TypeError: send_file() got an unexpected keyword argument attach…

How to check if a process with Command line argument is running using python

I would like to check if a script is running with a specific command line argument within a python script.For example I would like to check if:main.py testargIs running. Is there any way I can achieve …

cx_oracle and python 2.7 [duplicate]

This question already has answers here:Python module "cx_Oracle" module could not be found(4 answers)Closed 5 years ago.Im using python 2.7 and cx_oracle ( Windows x86 Installer (Oracle 10g, …

Inheritance in Python C++ extension

I have c++ library that need communicate with Python plugged in modules. Communication supposes implementing by Python some callback c++ interface. I have read already about writing extensions, but no …