cv2.cvtColor错误:( - 1515)scn == 3 ||函数cv :: cvtColor中的scn == 4

时间:2015-01-29 23:00:49

标签: python opencv

我正在尝试使用kmeans聚类来从图像中获取最常见的颜色。它适用于本地图像,但返回此错误与从网址中提取图像的新功能。这是抛出错误的行的代码:

# 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)

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

也许你可以尝试一下,只需要从代码中进行一些改动..

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)