0
import cv2
import os


for filename in os.listdir('C:/Users/Akhil/Downloads/New'):
     image = cv2.imread(filename)
      gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
      cv2.imwrite('gray_image.png',gray_image)
      cv2.imshow('color_image',image)
      cv2.imshow('gray_image',gray_image) 
      cv2.waitKey(0)                 
     cv2.destroyAllWindows()

I am importing all the images from a directory as shown above and turning each image into a grey scale image. But when I run this code, I am facing the following error:

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv::ipp_cvtColor, file C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\imgproc\src\color.cpp, line 7456
Traceback (most recent call last):
  File "11.py", line 7, in <module>
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.error: C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\imgproc\src\color.cpp:7456: error: (-215) scn == 3 || scn == 4 in function cv::ipp_cvtColor
sytech
  • 29,298
  • 3
  • 45
  • 86
dv akhil
  • 93
  • 8
  • 1
    There are a few different questions that have been asked related to this same error message. See [this question](http://stackoverflow.com/questions/30506126/open-cv-error-215-scn-3-scn-4-in-function-cvtcolor) which mentions you can open an image in grey-scale by `cv2.imread(filename, 0)` -- Does that help you at all? – sytech Oct 17 '16 at 12:41
  • You should have a look at the glob module. files=glob.glob("./*.png") will give you a list of paths to your png files in the current folder – kezzos Oct 17 '16 at 12:52
  • @kezzos I agree with you but what is there are different formats like jpg png etc.... – dv akhil Oct 17 '16 at 13:23
  • You would simply concatenate the lists e.g. files=glob.glob("*.png") + glob.glob("*.jpg") etc. – kezzos Oct 17 '16 at 14:10

1 Answers1

1

This is often a symptom of failing to find the image.

Note that the filenames returned by os.listdir() are relative to the directory passed to listdir(). So OpenCV is searching for a file named "image_01.jpg" for example, in your current working directory, and can't find anything. But instead of failing, it returns an empty image.

Try path_to_image = os.path.join(my_image_folder, filename) and imread that.

Jacob Panikulam
  • 1,196
  • 1
  • 9
  • 12