Reputation: 79
The following is the code i had entered in the Spyder Environment :
import os
import cv2
import numpy as np
path1="E:\\academic\\FINAL YR PROJ\\PROJECT_DATASETS\\floyd_jan\\dr"
path2="E:\\academic\\FINAL YR PROJ\\PROJECT_DATASETS\\floyd_jan\\greendr"
names=[]
names=os.listdir(path1)
for i in names:
bgr = cv2.imread(path1+"\\"+i,1)
green = bgr[: , : , 1]
lab = cv2.cvtColor(green, cv2.COLOR_BGR2LAB)
lab_planes = cv2.split(lab)
clahe = cv2.createCLAHE(clipLimit=2.0,tileGridSize=(8,8))
lab_planes[0] = clahe.apply(lab_planes[0])
lab = cv2.merge(lab_planes)
bgr = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)
cv2.imwrite(path2+"\\"+i,bgr)
I am getting the following error on running the code :
Traceback (most recent call last):
File "", line 8, in lab = cv2.cvtColor(green, cv2.COLOR_BGR2LAB)
error: C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:10724: error: (-215) (scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F) in function cv::cvtColor
Unable to figure out the solution!
Upvotes: 0
Views: 976
Reputation: 1
Try referring to this link, open cv error: (-215) scn == 3 || scn == 4 in function cvtColor . Changing the back slash to forward slash solved the error for me.
Upvotes: 0
Reputation: 536
Print the name of the file prior to reading it with imread
. That should give a fair idea about which file is being read by your program.
If you are sure that all the files in the directory are images, it's mostly likely caused by desktop.ini
, a hidden file being read by imread. In that case, imread
will return None and bgr will be of NoneType, which cannot be understood by cvtColor
function.
Put a condition
if bgr:
...
EDIT : Probably, desktop.ini is not the issue. If it was, then you should have gotten an error at line 7 where you tried extracting the green channel. As pointed out by @sgarizvi, you are passing grayscale value to cvtColor.
Upvotes: 0
Reputation: 16796
The problem is in the part where you are calling the color-space conversion function
green = bgr[: , : , 1]
lab = cv2.cvtColor(green, cv2.COLOR_BGR2LAB)
You are using a gray-scale (single channel) image green
to perform a color-space conversion (cv2.COLOR_BGR2LAB
) which is intended for color images (3 channels).
What you should be doing instead is to use bgr
in place of green
as an input for cv2.cvtColor
.
lab = cv2.cvtColor(bgr, cv2.COLOR_BGR2LAB)
Upvotes: 1