OpenCV, Qt, imread, namedWindow, imshow does not work - image

In the .pro file:
QT += core
QT -= gui
TARGET = latihan_2
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += E:\OpenCV\OpenCV\opencv\build\include
LIBS += E:\OpenCV\OpenCV\opencv\build\x86\mingw\lib\libopencv_core246.dll.a
LIBS += E:\OpenCV\OpenCV\opencv\build\x86\mingw\lib\libopencv_highgui246.dll.a
LIBS += E:\OpenCV\OpenCV\opencv\build\x86\mingw\lib\libopencv_imgproc246.dll.a
LIBS += E:\OpenCV\OpenCV\opencv\build\x86\mingw\lib\libopencv_features2d246.dll.a
LIBS += E:\OpenCV\OpenCV\opencv\build\x86\mingw\lib\libopencv_calib3d246.dll.a
In main.cpp:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main(){
//read image
Mat image = imread("img.jpg", 1);
//create image window named "My image"
namedWindow("My Image", CV_WINDOW_AUTOSIZE);
//show the image on window
imshow("My image", image);
//wait key for 5000ms
waitKey(5000);
return 1;
}
When I hit run, there is no error, but it only shows a black window named qtcreator_process_stub.exe.
Why the "My image" window doesn't come out and shows the img.jpg?
I use Qt creator 2.8.1, based on Qt 5.1.1, and openCV-2.4.6.0.

You could also display a cv::Mat on a Qt window. I demonstrate how to do that on cvImage. The code below is adapted from cvImage::_open():
std::string filename = ...
cv::Mat mat = cv::imread(filename);
// Since OpenCV uses BGR order, we need to convert it to RGB
// NOTE: OpenCV 2.x uses CV_BGR2RGB, OpenCV 3.x uses cv::COLOR_BGR2RGB
cv::cvtColor(mat, mat, cv::COLOR_BGR2RGB)
// image is created according to Mat dimensions
QImage image(mat.size().width, mat.size().height, QImage::Format_RGB888);
// Copy cv::Mat to QImage
memcpy(image.scanLine(0), mat.data, static_cast<size_t>(image.width() * image.height() * mat.channels()));
// Display the QImage in a QLabel
QLabel label;
label.setPixmap(QPixmap::fromImage(image));
label.show();

First guess is that the image is in the wrong path, so first test should be to specify the full path to the image.
Also check the return value of your program (make sure the it doesn't return some crash error code - be consistent and return 0 for success and other values for fail).
And a little bit of coding that tells you where the code fails doesn't hurt, so check image.data or you can also use image.empty():
if(! image.data )
{
std::cout << "No image to display";
//can be any other method to display the error: qDebug, a messagebox...
//you can also
return 1;
}
else
{
//use the image
//if nothing goes wrong:
//return 0;
}

Check Projects->Run Settings ->Run in Terminal checkbox. If it is disabled, enable it.

I faced same problem and I solved it by fixing path environment variable. In my path environment variable I added some opencv folders wrongly, then I deleted them and add only bin folder for opencv DLLs and then the problem was solved.

Related

New Tango 3D Reconstruction API

I use the new 3D reconstruction API's (MIRA release). I have a problem when a call the Tango3DR_update function. It returns TANGO_3DR_INVALID code when I set the parameters associated with an image camera (const Tango3DR_ImageBuffer * image * const Tango3DR_Pose image_pose, Tango3DR_CameraCalibration const * calibration). I have checked my parameters, they seem to be correct. When I call this function without image parameters, this to work properly ... Is this a known bug?
thank you in advance for your answers.
TLDR; The support library ImageBufferManager has a bug with strides. Do color_image.stride = image_buffer->width; when creating your Tango3DR_ImageBuffer.
I think there are two things :
Image Format
First, you have to make sure to use the TANGO_HAL_PIXEL_FORMAT_YCrCb_420_SP. You can do that by using the ImageBufferManager from the support library.
ImageBufferManager and strides
Second, there is a catch if you use the support library ImageBufferManager though. TangoSupport_getLatestImageBuffer seems to fail to initialize the stride of the returned image (I got 0 and some other very large values) which the 3DR library doesn't like. The original TangoImageBuffer from OnColorAvailable has stride=1280 (=image_width) and forcing that value on the TangoImageBuffer returned
from the ImageBufferManager seems to fix the issue. I believe this is a bug in ImageBufferManager.
This means doing
color_image.stride = image_buffer->width;
instead of
color_image.stride = image_buffer->stride
when creating the Tango3DR_ImageBuffer.
Full code example
I got it working with the following code in my Render method :
TangoImageBuffer* image_buffer;
ret = TangoSupport_getLatestImageBuffer(
image_buffer_manager_, &image_buffer);
if (ret != TANGO_SUCCESS) {
LOG(ERROR) << "Error in TangoSupport_getLatestImageBuffer";
}
...
Tango3DR_ImageBuffer color_image;
color_image.width = image_buffer->width;
color_image.height = image_buffer->height;
// VERY Important - The support library ImageBufferManager seems to have
// a bug where it will always put the stride of the returned buffer
// at 0, which causes 3DR to fail
color_image.stride = image_buffer->width;
color_image.timestamp = image_buffer->timestamp;
color_image.format = (Tango3DR_ImageFormatType)image_buffer->format;
color_image.data = image_buffer->data;
ret = Tango3DR_update(
tango_3dr_context_,
&cloud,
&depth_pose_3dr,
&color_image,
&color_pose_3dr,
&tango_3dr_calibration_,
&updated_indices);
I am using the ImageManager from the support library. So my OnColorAvailable looks like that
void SynchronizationApplication::OnColorAvailable(
const TangoImageBuffer* buffer) {
if (tango_3dr_enabled_ && tango_3dr_use_color_) {
TangoErrorType ret = TangoSupport_updateImageBuffer(
image_buffer_manager_, buffer);
if (ret != TANGO_SUCCESS) {
LOG(ERROR) << "Error in TangoSupport_updatePointCloud";
}
}
}
And the image_buffer_manager_ is initialized as follow (the pixel format might be important).
TangoSupport_createImageBufferManager(
TANGO_HAL_PIXEL_FORMAT_YCrCb_420_SP,
image_width_,
image_height_,
&image_buffer_manager_
);
I am copying the calibration as follow :
void CopyCalibrationTangoTo3DR(const TangoCameraIntrinsics& tango,
Tango3DR_CameraCalibration* out) {
out->calibration_type =
(Tango3DR_TangoCalibrationType)tango.calibration_type;
out->cx = tango.cx;
out->cy = tango.cy;
memcpy(out->distortion, tango.distortion, sizeof(double) * 5);
out->fx = tango.fx;
out->fy = tango.fy;
out->height = tango.height;
out->width = tango.width;
}

Cannot turn on input method on a SDL2 created window

I wrote a test program for SDL2 text event handling, and the input method cannot be turned on when I'm focusing on the window created by SDL2.
I'm testing that on Linux with Xfce desktop and Fcitx input method engine.
The code is simple:
#include <SDL.h>
int main(int argc, char** argv)
{
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_EVENTS);
SDL_Window* window = SDL_CreateWindow("sdl text test", 50, 50, 400, 400, SDL_WINDOW_OPENGL);
SDL_StartTextInput();
while (1)
{
// process events
SDL_Event event{};
bool should_exit = false;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
should_exit = true;
break;
case SDL_TEXTEDITING:
printf("text edit: %s %d %d\n", event.edit.text, event.edit.start, event.edit.length);
break;
case SDL_TEXTINPUT:
printf("text input: %s\n", event.text.text);
break;
}
}
if (should_exit) break;
SDL_Delay(20);
}
SDL_StopTextInput();
}
I have a brief look on SDL2's documents, but failed to find anything related with keyboard grabbing.
try delete
videodata->ime_uilesss = UILess_SetupSinks(videodata);
in function IME_Init of SDL_windowskeyboard.c.
It's ugly, but works.
If videodata->ime_uiless == SDL_False,
then windows will show input method,
else the sdl2 will render and show input method.
I'm using Windows 10 + SDL and was looking at SDL support for the IME under Windows, and have no idea how it could possibly work considering the IME_Render/IME_Present function are never called in SDL codebase.
Compiling SDL with #define SDL_DISABLE_WINDOWS_IME in SDL_windowskeyboard.c fixed it for me (xwang's suggestion will lead to a similar result).
I don't know why but it may do the trick: build SDL2 yourself.
I once use SDL2 comes with Ubuntu apt repo, libsdl2-dev/bionic-updates,now 2.0.8+dfsg1-1ubuntu1.18.04.1 amd64 . IME cannot be shown or switched.
After I replace with myself-built SDL2 lib(same version 2.0.8), IME works good.

How to enable Box2d debug draw with Coco2d-x 3.0 beta 2

How I should enable debug draw with Cocos2d-x 3.0? There are some codes that do that with cocos2d-x 2.0 but they don't even compile on Cocos2d-x. Unfortunately I am new to both cocos2d-x and Box2d and I don't know how to port them. That would be great if you would share the method how you draw shapes.
EDIT:
I have found this:
http://blog.csdn.net/tian2kong/article/details/20386213
I have overridden the draw method of my applications main Layer like this:
void HelloWorld::draw()
{
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION);
kmGLPushMatrix();
m_world->drawDebugData();
kmGLPopMatrix();
}
And also have done this:
GLESDebugDraw *debugDraw = new GLESDebugDraw(PTM_RATIO);
m_world.SetDebugDraw(debugDraw);
uint32 flags = 0;
flags += b2Draw::e_shapeBit;
/*
flags += b2Draw::e_jointBit;
flags += b2Draw::e_centerOfMassBit;
flags += b2Draw::e_aabbBit;
flags += b2Draw::e_pairBit;
*/
debugDraw->SetFlags(flags);
And it worked! But when I addChild a sprite, the shapes stay blow my sprites. How to bring them front?
Open spotlight and search for GLES-Render.cpp. This file will be there in a path in the cocos2dx folder. Open the enclosing folder and drag the files GLES-Render.h and GLES-Render.cpp to the project.
Then add the following code in the init method
b2Draw *m_debugDraw = new GLESDebugDraw(PTM_RATIO);
uint32 flags = 0;
flags += b2Draw::e_shapeBit;
flags += b2Draw::e_jointBit;
flags += b2Draw::e_aabbBit;
flags += b2Draw::e_pairBit;
flags += b2Draw::e_centerOfMassBit;
m_debugDraw->SetFlags(flags);
H_world->SetDebugDraw(m_debugDraw);
Then add the draw method.
void HelloWorld::draw()
{
kmGLPushMatrix();
H_world->DrawDebugData();
kmGLPopMatrix();
}
Don't forget to include GLES-Render.h
You CANNOT bring bodies to front. if you want to see your bodies behind your sprites you can set their opacity as sprite->setOpacity(100).
i use the following code to draw
void GameLayer:: setPhysics() {
b2Vec2 gravity = b2Vec2(0.0f,0.0f);// Initializing World
world = new b2World(gravity);
m_debugDraw = new GLESDebugDraw( PTM_RATIO );
world->SetDebugDraw(m_debugDraw);
world->SetAllowSleeping(false);
uint32 flags = 0;
flags += b2Draw::e_shapeBit;
// flags += b2Draw::e_jointBit;
// flags += b2Draw::e_aabbBit;
// flags += b2Draw::e_pairBit;
// flags += b2Draw::e_centerOfMassBit;
m_debugDraw->SetFlags(flags);
contactListener = new MyContactListener(this);
world->SetContactListener(contactListener);
}
which is same as yours.... can you please xplain your problem a bit more
For Debug Draw You Need Two File
File Download Link
After that
Inclue that file
and create a Vaiable in .h file
GLESDebugDraw *debugDraw;
and in .ccp file
b2Vec2 gravity;
gravity.Set(0.0f, -9.8f);
this->world = new b2World(gravity);
this->debugDraw = new GLESDebugDraw(PTM_RATIO);
this->world->SetDebugDraw(debugDraw);
uint32 flags = 0 ;
flags += b2Draw::e_shapeBit ;
flags += b2Draw::e_jointBit;
// flags + = b2Draw :: e_aabbBit;
// flags + = b2Draw :: e_pairBit;
flags += b2Draw::e_centerOfMassBit;
this->debugDraw->SetFlags (flags);
in . h file
void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags);
in .cpp
void HelloWorld::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
Layer::draw(renderer, transform, flags);
Director* director = Director::getInstance();
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION );
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
world->DrawDebugData();
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
}
NOW Enjoy Debug draw
You could simply add another layer above your current layer.
And you can add the code you post here in the up layer then everything should be working.

Execution Error using OpenCV for video Capturing

I am facing the following problem when I execute the programme. The program crashed abnormally at line no 1 of the main function. the path of the is all correct, nothing is wrong there. All set-up of the lib-include additional libraries are done perfectly. What could be wrong here?
#include <cv.h>
#include <highgui.h>
int main()
{
//load the video file to the memory
** CvCapture *capture = cvCaptureFromAVI("A.avi"); ** // this instruction crashed the file is there in the folder, that not an issue....
if( !capture ) return 1;
//obtain the frames per seconds of that video
int fps = ( int )cvGetCaptureProperty( capture, CV_CAP_PROP_FPS );
//create a window with the title "Video"
cvNamedWindow("Video");
while(true) {
//grab and retrieve each frames of the video sequencially
IplImage* frame = cvQueryFrame( capture );
if( !frame ) break;
//show the retrieved frame in the "Video" window
cvShowImage( "Video", frame );
int c;
if(fps!=0){
//wait for 1000/fps milliseconds
c = cvWaitKey(1000/fps);
}else{
//wait for 40 milliseconds
c = cvWaitKey(40);
}
//exit the loop if user press "Esc" key (ASCII value of "Esc" is 27)
if((char)c==27 ) break;
}
//destroy the opened window
cvDestroyWindow("Video");
//release memory
cvReleaseCapture( &capture );
return 0;
}
As a sanity check, let's see if the VideoCapture stuff from the OpenCV C++ API works. Let's give this a try:
#include <opencv2/opencv.hpp>
#include <cv.h>
using namespace cv;
VideoCapture capture("A.avi");
Mat matFrame;
capture >> matFrame; //each time you do this, the next video frame goes into the 'matFrame' object
IplImage* frame=cvCloneImage(&(IplImage)matFrame); //convert Mat to IplImage to hook in with your code
I'm mixing the C and C++ OpenCV APIs together here, but I just wanted to help you get it running.

Analyzing camera feed under OSX

I am looking for a way to programmatically analyze a video feed from an external usb webcam under OSX.
Since I haven't done any low level programming like this before I am currently kind of lost on where to start.
How can I access a webcam feed and grab the image data to then process further?
At this point I am just trying to understand the basic concept and am not looking for language-specific solutions.
Any sample code would be highly appreciated.
I'd appreciate it very much if someone could point me in the right direction and help me get started.
Thank you very much in advance!
Thomas
Use OpenCV.
And check my previous answer on this subject if you are looking for a code example to display the webcam images. It converts the video feed to grayscale and displays them on a window:
OpenCV 2.1: Runtime error
If you just want to display the frames, then replace the else block by this:
else
{
cvShowImage("Colored video", color_frame);
}
In case you are wandering how to manipulate the pixels of the frame:
int width = color_frame->width;
int height = color_frame->height;
int bpp = color_frame->nChannels;
for (int i=0; i < width*height*bpp; i+=bpp)
{
if (!(i % (width*bpp))) // print empty line for better readability
std::cout << std::endl;
std::cout << std::dec << "R:" << (int) color_frame->imageData[i] <<
" G:" << (int) color_frame->imageData[i+1] <<
" B:" << (int) color_frame->imageData[i+2] << " ";
}
For quick access to a webcam and for manipulation of pixel data, you can use Processing with the Video library - the easiest way to start is to check out the examples bundled with the IDE.
Processing is a java based visualisation language which is easy to learn and use and works on WIndows, MacOSX and Linux. I found the webcam stuff worked out of the box on my MacBook.
Here is an example script (based on an example bundled in the IDE) which loads a webcam feed and renders the pixels in greyscale.
import processing.video.*;
int numPixels;
Capture video;
void setup() {
// Change size to 320 x 240 if too slow at 640 x 480
size(640, 480, P2D);
video = new Capture(this, width, height, 24);
numPixels = video.width * video.height;
// Make the pixels[] array available for direct manipulation
loadPixels();
}
void draw() {
if (video.available()) {
video.read(); // Read a new video frame
video.loadPixels(); // Make the pixels of video available
for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame...
// Make all the pixels grey if mouse is pressed
if (mousePressed) {
float greyVal = brightness(video.pixels[i]);
pixels[i] = color(greyVal);
} else {
// If mouse not pressed, show normal video
pixels[i] = video.pixels[i];
}
}
updatePixels(); // Notify that the pixels[] array has changed
}
}
Moreover, there is a great interface to OpenCV which can be used for edge detection etc.

Resources