ROS Image subscriber - window popup does not appear - opencv

I am following image subscribe tutorial on official ROS page. when I run my_subscriber no window popup appears. I type -
rosrun image_transport_tutorial my_subscriber
output is -
init done
opengl support available
And then nothing happens. (even the output "init done" is unexplained because there is not output line in my_subscriber.cpp)
I am following this tutorial - ROS tutorial
I have roscore and rosrun image_transport_tutorial my_publisher pathtofile already running in dofferent terminals.
I checked that publisher is publishing by running command
rostopic list -v
my_subscriber file has the following contents -
#include <ros/ros.h>
#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <opencv2/highgui/highgui.hpp>
#include <cv_bridge/cv_bridge.h>
#include <stdio.h>
void imageCallback(const sensor_msgs::ImageConstPtr& msg)
{
try
{
cv::imshow("view", cv_bridge::toCvShare(msg, "bgr8")->image);
}
catch (cv_bridge::Exception& e)
{
ROS_ERROR("Could not convert from '%s' to 'bgr8'.", msg->encoding.c_str());
}
}
int main(int argc, char **argv)
{
std::cout<<"kapil";
ros::init(argc, argv, "image_listener");
ros::NodeHandle nh;
cv::namedWindow("view");
cv::startWindowThread();
image_transport::ImageTransport it(nh);
image_transport::Subscriber sub = it.subscribe("camera/image", 1, imageCallback);
ros::spin();
cv::destroyWindow("view");
}
Solved : I added waitKey function in the try block as suggested in one of the answers.
cv::waitKey(30);

According to the comment to this answer, using cv::startWindowThread() does not always work. Maybe this is the issue in your case.
Try to add cv::waitKey(10) after cv::imshow instead. This will wait for some key press for 10 milliseconds, giving the window time to show the image.
(This always seemed to me like a dirty hack, but it is the common way to show images in OpenCV...)

Related

'select timeout' for opencv application while running on yocto linux environment

We are trying to port opencv application on an single board computer which runs on Yocto Linux and while doing that we have had many issues. Issues related to drivers, which are solved by changing the configuration file of the kernel and all.
We were able to solve all those issues and finally, we are hitting the 'select timeout' error during below highlight line in the snapshot code.
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core.hpp>
#include <opencv2/core/core.hpp>
using namespace std;
int main(int, char**) {
cv::VideoCapture camera(0);
if (!camera.isOpened()) {
return 1;
}
cv::Mat frame;
while (camera.isOpened()) {
***camera >> frame;***
}
return 0;
}
This code does work on my local machine, but not on the SBC. Are there any leads for this issue to solve? I would be happy to share more details if required.
P.S.
Camera is well detected and thus not throwing any error at !camera.isOpened() code.
It gives the error 'select timeout' error at camera >> frame line.

Can't run simple Emscripten thread example in Firefox

I'd like to compile the following C++ snippet using threads with Emscripten
#include <cstdio>
#include <thread>
void foo() { puts("foo\n"); }
void bar(int x) { printf("bar %d\n", x); }
int main(int argc, char* argv[]) {
std::thread first(foo);
std::thread second(bar, 123);
puts("main, foo and bar now execute concurrently...\n");
first.join();
second.join();
puts("foo and bar completed.\n");
}
To get Emscripten to use threads I pass the "-s USE_PTHREADS=1" option during compilation and linking
target_compile_options(
Emscripten PRIVATE -v "SHELL:-s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=8")
target_link_options(
Emscripten PRIVATE -v --emrun
"SHELL:-s MINIFY_HTML=0 -s USE_PTHREADS=1 -s PTHREAD_POOL_SIZE=8")
I also change the suffix of the compilation output to .html which leads to Emscripten directly producing a .html page
set(CMAKE_EXECUTABLE_SUFFIX ".html")
So far so good. The snippet compiles as expected and I can see the toolchain passing some pthread arguments on the command line.
In order to test the output I fire up a webserver with python and open the .html page with Firefox. The Firefox option javascript.options.shared_memory was enabled by default in my version (78.0.1) so I thought that my code should work out of the box. Sadly this isn't the case and I'm getting Uncaught ReferenceError: SharedArrayBuffer is not defined exceptions thrown at me through the console.
Testing the code with node works though:
$ node --experimental-wasm-threads --experimental-wasm-bulk-memory Emscripten.js
main, foo and bar now execute concurrently...
foo
bar 123
foo and bar completed.
/edit
Ok, seems like SharedArrayBuffer still undergoes some standardization process...
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer/Planned_changes

How do I construct a syntax-only match-finder tool?

I am building an ASTMatcher-based tool that I would like to run over my sources:
int main(int argc, const char** argv) {
CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());
MatchFinder Finder;
// Repeated calls to Finder.addMatcher(...);
Tool.run(newFrontendActionFactory(&Finder).get());
// Handle the results of the matching.
}
Running this over a source file that depends on other headers yields the following error:
~$ /path/to/my/tool /path/to/my/file.cpp --
/path/to/my/file.cpp:8:10: fatal error: 'string' file not found
#include <string>
^~~~~~~~
1 error generated.
Error while processing /path/to/my/file.cpp.
I do not want to include any other headers in this processing, lest my matchers find content in those headers that I do not want to handle.
I tried passing -fsyntax_only to the tool, but I get the same result as above:
~$ /path/to/my/tool /path/to/my/file.cpp -- -fsyntax-only
I noticed in the ASTMatcher tutorial that there is a clang::SyntaxOnlyAction. However, I have been unable to figure out how MatchFinder and SyntaxOnlyAction can be used in conjunction with one another. Likewise, I have been able to do an AST dump from the command line of the same file, no problem, so I know it's possible.
Is it possible to configure a MatchFinder-based tool to honor the syntax-only behavior?

opencv 2.4.6: can't open the webcam, invalid exception handler (Windows 7, VS2013)

I have a problem in opening my usb webcam using opencv, actually I can't even open the webcam. For instance also the following code doesn't work:
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/legacy/legacy.hpp"
using namespace cv;
using namespace std;
int main(void){
VideoCapture c(0);
}
The error has this form:
"First-chance exception at 0x775370CF (ntdll.dll) in blabla.exe: 0xC0000008: An invalid handle was specified."
I'm in debug mode (it has the same result in release mode) and i'm using pre-compiled opencv libs (that I guess had been compiled with VS2012). I have no idea.
As it is said in http://www.mattmontag.com/development/notes-on-using-opencv-2-3-with-visual-studio-2010
"If everything compiles and then you get runtime errors at the first instance of an OpenCV function call, something like this:
First-chance exception at 0x7c90e4ff in OpenCVHello.exe: 0xC0000008: An invalid handle was specified.
This might be a bug in the OpenCV build, I don't know. You can disable it by going to Debug > Exceptions, unfold Win32 exceptions, and uncheck 0xC0000008."

webcam usage in opencv

I am new with opencv.
I want to capture images from webcam (intex it-105wc).
I am using Microsoft visual c++ 2008 express edition on windows xp.
There is no problem with build solution, but when i try to debug the code it gives following (this happens wwhile executing cvCaptureFromCAM(CV_CAP_ANY);)
Loaded C:\Program Files\Common Files\Ahead\DSFilter\NeVideo.ax, Binary was not built with debug information.
and then exits the code.
so, is there any problem with my code or is it compatibility issue with webcam??
#include "stdafx.h"
#include<stdio.h>
#include <cv.h>
#include <highgui.h>
void main(int argc,char *argv[])
{
int c;
IplImage* color_img;
CvCapture* cv_cap = cvCaptureFromCAM(CV_CAP_ANY);
if(!cv_cap)
{
printf( "ERROR: Capture is null!\n");
}
cvNamedWindow("Video",0); // create window
for(;;)
{
color_img = cvQueryFrame(cv_cap); // get frame
if(color_img != 0)
cvShowImage("Video", color_img); // show frame
c = cvWaitKey(10); // wait 10 ms or for key stroke
if(c == 27)
break; // if ESC, break and quit
}
/* clean up */
cvReleaseCapture( &cv_cap );
cvDestroyWindow("Video");
}
This error message seems to be related to a video codec from the nero burning tools.
If you do not need this codec, you could just unregister it and see, if that solves your problem.
To do that, execute the following on the commandline:
regsvr32 /u "C:\Program Files\Common Files\Ahead\DSFilter\NeVideo.ax"
You should see the message:
DllUnregisterServer in C:\Program Files\Common Files\Ahead\DSFilter\NeVideo.ax succeeded.
To undo this, execute
regsvr32 "C:\Program Files\Common Files\Ahead\DSFilter\NeVideo.ax"

Resources