2017-11-11 75 views
-3
files2 = [f for f in listdir(dstpath) if isfile(join(dstpath,f))]   
for image in files2: 
    img = cv2.imread(os.path.join(dstpath,image)) 
    equ = cv2.equalizeHist(img) 
    dstPath2 = join(dstpath,image) 
    cv2.imwrite(dstPath2,equ) 

我有jpg格式,包括灰度圖像的文件夾,但是當我跑我上面的代碼對於直方圖均衡,它給了我上面提到的錯誤。請幫助histogram.cpp:3915:錯誤:(-215)_src.type()== CV_8UC1在功能簡歷:: equalizeHist

+0

請參見[如何提問](https://stackoverflow.com/help/how-詢問)並相應地編輯問題。其中一部分就是給出一個可以理解的標題,並在問題主體中包含完整的追溯。 – roganjosh

+0

使用年度調試工具驗證圖像是否已加載。 – flamelite

回答

0

imread默認情況下在彩色模式下加載圖像。嘗試使用img = cv2.imread(your_image_path,cv2.IMREAD_GRAYSCALE)代替

+0

現在它甚至不會將原始圖像轉換爲灰度 –

0
@author: Quantum 
""" 
import cv2 
import os 
from os import listdir,makedirs 
from os.path import isfile,join 


path = r'' # Source Folder 
dstpath = r'' # Destination Folder 


try: 
    makedirs(dstpath) 
except: 
    print ("Directory already exist, images will be written in asme folder") 

# Folder won't used 
files = [f for f in listdir(path) if isfile(join(path,f))] 

for image in files: 
    try: 
     img = cv2.imread(os.path.join(path,image),cv2.IMREAD_GRAYSCALE) 
     **imgnew=cv2.equalizeHist(img)** 
     dstPath = join(dstpath,image) 
     cv2.imwrite(dstPath,imgnew) 
    except: 
     print ("{} is not converted".format(image)) 

我所做的只是添加了histeq功能,而我的文件轉換成灰度