Ask Your Question
0

How to fix this error: grayscaled = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) cv2.error: /io/opencv/modules/imgproc/src/color.cpp:10638: error: (-215) scn == 3 || scn == 4 in function cvtColor?

asked 2017-11-28 20:24:47 -0600

sjnegi gravatar image

updated 2017-11-28 21:33:16 -0600

berak gravatar image

My code is :

import cv2
import numpy as np

img = cv2.imread('bookpage.jpg')
retval, threshold = cv2.threshold(img, 12, 255, cv2.THRESH_BINARY)

grayscaled = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
retval2, threshold2 = cv2.threshold(grayscaled, 12, 255, cv2.THRESH_BINARY)
gaus = cv2.adaptiveThreshold(grayscaled, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 115, 1)


cv2.imshow('original', img)
cv2.imshow('threshold', threshold)
cv2.imshow('threshold2', threshold2)
cv2.imshow('gaus', gaus)
cv2.waitKey(0)
cv2.destroyAllWindows()
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2017-11-29 03:50:43 -0600

supra56 gravatar image

@ sjnegi. I have tested 2 codes. [1]Your code is working. [2] add parameter zero or one img = cv2.imread('bookpage.jpg',1). On mine side, I used ones.

edit flag offensive delete link more
0

answered 2017-11-28 21:33:33 -0600

berak gravatar image

updated 2017-11-28 21:43:07 -0600

noobs NEVER check, if the image was actually read...

it should be:

img = cv2.imread('bookpage.jpg')
if img != None: # wrong image path
     # any other operation
edit flag offensive delete link more

Comments

The code is correct! The error is in img = None or invalid, then this answer is correct!!

arigarcia gravatar imagearigarcia ( 2017-11-29 06:33:49 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-11-28 20:24:47 -0600

Seen: 3,038 times

Last updated: Nov 29 '17