cv2.cvtColor error: (-215) scn == 3 || scn == 4 in function cv::cvtColor

匿名 (未验证) 提交于 2019-12-03 09:02:45

问题:

I'm attempting to use kmeans clustering to get the most common colors out of an image. It works fine with local images, but returns this error with the new functionality of pulling an image from a url. Here's the code up to the line that is throwing the error:

# import the necessary packages from sklearn.cluster import KMeans import numpy as np import urllib import argparse import utils import cv2  def getCommonColors(url):      req = urllib.urlopen(url)     arr = np.asarray(bytearray(req.read()), dtype=np.uint8)     img = cv2.imdecode(arr,-1)       image = cv2.imread(np.array_str(img))     image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) 

Any help would be greatly appreciated!

回答1:

maybe you could try it, just some change from your code..

import numpy as np import urllib2        #maybe requests is another good choice import cv2  def getCommonColors(url):      req = urllib2.urlopen(url)     arr = np.asarray(bytearray(req.read()), dtype=np.uint8)     img = cv2.imdecode(arr,-1)      # image = cv2.imread(np.array_str(img))   <-- I think you shoudn't use this method, it will return NoneType in python     image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!