RESOLVED: [MacOS] OpenCV error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

Hello, Malcolm ))))

Traceback (most recent call last):
  File "/project1/script1_callbacks", line 18, in onCook
cv2.error: OpenCV(4.5.0) /Users/malcolm/Desktop/devel/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

Script TOP content:

# me - this DAT
# scriptOp - the OP which is cooking

import cv2
import numpy as np

# press 'Setup Parameters' in the OP to call this function to re-create the parameters.
def onSetupParameters(scriptOp):
	return

# called whenever custom pulse parameter is pushed
def onPulse(par):
	return

def onCook(scriptOp):
	img = op('null1').numpyArray(delayed=True)
	gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)

	gray = np.float32(gray)
	dst = cv2.cornerHarris(gray,2,3,0.04)

	#result is dilated for marking the corners, not important
	dst = cv2.dilate(dst,None)

	# Threshold for an optimal value, it may vary depending on the image.
	img[dst>0.01*dst.max()]=[0,0,255,255]

    # THIS CODE CALLS THE ERROR BUT NOT WHICH EXPECTED
	cv2.imshow('dst',img)
	if cv2.waitKey(0) & 0xff == 27:
		cv2.destroyAllWindows()
    # END ERROR AREA
	return

I just copy-pasted the simple example from Harris Corner Detector in OpenCV. Just like in Feature Tracking with Script TOP and OpenCV in TouchDesigner - Tutorial by Elburz Sorkhabi.

I just forget to remove drawing code in the end and get that error. When I removed that, the error is disappears.

I don’t know is it alright or a bug…


Mac OS 11.0.1, Touchdesigner Experimental 2020.45520 (JAN) and 2020.44350 (DEC). I don’t know is there are that error in older versions.

UPD: Now, the problem not in that code area. I remove it.
Now I see that error happens every time when source TOP node (‘null1’) resolution changes.

  1. Movie In TOP → Fit TOP → null1 TOP
  2. Change resolution to half → see the error
  3. Change any parameter in Fit TOP removes error

UPD2: if I connect null1 TOP → Script TOP I can’t see the error

I think you need to turn the RGBA into RGB and cast the type to uint8. I also think that the waitKey thing doesn’t work well in TouchDesigner so I disabled it.

# me - this DAT
# scriptOp - the OP which is cooking

import cv2
import numpy as np
cv2.namedWindow('dst',cv2.WINDOW_NORMAL)

# press 'Setup Parameters' in the OP to call this function to re-create the parameters.
def onSetupParameters(scriptOp):
	return

# called whenever custom pulse parameter is pushed
def onPulse(par):
	return

def onCook(scriptOp):
	img = op('null1').numpyArray(delayed=True)
	img = img[:,:,:3]
	gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)

	gray = np.float32(gray)
	dst = cv2.cornerHarris(gray,2,3,0.04)

	#result is dilated for marking the corners, not important
	dst = cv2.dilate(dst,None)

	# Threshold for an optimal value, it may vary depending on the image.
	img[dst>0.01*dst.max()]=[0,0,255]

    # THIS CODE CALLS THE ERROR BUT NOT WHICH EXPECTED
	cv2.imshow('dst',img.astype(np.uint8))
	#if cv2.waitKey(0) & 0xff == 27:
	#	cv2.destroyAllWindows()
    # END ERROR AREA
	return

I mean the
‘OpenCV(4.5.0) /Users/malcolm/Desktop/devel/opencv/modules/imgproc/src/color.cpp:182: e’
is the absolute path for developer, not for the other users.

That path just get’s baked into the .dlls since that’s where I built it. The error is occurring firing due to the Python code, causing an assert in the .dll to go off. The. dll is reporting where in the original C++ code the assert is located, just cause that’s all it knows. You can ignore the path and just focus on the error message.

1 Like