Can not read/write an image in opencv - c++

I have this code to create and write an image in opencv:
char * outImage="C:/tmp/000014/outimage.jpg";
Mat gray_image(imageWidth,imageHeight, CV_8UC1, Scalar(255));
imwrite( outImage, gray_image );
but when I am running thid code, I am getting error that file extension is not known and hence iwrite can not write image.
What is wrong with this code?
edit1
I changed the code to see there is a problem with code or installation.
so I have this code now
Mat x=imread("C:/tmp/a.jpg");
imshow("Image", x);
waitKey(0);
I am sure that the file c:/tmp/a.jpg exist.
When I run the application, I am getting this error:
OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in unknown function, file C:\slave\builds\WinInstallerMegaPack\src\opencv\modules\core\src\array.cpp, line 2482
C:\slave\builds\WinInstallerMegaPack\src\opencv\modules\core\src\array.cpp:2482: error: (-206) Unrecognized or unsupported array type
I followed this answer in so to install OpenCV o my system (I use vc11, insteade of vc10 during installation) Installing OpenCV 2.4.3 in Visual C++ 2010 Express
edit 2
The problem was that I mixed the debug and release libraries,
For release use release libraries which don't have d at the end (for example opencv_calib3d243.lib
instead od opencv_calib3d243d.lib)
for debug use debug libraries which has d at the end for example opencv_calib3d243d.lib
Make sure that you are using the correct lib name based on the opencv version that you have for example opencv_calib3d245d.lib for opencv 2.4.5.

Make sure you have not mixed up release and debug DLL's of OpenCV. Plus check whether x86 or x64 DLL's would work. Sometimes x64 DLL's do mess up on x64 machines. Dont know why but I had to switch to x86 version.

Try this:
void write(){
char * outImage="C:/tmp/000014/outimage.jpg";
Mat img = Mat(Size(imageWidth,imageHeight), CV_8UC1, Scalar(255));
imwrite(out, img);
}
Please be sure of your output path, imageWidth,imageHeight.
MoreOver check your dependance (you need libjpeg8 under Linux):
For Linux
sudo apt-get install libpng12-0 libpng12-dev libpng++-dev libpng3 libpnglite-dev libpngwriter0-dev libpngwriter0c2 zlib1g-dbg zlib1g zlib1g-dev pngtools libjasper-dev libjasper-runtime libjasper1 libjpeg8 libjpeg8-dbg libjpeg62 libjpeg62-dev libjpeg-progs libtiff4-dev libtiff4 libtiffxx0c2 libtiff-tools ffmpeg libavcodec-dev libavcodec52 libavformat52 libavformat-dev libswscale0 libswscale-dev openexr libopenexr6 libopenexr-dev
For Windows follow that guide:
http://docs.opencv.org/doc/tutorials/introduction/windows_install/windows_install.html

Related

Allegro5 cannot decode png images on Ubuntu 20.04 (using cmake)

Allegro5 cannot seem to be able to decode png images for my game project. This function:
ALLEGRO_BITMAP* BrushCollection::LoadBitmapFromFile(const char* fileName)
{
ALLEGRO_PATH* path = al_get_standard_path(ALLEGRO_EXENAME_PATH);
al_append_path_component(path, "assets");
al_set_path_filename(path, fileName);
ALLEGRO_BITMAP* bitmap = al_load_bitmap(al_path_cstr(path, '\\'));
allegro_init(bitmap, L"UI Bitmap");
return bitmap;
}
returns NULL. I know image paths are correct because the same code works on Windows.
Related to this question: here
I'm having this same issue in Ubuntu 20.04 with the precompiled allegro 5.2.7 package! I added png as an external library to the project. I do have libpng16-16 and libpng-dev installed in my system, but allegro can't seem to be able to decode png images!
This is the command generated by cmake by the way, which seems to be including libpng in the linking:
/usr/bin/c++ -g CMakeFiles/Planetaire.dir/src/main.cpp.o CMakeFiles/Planetaire.dir/src/Game.cpp.o CMakeFiles/Planetaire.dir/src/brush/BrushCollection.cpp.o CMakeFiles/Planetaire.dir/src/events/EventStack.cpp.o CMakeFiles/Planetaire.dir/src/events/MouseEvents.cpp.o CMakeFiles/Planetaire.dir/src/text/FontCollection.cpp.o CMakeFiles/Planetaire.dir/src/tools/AllegroInit.cpp.o CMakeFiles/Planetaire.dir/src/tools/Debug.cpp.o CMakeFiles/Planetaire.dir/src/ui/actions/ContinueGame.cpp.o CMakeFiles/Planetaire.dir/src/ui/actions/HideInGameMenu.cpp.o CMakeFiles/Planetaire.dir/src/ui/actions/HideMainMenu.cpp.o CMakeFiles/Planetaire.dir/src/ui/actions/QuitGame.cpp.o CMakeFiles/Planetaire.dir/src/ui/actions/QuitToDesktop.cpp.o CMakeFiles/Planetaire.dir/src/ui/actions/ShowInGameMenu.cpp.o CMakeFiles/Planetaire.dir/src/ui/actions/ShowMainMenu.cpp.o CMakeFiles/Planetaire.dir/src/ui/actions/StartNewGame.cpp.o CMakeFiles/Planetaire.dir/src/ui/components/AbstractComponent.cpp.o CMakeFiles/Planetaire.dir/src/ui/components/InGameMenu.cpp.o CMakeFiles/Planetaire.dir/src/ui/components/MainMenu.cpp.o CMakeFiles/Planetaire.dir/src/ui/AbstractUIElement.cpp.o CMakeFiles/Planetaire.dir/src/ui/ButtonElement.cpp.o CMakeFiles/Planetaire.dir/src/ui/ImageElement.cpp.o CMakeFiles/Planetaire.dir/src/ui/MainUIElement.cpp.o CMakeFiles/Planetaire.dir/src/ui/PanelElement.cpp.o CMakeFiles/Planetaire.dir/src/world-generator/Terrain.cpp.o -o Planetaire -lallegro -lallegro_main -lallegro_font -lallegro_ttf -lallegro_image -lallegro_primitives /usr/lib/x86_64-linux-gnu/libpng.so
I wonder what I'm doing wrong here. This works out of the box on windows.
I think I know the answer!
The path is simply wrong, when doing:
ALLEGRO_BITMAP* bitmap = al_load_bitmap(al_path_cstr(path, '\\'));
This is building a backward slash path. Linux/Mac can't find this file. When I corrected this path, the application worked!

OpenCV 3.0 install WITH_OPENCL_SVM=ON on Machine with OpenCL 1.1

So I installed OpenCV WITH_OPENCL_SVM=ON thinking that I was going to eventually get a GPU with OpenCL 2.0 on it (currently only 1.1.) However now when I try to run any programs with OpenCV I get an
Error on line 2629 (ocl.cpp): CL_DEVICE_SVM_CAPABILITIES via clGetDeviceInfo failed: -30
I believe when I create a cv::UMat. The line of code in ocl.cpp is dependent on HAVE_OPENCL_SVM being defined, I assume it then does a check for the SVM capability and fails that check because I don't have 2.0. I tried:
#undef HAVE_OPENCL_SVM
in my code and modifying cvconfig.h (honestly don't know how/when that file is referenced) so that it is not defined as well as un-defining it there again... and the error persists.
Thanks!

How to use LibVLC with Qt 5

I'm trying to use LibVLC in a Qt 5 program to open a VLC instance and play a video.
The following code comes from https://wiki.videolan.org/LibVLC_Tutorial/
I'm using Linux.
.pro :
TEMPLATE = app
TARGET = projectLoic
INCLUDEPATH += . vlc
QT += widgets
# Input
HEADERS +=
SOURCES += main.cpp
LIBS +=-lvlc
main :
#include <vlc/vlc.h>
#include <QApplication>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
libvlc_instance_t * inst;
libvlc_media_player_t *mp;
libvlc_media_t *m;
// Load the VLC engine
inst = libvlc_new(0, NULL);
// Create a new item
m = libvlc_media_new_path (inst, "/home/........mp3");
// Create a media player playing environement
mp = libvlc_media_player_new_from_media (m);
// play the media_player
libvlc_media_player_play (mp);
return app.exec();
}
The compilation is fine. But the program immediatly crashes when I build it (with Qt Creator). Any idea?
Many thanks
Many things could cause this crash. The best is to get VLC source code to trace back the issue. Passing the option '--verbose=2' when initializing libVLC can help as well.
In my case the cause of the crash was due to this bug in the ubuntu package of vlc:
https://bugs.launchpad.net/ubuntu/+source/vlc/+bug/1328466
When calling libvlc_new() vlc modules and their dependent libraries are loaded into memory. The qt module of LibVLC was searching for Qt4 shared objects instead of Qt5 (manually installed).
The solution was to rebuild the module cache which was outdated an pointing to Qt4 binaries. You can reset it on the command line:
sudo /usr/lib/vlc/vlc-cache-gen -f /usr/lib/vlc/plugins/
Or pass to vlc the option:
--reset-plugins-cache
I have never used this library, but Are you using exactly this code?
m = libvlc_media_new_path (inst, "/home/........mp3");
This path may be the problem.
What distribution of Linux are you using?
I ran into this same problem with Qt5 and LibVLC, and the primary cause (Ubuntu 12.04 LTS and 14.04 LTS), was that LibVLC was loading the qt4 interface plugin, which conflicted with Qt5. If you check your call stack, you'll most likely see that at a Qt4 library was being loaded, causing the crash.
If this is the problem, there are only 3 options (tested with LibVLC 2.2 and Qt5 on Ubuntu 12.04 and 14.04 LTS 64 bit).
The first (worst) is to delete the qt4 user interface plugin. You can test this is the problem by moving and running and then setting it back. Deleting will break your regular VLC player most likely.
Second option is to create a copy of the plugins directory and set that in your runtime path using VLC_PLUGIN_PATH, environment variable. but I've had trouble getting that to work without changing the original plugin path folder also (which will break your VLC player unless you modify your shortcuts, etc. to also set VLC_PLUGIN_PATH.
The third option, and what I ended up doing, was to custom build my own static LibVLC binary with a few edits to the source code so that it will not load the qt4 interface plugin installed with VLC. You can follow these steps to do this.
1) Download VLC Source Code for your platforms distribution.
http://download.videolan.org/pub/videolan/vlc/
Make sure you download the version matching your distribution. For
example, match the VLC version to what is installed with Ubuntu.
2) Extract source code to folder
3) Install dependencies for the OS distribution
sudo apt-get build-dep vlc
4) Modify src/modules/bank.c
Edit the module_InitDynamic function
Add the following code at the top of the function:
// HACK TO DISABLE QT4 PLUGIN
if(strstr(path, "qt4") != NULL)
return NULL;
// END HACK
3) From terminal
./bootstrap
./configure --disable-qt --disable-skins2 --enable-xcb --prefix=/home/$USER/vlc-custom_build_output_folder_name
./make
./make install
4) Copy/Save the resulting files in the install folder.
Then just link against this library instead.

Issue with running open CV face detection on Mac mountain lion

I ran into an issue getting the standard open CV face detection (facedetect) working. The web cam light comes on but noting happens, the program is launched with a tiny sized window like this:
I am working from an excellent blog post and sample code. Here I what I have done:
Install OpenCV & get OpenCV source
brew tap homebrew/science
brew install --with-tbb opencv
wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.6/opencv-2.4.6.tar.gz
tar xvzf opencv-2.4.6.tar.gz
Run the facedetect sample with the standard classifier.
cd ~/opencv-2.4.6/samples/c
chmod +x build_all.sh
./build_all.sh
./facedetect --cascade="../../data/haarcascades/haarcascade_frontalface_alt.xml"
I can modify the C++ sample code and recompile and run, but I have no idea what the issue is.
Does anyone have a suggestion?
Update The issue is the image from cvQueryFrame is empty:
IplImage* iplImg = cvQueryFrame( capture );
frame = iplImg;
if( frame.empty() )
{
cout << "FRAME EMPTY\n"; // This is getting logged
break;
}
Update: It works ok when the source is a static image, so the issue is something related to the webcam source.
You can try to localise the problem, did you try to capture an image from the and web cam show it, without running any other operation?
It seems there is a problem capturing image from the web cam via OpenCV, this kind of problems may happen due to hardware, for instance on my friends macbook pro captured image was 320x240 and on mine it was 640x480. My friend just changed a simple configuration from settings of the camera and his problem was solved. Your problem might be something like this.
Or you can try to run face detector just with some images, you need to change the code such that it loads an image from your disk and try to detect face on them. If it doesn't work that way either we can say that your problem is not camera, there is a bigger issue, or if it works we can surely say that the problem is web cam.
EDIT
If you are using IplImage type be sure to get couple more images from the camera, sometimes first image is empty.
This was due to a bug in OpenCV - its been fixed ( bug report here http://code.opencv.org/issues/3156) , but the version in homebrew/science is from before the fix.
You can get install a newer version by editing the brew formula for opencv ( based on this pull request https://github.com/Homebrew/homebrew-science/pull/540 )
edit /usr/local/Library/Formula/opencv.rb , and replace these lines:
url 'http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.6.1/opencv-2.4.6.1.tar.gz'
sha1 'e015bd67218844b38daf3cea8aab505b592a66c0'
with these ones
url 'https://github.com/Itseez/opencv/archive/2.4.7.1.tar.gz'
sha1 'b6b0dd72356822a482ca3a27a7a88145aca6f34c'
Then do
brew remove opencv
brew install opencv
Works on Mavericks (for me at least), should work on Mountain Lion
UPDATE: the version of OpenCV in homebrew/science has now been updated, so this answer is now out of date!
brew upgrade opencv
will make homebrew get the latest version, with fixed webcam capture.

Possible bug in OpenCV2.4 capturing frames from video

Could it be that there is a bug in OpenCV2.4 highgui for capturing frames from video in windows?
I installed both the precompiled libraries, the ones compiled by me, I can compile everything perfectly and I can run my programs if
they are image based. The problem is only for videos. OpenCV crashes in this function always:
virtual IplImage* retrieveFrame(int)
{
unsigned char* data = 0;
int step=0, width=0, height=0, cn=0;
if(!ffmpegCapture ||
!icvRetrieveFrame_FFMPEG_p(ffmpegCapture,&data,&step,&width,&height,&cn)) <-------CRASHES HERE
return 0;
cvInitImageHeader(&frame, cvSize(width, height), 8, cn);
cvSetData(&frame, data, step);
return &frame;
}
This is inside the class cap_ffmpeg.cpp and is called by VideoCapture.
I tried versions 2.4.2 and 2.4.9. My programes were working finde with opencv2
More information
Windows 7
Build the projects with cmake (important as it could be that cmake is not building/finding the right codecs)
VisualStudio 9 2008
OpenCV 2.4.2
EDIT
It looks like it is actually a bug, so, how can I solve this problem and change my code to be able to read avi files?
As a temporary solution I decided to re-encode the videos so OpenCV doesn't use the ffmpeg. I used VirtualDub with the microsoft video 1 compression, which uses "msvidc32.dll" driver.
It works with all my videos so it is enough by now as I can keep working with OpenCV in windows.
I had similar problem. I downloaded VirtualDub, but it did not open one of my avi video because of its FMP4 encode. So in the end, the solution that solved the problem was to install ffdshow, a decoder for windows. See this link: http://www.moviecodec.com/video-codecs/fmp4-codec-with-virtualdub-45814/
Then I do not need to use VirtualDub anymore to re-encode my video anymore!
if your using x64. Please use opencv_ffmpeg245_64.dll this solves may solve the problem. Or rename opencv_ffmpeg245_64.dll to opencv_ffmpeg245.dll may solve the problem.