# Trying to use G-Api in my Qt class

**URL:** <https://forum.opencv.org/t/trying-to-use-g-api-in-my-qt-class/333>\
**Category:** Uncategorized\
**Tags:** gapi\
**Created:** [December 15, 2020, 2:23pm UTC](https://forum.opencv.org/t/trying-to-use-g-api-in-my-qt-class/333 "2020-12-15T14:23:24Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![franz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/franz/32/10968_2.png) [@franz](https://forum.opencv.org/u/franz)\
**Post date:** [December 15, 2020, 2:23pm UTC](https://forum.opencv.org/t/trying-to-use-g-api-in-my-qt-class/333/1 "2020-12-15T14:23:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [December 15, 2020, 3:06pm UTC](https://forum.opencv.org/t/trying-to-use-g-api-in-my-qt-class/333/2 "2020-12-15T15:06:09Z")

</div>

what is your best attempt at a solution?

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [December 15, 2020, 4:34pm UTC](https://forum.opencv.org/t/trying-to-use-g-api-in-my-qt-class/333/3 "2020-12-15T16:34:29Z")

</div>

> [@franz](#):
>
> I get the frames from a video camera and process them in a class that I have called processor.

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”.

---

<div class="post-metadata">

**Author:** ![franz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/franz/32/10968_2.png) [@franz](https://forum.opencv.org/u/franz)\
**Post date:** [December 15, 2020, 4:31pm UTC](https://forum.opencv.org/t/trying-to-use-g-api-in-my-qt-class/333/4 "2020-12-15T16:31:54Z")

</div>

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

```auto
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;
};

```

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [December 15, 2020, 4:42pm UTC](https://forum.opencv.org/t/trying-to-use-g-api-in-my-qt-class/333/5 "2020-12-15T16:42:07Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![franz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/franz/32/10968_2.png) [@franz](https://forum.opencv.org/u/franz)\
**Post date:** [December 15, 2020, 4:52pm UTC](https://forum.opencv.org/t/trying-to-use-g-api-in-my-qt-class/333/6 "2020-12-15T16:52:31Z")

</div>

Hi Berak,  
this is what I did so far:

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

```auto
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:

```auto
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:

```auto
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

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [December 15, 2020, 4:54pm UTC](https://forum.opencv.org/t/trying-to-use-g-api-in-my-qt-class/333/7 "2020-12-15T16:54:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![franz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/franz/32/10968_2.png) [@franz](https://forum.opencv.org/u/franz)\
**Post date:** [December 15, 2020, 4:55pm UTC](https://forum.opencv.org/t/trying-to-use-g-api-in-my-qt-class/333/8 "2020-12-15T16:55:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [December 15, 2020, 4:56pm UTC](https://forum.opencv.org/t/trying-to-use-g-api-in-my-qt-class/333/9 "2020-12-15T16:56:10Z")

</div>

read up on “PIMPL pattern”

---

<div class="post-metadata">

**Author:** ![franz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/franz/32/10968_2.png) [@franz](https://forum.opencv.org/u/franz)\
**Post date:** [December 15, 2020, 5:01pm UTC](https://forum.opencv.org/t/trying-to-use-g-api-in-my-qt-class/333/10 "2020-12-15T17:01:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![franz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/franz/32/10968_2.png) [@franz](https://forum.opencv.org/u/franz)\
**Post date:** [December 15, 2020, 6:06pm UTC](https://forum.opencv.org/t/trying-to-use-g-api-in-my-qt-class/333/11 "2020-12-15T18:06:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![franz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/franz/32/10968_2.png) [@franz](https://forum.opencv.org/u/franz)\
**Post date:** [December 16, 2020, 5:28pm UTC](https://forum.opencv.org/t/trying-to-use-g-api-in-my-qt-class/333/12 "2020-12-16T17:28:54Z")

</div>

Fixed using smart pointers.  
Thanx
