Ask Your Question
0

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor,

asked 2017-09-29 03:18:19 -0600

papabiceps gravatar image

I'm trying to run a code that does template matching. I'm getting assertion failed error on cvtColor function.

Here is the error.

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/travis/miniconda/conda-
bld/conda_1485299292920/work/opencv-3.2.0/modules/imgproc/src/color.cpp, line 9748
Traceback (most recent call last):
File "opencv_basics.py", line 60, in <module>
img_gray = cv2.cvtColor(img_rgb,cv2.COLOR_BGR2GRAY)
cv2.error: /home/travis/miniconda/conda-bld/conda_1485299292920/work/opencv-
3.2.0/modules/imgproc/src/color.cpp:9748: error: (-215) scn == 3 || scn == 4 in function cvtColor

Here is the code I'm trying to run.

import cv2
import numpy as np

img_rgb = cv2.imread('template.jpg')
img_gray = cv2.cvtColor(img_rgb,cv2.COLOR_BGR2GRAY)

template = cv2.imread('opencv-template-for-matching.jpg',0)
w, h = template.shape[::-1]

res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where( res >= threshold)

for pt in zip(*loc[::-1]):
    cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,255,255), 2)

cv2.imshow('Detected',img_rgb)
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2017-09-29 03:36:08 -0600

berak gravatar image

this is probably the most common rookie error there is ...

you're loading an image, but never check the outcome.

since the error came from cvtColor, obviously img_rgb was never loaded (wrong path)

lesson here: whenever you load anything, check !

img_rgb = cv2.imread('template.jpg')
if img_rgb == None: 
    raise Exception("could not load image !")
edit flag offensive delete link more

Comments

So I found out that the image is not getting loaded. What am I supposed to do? I checked the path, the image is there, but the it not getting loaded.

papabiceps gravatar imagepapabiceps ( 2017-09-29 03:40:47 -0600 )edit
1

How can I be so..... It was in the wrong path.

papabiceps gravatar imagepapabiceps ( 2017-09-29 03:46:06 -0600 )edit

try an absolute path, or, if it is relative, it is relative to where you start your program

(this is not an opencv problem at all, but a basic resource handling one)

berak gravatar imageberak ( 2017-09-29 03:51:11 -0600 )edit
0

answered 2017-09-29 07:30:27 -0600

supra56 gravatar image

There is nothing wrong with filename. Try this:

import numpy as np
import cv2

image = cv2.imread('photo.jpg')
template = cv2.imread('template.jpg')

# resize images
image = cv2.resize(image, (0,0), fx=0.5, fy=0.5) 
template = cv2.resize(template, (0,0), fx=0.5, fy=0.5) 

# Convert to grayscale
imageGray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
templateGray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)

# Find template
result = cv2.matchTemplate(imageGray,templateGray, cv2.TM_CCOEFF)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
top_left = max_loc
h,w = templateGray.shape
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(image,top_left, bottom_right,(0,0,255),4)

# Show result
cv2.imshow("Template", template)
cv2.imshow("Result", image)

cv2.moveWindow("Template", 10, 50);
cv2.moveWindow("Result", 150, 50);

cv2.waitKey(0)
edit flag offensive delete link more

Comments

Btw, I'm using raspberry pi 3, picamera version 2, Debian Strecth version 9, python 3.5 and OpenCV 3.3.0.

supra56 gravatar imagesupra56 ( 2017-09-29 07:47:31 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-09-29 03:18:19 -0600

Seen: 39,020 times

Last updated: Sep 29 '17