Ask Your Question
0

How to call imshow("", cv::Mat) from a function passed to void* ptr of trackbar callback?

asked 2018-07-23 05:48:23 -0600

Kroll gravatar image

I have a local cv::Mat image in my class member function. Then I make some settings to create a named window and a trackbar.

I need to pass contents of this local image variable to the handler function for threshold it by slider and cv::imshow("window", thrshld);

My tries gave me only segfaults. I used lambdas, std::function and std::shared_ptr<cv::mat>.

Please, help!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-07-23 07:02:09 -0600

updated 2018-07-23 07:03:32 -0600

What you basically do is creating a void* to the image, then inside your handler you do

void onMouse(int event, int x, int y, int flags, void* param)
{
    Mat src;
    src = *((Mat*)param);

   ...

and in source code you do

setMouseCallback("source", onMouse, &src);

or if you really do not care about copies and optimized code, just make a global Mat element and address it from everywhere. Thats how the tutorials do it.

edit flag offensive delete link more

Comments

I catched the void* param and this image have 0x0 size. With same momory address of class member cv::Mat I passed to void* param. But before createTrackbar(...) and callback it was good cv::Mat displayable image.

Kroll gravatar imageKroll ( 2018-07-23 07:39:35 -0600 )edit

Provide your full code, there must be something you are still doing wrong.

StevenPuttemans gravatar imageStevenPuttemans ( 2018-07-23 07:59:11 -0600 )edit

It is qite bit complicated to provide it clean and interesting part. I create a member cv::VideoCapture in first object of one class which is in main.cpp thread, then I call a read(cv::Mat) of this object from std::thread loop of second object of another class, in this loop I make some objects and image transformations then call cv::imshow, in those third objects I trying to call theirs cv::imshow and cv::createTrackbar to see their intermediate results and tune by slider some parameters like a threshold etc. Now I find out that if cv::Mats I pass to cv::imshow are local then the images will be shown well, but if cv::Mat is a reference to a member it goes to display of empty image or errors and segfaults.

Kroll gravatar imageKroll ( 2018-07-24 01:53:44 -0600 )edit

Like this QObject::startTimer: Timers cannot be started from another thread if i call cv::waitKey(30).

Kroll gravatar imageKroll ( 2018-07-24 01:54:15 -0600 )edit

But that are no OpenCV but QT related issues it seems. On top of that, even if it is complicated to provide clean code, again, without something to test, no-one will be able to put you on the bug.

StevenPuttemans gravatar imageStevenPuttemans ( 2018-07-24 02:07:47 -0600 )edit

I have no Qt in this project. I find out intresting thing for my purpose. Basicaly I need to wedge to a part of code and display an intermediate result image. But no need to pass havy data to the createTrackbar. All I need to do is pass a reference to a variable which will be interconnected with a gui slider. I will just change it from gui. All cv calculations and imshow is need to do from outside of cv::TrackerCallback. I.e. need to use cv::createTrackbar(string, string, int*, int) without a cv::TrackerCallback :) All examples I found were about something happen in cv::TrackerCallback but actualy is no need to bring part of code to cv::TrackerCallback scope. Less other thread calculations - less errors. And namedWindow & createTrackbar must be called once in constructor.

Kroll gravatar imageKroll ( 2018-07-24 03:40:32 -0600 )edit

@Kroll i guess we misunderstand eachother. Again, your explanation seems reasonable, but I cannot do anything useful based on text. About the no QT, thats incorrect. My guess, you had QT installed on your system and compiled OpenCV with QT support, because the QObject error is a QT thread issue.

StevenPuttemans gravatar imageStevenPuttemans ( 2018-07-24 03:49:44 -0600 )edit

@StevenPuttemans I know that OpenCV with Qt support but just noted it is 100% not my Qt error. I don't know in which cases OpenCV can print this error. I opened another question with code :) It may clarify this situation too.

Kroll gravatar imageKroll ( 2018-07-24 09:36:56 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-07-23 05:43:52 -0600

Seen: 929 times

Last updated: Jul 23 '18