cv2.cvtColor错误:(-215)scn == 3 || scn == 4在函数cv :: cvtColor中 [英] cv2.cvtColor error: (-215) scn == 3 || scn == 4 in function cv::cvtColor

查看:318
本文介绍了cv2.cvtColor错误:(-215)scn == 3 || scn == 4在函数cv :: cvtColor中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用kmeans聚类来获取图像中最常见的颜色.它适用于本地图像,但是通过从url中提取图像的新功能返回此错误.这是引发错误的代码行:

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)

任何帮助将不胜感激!

推荐答案

也许您可以尝试一下,只需对您的代码进行一些更改即可.

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)

这篇关于cv2.cvtColor错误:(-215)scn == 3 || scn == 4在函数cv :: cvtColor中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆