Camera freezes on first frame with " Invalid number of channels in input image" in logcat

Another newbie question here. I’m running object recognition on an android phone. This code came from a tutorial:

extern "C" void Detect(Circle * outFaces, int maxOutFacesCount, int& outDetectedFacesCount)
	Mat frame;
	_capture >> frame;

	if (frame.empty())

	std::vector<Rect> faces;

	Mat grayscaleFrame;

	cvtColor(frame, grayscaleFrame, COLOR_BGR2GRAY);
	Mat resizedGray;
	resize(grayscaleFrame, resizedGray, Size(frame.cols / _scale, frame.rows / _scale));
	equalizeHist(resizedGray, resizedGray);

	_faceCascade.detectMultiScale(resizedGray, faces);

	for (size_t i = 0; i < faces.size(); i++)
		Point center(_scale * (faces[i].x + faces[i].width / 2), _scale * (faces[i].y + faces[i].height / 2));
		ellipse(frame, center, Size(_scale * faces[i].width / 2, _scale * faces[i].height / 2), 0, 0, 360, Scalar(0, 0, 255), 4, 8, 0);

		outFaces[i] = Circle(faces[i].x, faces[i].y, faces[i].width / 2);

		if (outDetectedFacesCount == maxOutFacesCount)

	imshow(_windowName, frame);

Googling has shown me that it’s stopping at cvtColor. My logcat shows this:

44-22270/? E/cv::error(): OpenCV(4.5.3-pre) Error: Unspecified error (> Invalid number of channels in input image:
    >     'VScn::contains(scn)'
    > where
    >     'scn' is 1
    ) in cv::impl::(anonymous namespace)::CvtHelper<cv::impl::(anonymous namespace)::Set<3, 4, -1>, cv::impl::(anonymous namespace)::Set<1, -1, -1>, cv::impl::(anonymous namespace)::Set<0, 2, 5>, cv::impl::(anonymous namespace)::NONE>::CvtHelper(cv::InputArray, cv::OutputArray, int) [VScn = cv::impl::(anonymous namespace)::Set<3, 4, -1>, VDcn = cv::impl::(anonymous namespace)::Set<1, -1, -1>, VDepth = cv::impl::(anonymous namespace)::Set<0, 2, 5>, sizePolicy = cv::impl::(anonymous namespace)::NONE], file C:\Users\mjpg7\nxxxx-pxxxxx\OpenCV\opencv-master\modules\imgproc\src/color.simd_helpers.hpp, line 92
2021-06-14 14:03:10.692 22244-22270/? E/CRASH: signal 6 (SIGABRT), code -1 (?), fault addr --------

(that’s the beginning and hopefully the important part)

Is there something wrong with that call to cvtColor as it stands for the current version of OpenCV? All it’s doing is reading my phone camera (and freezing no matter what), the front one.

extern C and references? is that C++ code? that won’t work.

anyway, you pass a 1-channel image to cvtColor ('scn' is 1) while asking it to convert from a 3-channel image.

check the number of channels on frame.

It’s for a plugin to the platform-that-shall-not-be-named. That part does its job, seemingly. It freezes the camera in the middle of that bundle of code, I think.

were you the one with the yuv420p data? what I saw ought to be yuv422p

fascinating: C++ by-reference argument and C linkage - Stack Overflow

I don’t think so? I’m brand new to this and only just got the cascade files working at all…
anyhow, so it thinks I’m passing a black and white image to cvtcolor? That’s weird since my camera is full color rgb and the intention is to make the snapshot grayscale. Hm.

Interestingly, this code exactly as-is has been proven to work for the Windows version of the plugin. It’s from a Packt book, and other online tutorials use it. Of course, those were for OpenCV 3.4.1…

In any case, I worked around it by just checking if the frame had more than one channels (which…I still don’t understand why the frame of my android camera registers as having one channel, but it’s not stopping it from working anymore…)

it might have “one channel” if the data is some strange format, maybe some YUV, maybe compressed.

feel free to post the data from such a 1-channel frame, either as bytes or some attempt to interpret it as a picture.