打开cv2将彩色图像转换为灰度

时间:2018-08-24 15:49:58

标签: python opencv image-processing

我正在尝试使用XAMPP服务器和python 3.7使用Opencv2库将彩色图像转换为灰度,并且在Web上的结果是:

   no image --> -->


error   Python 3.7.0: C:\Users\Asus\AppData\Local\Programs\Python\Python37-32\python.exe
Fri Aug 24 21:58:51 2018
A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.

 C:\xampp\htdocs\cv2\rgbtogray.py in <module>()
     17         print ("no image")
     18 
=>   19 gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
     20 cv2.imshow('gray',gray)
     21 cv2.waitKey(0)
gray undefined, cv2 = <module 'cv2' from 'C:\\Users\\Asus\\AppData\\Lo...on37-32\\lib\\site-packages\\cv2.cp37-win32.pyd'>, cv2.cvtColor = <built-in function cvtColor>, img = None, cv2.COLOR_BGR2GRAY = 6
error: OpenCV(3.4.2) d:\build\opencv\opencv-3.4.2\modules\imgproc\src\color.hpp:253: error: (-215:Assertion failed) VScn::contains(scn) && VDcn::contains(dcn) && VDepth::contains(depth) in function 'cv::CvtHelper<struct cv::Set<3,4,-1>,struct cv::Set<1,-1,-1>,struct cv::Set<0,2,5>,2>::CvtHelper' 
      args = ("OpenCV(3.4.2) d:\\build\\opencv\\opencv-3.4.2\\modul...et<1,-1,-1>,struct cv::Set<0,2,5>,2>::CvtHelper'\n",) 

这是我使用xampp执行的代码

#!C:\Users\Asus\AppData\Local\Programs\Python\Python37-32\python.exe

print('Content-type: text/html\r\n')
print('\r\n')

import cgitb
import cv2
import numpy as np

cgitb.enable()


img = cv2.imread('C:\\xampp\\htdocs\\cv2\\color.png')
if img is None:
    print ("no image")

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow('gray',gray)
cv2.waitKey(0)
cv2.destroyAllWindows()

here is the image i want to convert

1 个答案:

答案 0 :(得分:0)

您的路径中avergeIncreace(df,'A') indexes = data[value].pct_change().fillna(0).gt(0).index.values #this returns an array containing all of the index (true and false) answer = 0 times = 0 for x in indexes: answer += (data[value][x] - data[value][x-1]) times += 1 print( answer/times ) (\b(\w+ (\s\w+)*)\b){1,20} 的组合充当转义符,Python认为它正在加载十六进制值。

您只需在文件路径之前添加\即可将其转换为原始字符串,如下所示:

x

这将阻止r中的\ x充当转义序列。

请参阅此处以供参考:https://python-reference.readthedocs.io/en/latest/docs/str/escapes.html