Trying to use G-Api in my Qt class

Hi everyone,
I’m learning the new G-Api module, but I’m having some difficulty using it in an OOP structure using classes and Qt.

I took a look at the OpenCV examples and they are not oop but they write all the code in one main.cpp file. In my case I have an object drawing in which I have * .h and * .cpp files. I get the frames from a video camera and process them in a class that I have called processor. I would like to know how to declare the g-api variables in the header file and process the arriving frames in the cpp file.

I would have a small example if possible.

Thanks in advance,

Greetings

what is your best attempt at a solution?

please show, what you have so far., and explain, what your ui should do.

this sounds like a major design flaw.
you should not need to expose any g-api types or variables to your qt ui (where the header is needed), it’s a matter of “seperation of concern”.

Hi,
I wrote a simple CamProcessor class based in Qt.
The header file is so:

class CamProcessor : public QObject
{
    Q_OBJECT
public:
    CamProcessor(QObject* parent = nullptr);
signals:
    void resultReady(const QVariantMap& result);
public slots:
    void handleImage(const QImage& image);
private:
    cv::GMat m_inImage;
    cv::GMat m_outImage;
    cv::GComputation m_gc;
};

try to do without the member variables.
(if you can’t get away with those, at least use a PIMPL pattern, or interfaces / pure virtual functions on the qt side, and add your variables to the implementation class only)

(also, just saying, … somehow, only noobs think, their current program would have any actual “users”, and that it would need any fancy gui…
if you want to learn about g-api, just do that. don’t overcomplicate it by coupling to some (very special api!) gui design problem)

Hi Berak,
this is what I did so far:

Hi,
I wrote a simple CamProcessor class based in Qt.
The header file is so:

class CamProcessor : public QObject
{
    Q_OBJECT
public:
    CamProcessor(QObject* parent = nullptr);

signals:
    void resultReady(const QVariantMap& result);

public slots:
    void handleImage(const QImage& image);

private:
    cv::GMat m_inImage;
    cv::GMat m_outImage;
    cv::GMat m_resized;
    cv::GComputation m_gc;
};

Then in the ctor of the class i wrote:

CamProcessor::CamProcessor(QObject* parent) : QObject(parent),
      m_gc(m_inImage, m_outImage)
{
    // My basic graph (only for test)
   m_resized  = cv::gapi::resize(m_inImage, cv::Size(), 0.5, 0.5);
    m_outImage = cv::gapi::BGR2Gray(m_resized);
}

And finally in my processor slot I wrote:

void CamProcessor::handleImage(const QImage& image)
{
    cv::Mat inputFrame;
    cv::Mat outputFrame;

    switch (image.format())
    {
    case QImage::Format_ARGB32:
    case QImage::Format_ARGB32_Premultiplied:
        qDebug() << "Image format: Format_ARGB32";
        break;    
    case QImage::Format_RGB32: // This is my case
    {
        cv::Mat mat(image.height(), image.width(), CV_8UC4, const_cast<uchar*>(image.bits()), static_cast<size_t>(image.bytesPerLine()));
        cv::cvtColor(mat, inputFrame, cv::COLOR_BGRA2BGR);
        m_gc.apply(inputFrame, outputFrame);
        cv::imshow("output", outputFrame);
        break;
    }
    case QImage::Format_RGB888:
        qDebug() << "Image format: Format_RGB888";
        break;
    case QImage::Format_Indexed8: // 8-bit, 1 channel
        qDebug() << "Image format: Format_Indexed8";
        break;
    default:
        qWarning() << "QImage format not handled in switch";
        break;
    }
}

Unfortunately my code crash on the m_gc.apply(…) routine and I don’t know how to solve.
Can you give a help?
Regards

again, all your “member” objects are only temporary objects, needed once in the handleImage function. no need for class members

Can you help with a code snippet please?
How to declare the GComputation object without declaring the member variables? The compiler fires errors

read up on “PIMPL pattern”

declaring member variables shouldn’t crash the software or does it?

How can the GComputation be a temporary variable? I don’t understand, can you explain please

Fixed using smart pointers.
Thanx