Ask Your Question

Run OpenCV to MFC

asked 2018-06-15 04:03:52 -0500

flaviu2 gravatar image

updated 2018-06-15 04:38:03 -0500

I have reproduced this sample, in a MFC app.

The cv::Mat is a CDOcument variable member:

// Attributes
std::vector<CBlob> m_blobs;
cv::Mat m_Mat;

and I draw rectangles over image, with an method (called at some time intervals, according FPS rate):

DrawBlobInfoOnImage(m_blobs, m_Mat);

Here is the code of this method:

void CMyDoc::DrawBlobInfoOnImage(std::vector<CBlob>& blobs, cv::Mat& Mat)
for (unsigned int i = 0;i < blobs.size();++i)
    if (blobs[i].m_bStillBeingTracked)
        cv::rectangle(Mat, blobs[i].m_rectCurrentBounding, SCALAR_RED, 2);
        double dFontScale = blobs[i].m_dCurrentDiagonalSize / 60.0;
        int nFontThickness = (int)roundf(dFontScale * 1.0);
        cv::putText(Mat, (LPCTSTR)IntToString(i), blobs[i].m_vecPointCenterPositions.back(), CV_FONT_HERSHEY_SIMPLEX, dFontScale, SCALAR_GREEN, nFontThickness);

but the result of this method is something like that:

image description

My question is: how can I draw only the last blobs result over my image ?

I have tried to clean up m_Mat, and to enable to draw only blobs.size() - 1 blob over image, none of this worked ...

edit retag flag offensive close merge delete



How did you "clean up m_Mat"? What is the code you used? That code probably didn't work correctly. If it is too hard to "undo" a modification (drawing of rectangles), perhaps it is necessary to copy (to create a clone) of the matrix before drawing, using Mat.clone() method

rwong gravatar imagerwong ( 2018-06-15 05:35:26 -0500 )edit

Draw the blobs as a dialog,IOW,do not draw in MAT.after all ,you are using MFC

jsxyhelu gravatar imagejsxyhelu ( 2018-06-22 07:02:59 -0500 )edit

Can you detail a little bit ? These blobs should not be drawn on m_Mat ? But where then ?

P.S. And do you mean with IOW ?

flaviu2 gravatar imageflaviu2 ( 2018-06-28 04:30:46 -0500 )edit

i think ,IOW = in other words

jsxyhelu gravatar imagejsxyhelu ( 2018-07-01 07:32:53 -0500 )edit

2 answers

Sort by » oldest newest most voted

answered 2018-07-01 07:41:32 -0500

actually ,i have a code ,very like what yours. image description

from my blog

the idea is: when we use MFC,in the on_draw() function,we can always draw sth at the image we will show to user.why not draw the rect in on_draw()? may it help you .

edit flag offensive delete link more

answered 2018-07-28 19:31:09 -0500

rwong gravatar image

(1) Check whether you are using GDI or GDI+. (2) In case you are using GDI, here is the documentation for the function you can use to paint a rectangle (but not filling the inside with color):

BOOL Rectangle( HDC hdc, int left, int top, int right, int bottom );

Example usage:

To confirm that your application uses GDI, search your source code for a data type called HDC. This data type is a Handle to a Device Context.

Selecting the color and thickness for the rectangle border is slightly complicated in GDI programming. You need to create a GDI Pen object with the color and thickness, then temporarily swap (or "select", which is a terminology with special meaning in GDI) your Pen with the currently-in-use Pen in the HDC, then issue the Rectangle command to the HDC, and finally restore the original Pen object.

It is best to get a book "GDI programming with Windows 32 API". Without a book, it is hard to learn.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2018-06-15 04:03:52 -0500

Seen: 826 times

Last updated: Jul 28 '18