Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid number of channels in input image

I am receiving an error when running my program, I think specifically about color manipulation in the OpenCV library.

I'm trying to build a program that takes a video feed from a Raspberry Pi camera and analyze it. I want to find the brightest point in the video and calculate the distance and angle the point is at from the center of the video feed.

The project I am doing has the camera pointed at the center of a dark box, with a moving point of light.

I am using OpenCV 4.0.0 and C++ on a Raspberry Pi 3, as well as the raspicam library.

I am taking pointers from this guide, but I am using C++ and a video feed instead of Python and a static image.

    raspicam::RaspiCam_Cv Camera;
    cv::Mat image;
    cv::Mat gray;
    int nCount=100;
    int nR, nC;         // numRows, numCols
    cv::Point imgMid;
    Vect toCenter;

    // for recording brightest part of img
    double minVal, maxVal;
    cv::Point minLoc, maxLoc;

    Camera.set(cv::CAP_PROP_FORMAT, CV_8UC1);

    #ifdef DEBUG
    cout << "Opening camera..." << endl;
    if (!Camera.open()) {
        cerr << "Error opening the camera" << endl;
        return -1;
    }
    cout << "Capturing " << nCount << " frames ...." << endl;
    #endif

    for (int i=0; i< nCount; i++) {
        Camera.grab();
        Camera.retrieve(image);
        nR = image.rows;
        nC = image.cols;
        imgMid.x = nC / 2;
        imgMid.y = nR / 2;

        // convert to grayscale image
        cv::cvtColor(image, gray, cv::COLOR_BGR2GRAY);

        // find x, y coord of brightest part of img
        cv::minMaxLoc(gray, &minVal, &maxVal, &minLoc, &maxLoc);

        // calculate vector to the center of the camera
        toCenter.first = distBtwn(imgMid.x, maxLoc.x, imgMid.y, maxLoc.y);
        toCenter.second = angle(imgMid.x, maxLoc.x, imgMid.y, maxLoc.y);

I expect the program to take a frame of the video feed, convert it to grayscale, find the brightest part of the frame, and finally do some calculations to find the distance to the center of the frame and angle of the point from the positive x-axis.

This is the error

I apologize for the phone camera, but I am working with somebody else in a different city, where they have the testing equipment (I am the coder) and this is what they sent me.

like image 933
Deditionis Avatar asked Jun 07 '19 05:06

Deditionis


People also ask

How can I tell how many channels a photo has?

If len(img. shape) gives you three, third element gives you number of channels.


2 Answers

As error message said, the image given in input to the color conversion function has an invalid number of channels.

The point is that you are acquiring frames as single 8bit channel

Camera.set(cv::CAP_PROP_FORMAT, CV_8UC1)

and then you try to convert this frame in grayscale

cv::cvtColor(image, gray, cv::COLOR_BGR2GRAY)

You have 2 easy options to solve this issue:

  1. you change the camera acquisition format in order to have color information in your frames, for example using CV_32S or CV_32F
  2. you skip the color conversion as you already have grayscale image, thus no need to convert it.

Take a look to this link for OpenCV color manipulation

like image 146
Mauro Dorni Avatar answered Sep 19 '22 20:09

Mauro Dorni


You want to do color manipulation but your image has the type CV_8U1. It has to be at least a three channel image like CV_8UC3 or CV_32F. Try a different CV_Type

Camera.set(cv::CAP_PROP_FORMAT, CV_32F);
like image 34
Muhammed Yücel Avatar answered Sep 21 '22 20:09

Muhammed Yücel