0

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!

3 Answers3

1

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)
sgarizvi
  • 16,623
  • 9
  • 64
  • 98
  • Yes, I get it what you intend to say, but I want to work with green channel alone and not the RGB image, in the sense that I want to perform image processing techniques on green channel image only. In that case what should I edit in the code.. I basically want to perform CLAHE on the green image. – Kshitij Zutshi Feb 10 '18 at 04:59
  • @KshitijZutshi... Well, if you want to use only the green channel ,there is no need to convert color space and split the channels of LAB image. Just `apply` CLAHE on the green channel which has been extracted using the statement `green = bgr[:,:,1]`.. – sgarizvi Feb 10 '18 at 08:19
0

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.

Sagar B Hathwar
  • 536
  • 6
  • 18
0

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.

  • Well, the slash is not the problem indeed in my case, I am aware of the windows- python issue with slashes.. the problem is with performing image processing techniques on the single green channel alone from the given RGB image.. – Kshitij Zutshi Feb 10 '18 at 05:01