Ive been stuck with this problem for a few days and I thought I'd ask for help.
I have written this test code to display my issue
this is my take picture function
cv::Mat takePicture(){
cv::Mat pic;
VideoCapture cam(0);
if(!cam.isOpened()){
std::cout << "Failed to make connection to Cam" << std::endl;
exit(1);
}
cam.set(CV_CAP_PROP_FRAME_WIDTH, 320);
cam.set(CV_CAP_PROP_FRAME_HEIGHT, 240);
cam >> pic;
cvWaitKey(100);
return pic.clone();
}
this is a loop in my main function that will cause a crash
if(test_mode == true){
while(true){
Mat testImg;
char fileName[50];
sprintf(fileName, "testFile%d", threadNo);
testImg = takePicture();
cout << "current pic is " << threadNo << endl;
threadNo++;
testImg.release();
}
}
The program will run fine until image 27 it will then go slower and by image 30 I will get a variety of errors they are
mmap: Cannot allocate memory
munmap: Invalid argument
Unable to stop the stream.: Bad file descriptor
libv4l1: error allocating v4l1 buffers: Cannot allocate memory
HIGHGUI ERROR: V4L: Mapping Memmory from video source error: Invalid argument
HIGHGUI ERROR: V4L: Initial Capture Error: Unable to load initial memory buffers
*** glibc detected *** ./Cam: free(): invalid pointer: 0x0076ab88 ***
Aborted
I keep thinking this must be caused by a leak. Thanks in advance for the help.
Related
I build OpenCV 3.4 with Gstreamer MSVC 1.16.1, using Cmake and Visual Studio 10.
I have include bin directory to the system path variable, added all additional include and library to Visual Studio.
Now when I am trying to read an Image to test if OpenCV is correctly installed it throws an error as:
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file .../opencv/modules/highgui/src/window.cpp
The code was:
Mat image1;
image1 = imread("D:\\Capture2.JPG");
if(! image1.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
}
imshow("Image",image1);
cvWaitKey(0);
return 0;
Now I tried to play a video using demo code from openCV site:
// opencv_3.4_test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int _tmain(int argc, _TCHAR* argv[])
{
// Create a VideoCapture object and open the input file
// If the input is the web camera, pass 0 instead of the video file name
VideoCapture cap("Wildlife.mp4");
// Check if camera opened successfully
if(!cap.isOpened()){
cout << "Error opening video stream or file" << endl;
return -1;
}
while(1){
Mat frame;
// Capture frame-by-frame
cap >> frame;
// If the frame is empty, break immediately
if (frame.empty())
break;
// Display the resulting frame
imshow( "Frame", frame );
// Press ESC on keyboard to exit
char c=(char)waitKey(25);
if(c==27)
break;
}
// When everything done, release the video capture object
cap.release();
// Closes all the frames
destroyAllWindows();
return 0;
}
The program is building correctly but I am getting following error while running it:
warning: Error opening file (/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:808)
warning: ?Wildlife.mp4 (/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:809)
GStreamer: error opening bin syntax error
Where can be the error as they both are simplest OpenCV program.
So the error was, there was an invisible character ('\u202A') present just after " of the filename. Once I deleted it, everything runs fine.
I found this from the warning C4566: character represented by universal-character-name '\u202A' cannot be represented in the current code page (1252)
The following the the code to load the image tree
#include<opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat testobject = imread("tree",IMREAD_UNCHANGED);
if (testobject.empty())
std::cout << "failed to open img.jpg"
<<std::endl;
else
std::cout << "img.jpg loaded OK" << std::endl;
imshow("color", testobject);
waitKey();
return(0);
}
but am getting the following output
failed to open img.jpg
and error as
OpenCV(3.4.3) Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow, file C:\build\3_4_winpack-build-win64-vc14\opencv\modules\highgui\src\window.cpp, line 356
Am using visual studio 2017
can anybody please help me out this my first open cv program
In code you should have
Mat testobject = imread("tree.jpg",IMREAD_UNCHANGED);
Image should be in file where You have cpp files, not where sln file is.
I'm trying connect to my IP Camera as follows:
const string sourceIPcam = "http://192.168.25.112:202/videostream.cgi? user=admin&pwd=admin&resolution=32";
Mat frame;
VideoCapture cap(sourceIPcam);
namedWindow("IP Video Camera", CV_WINDOW_AUTOSIZE);
if (!cap.isOpened())
{
cout << "Could not open the camera " << sourceIPcam << endl;
return -1;
}
When running the above code I get the following error:
[mp3 # 023f3980] Header missing
[mp3 # 023f3980] Header missing
[mp3 # 023f3980] Header missing
[mp3 # 023f3980] Header missing
warning: Could not find codec parameters (../../modules/highgui/src/cap_ffmpeg_i
mpl.hpp:556)
init done
opengl support available
Any help would be appreciated.
I have application made with QTCreator. It uses OpenCV 2.4.11. I have the same application on Visual. Code is the same in both.
OpenCV for Microsoft Visual 2013 was made using this link:
https://www.youtube.com/watch?v=e_TQ9c3n_d8
It is for 2.4.10, but it's the same for 2.4.11.
And I configured QtCreator with this tutorial:
How to link opencv in QtCreator and use Qt library
Now the code:
#include <opencv2\highgui\highgui.hpp>
#include <iostream>
#include <fstream>
#include <opencv2\highgui\highgui.hpp>
#include "opencv2\stitching\stitcher.hpp"
using namespace cv;
using namespace std;
void ReadPhotos();
double begin_t, end_t;
int photo_number = 0;
Mat photos[100];
Mat image;
vector< Mat > ImagesVector;
vector<Mat> roisVector;
int main()
{
cout << "Starting program!" << endl;
ReadPhotos();
Size size(1050, 600);
for (int i = 0; i < photo_number; i++){
//resize(photos[i], photos[i], size);
ImagesVector.push_back(photos[i]);
}
Stitcher stitcher = Stitcher::createDefault(true);
stitcher.setWarper(new SphericalWarper());
stitcher.setFeaturesFinder(new detail::SurfFeaturesFinder(300, 3, 4, 3, 4));
stitcher.setRegistrationResol(0.9);
stitcher.setSeamEstimationResol(0.9);
stitcher.setCompositingResol(1);
stitcher.setPanoConfidenceThresh(1);
stitcher.setWaveCorrection(true);
stitcher.setWaveCorrectKind(detail::WAVE_CORRECT_HORIZ);
stitcher.setFeaturesMatcher(new detail::BestOf2NearestMatcher(false, 0.3));
stitcher.setBundleAdjuster(new detail::BundleAdjusterRay());
Stitcher::Status status = Stitcher::ERR_NEED_MORE_IMGS;
try{
status = stitcher.stitch(ImagesVector, image);
}
catch (cv::Exception e){}
imwrite("panorama.jpg", image);
waitKey(0);
return 0;
}
void ReadPhotos(){
string sourceIN;
string sourcePhoto;
sourceIN = "paths1.txt";
ifstream FileIN(sourceIN);
if (FileIN.is_open())
{
while (getline(FileIN, sourcePhoto)){
photos[photo_number] = imread(sourcePhoto, 1);
photo_number++;
}
}
else{
cout << "Can't find file" << endl;
}
cout << "Number of photos: " << photo_number << endl;
}
Read photos function takes path to images from txt file and load photos.
In visual studio it's working good, I can stitch 3500x2000 resulution images and it gives nice output panorama. In QtCreator I try to stitch the same images and it gives following errors:
OpenCV Error: Insufficient memory (Failed to allocate 290519044 bytes) in OutOfMemoryError, file C:\OpenCV2411\opencv\sources\modules\core\src\alloc.cpp, line 52
terminate called after throwing an instance of 'cv::Exception'
what(): C:\OpenCV2411\opencv\sources\modules\core\src\alloc.cpp:52: error: (-4) Failed to allocate 290519044 bytes in function OutOfMemoryError
Bad alloc - sorry, I can't make this error again so can't copy exactly.
Anyone has any idea why the same code is not working on QTCreator and is working in Visual Studio 2013? Library is the same. One thing I can think of is that I build libraries for QTCreator myself with Cmake. Maybe it has something with this.
I'am actually trying to extract images from a gifFile using the giflib with the following code.
t_gif initGif(const char *filename){
t_gif gif;
int *error;
GifFileType *GifFile = DGifOpenFileName(filename, error);
assert(error != NULL);
int ret = DGifSlurp(GifFile);
assert(ret == GIF_OK);
gif.h = (int)GifFile->SHeight;
gif.w = GifFile->SWidth;
gif.nbFrames = GifFile->ImageCount;
gif.colorSize = GifFile->SColorResolution;
GifImageDesc Image = GifFile->Image;
SavedImage *img = &GifFile->SavedImages[0];
cout << "width: " << gif.w << endl;
cout << "height: " << gif.h << endl;
cout << "Image Count: " << gif.nbFrames << endl;
cout << "SColor Resolution: " << gif.colorSize << endl;
Mat color = Mat(Size(gif.w, gif.h), CV_8UC1, img->RasterBits);
imwrite("./test.png", color);
return gif;
}
But this causes a segfault. I am using opencv in v2.4.5 and giflib in v5.0.4.
I think this is not caused by Opencv because with giflib in v4 I had no problem here.
MoreOver the following test also causes segfault.
printf("%u\n", (unsigned int)img->RasterBits[0]);
Gdb output:
(gdb) run bsd.gif
Starting program: /home/matt/Code/perso/utils/gif/a.out bsd.gif
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Gif Analyser tool
started by MG in may 2013
compiled with giflib: v5.0.4
width: 1000
height: 907
Image Count: 0
SColor Resolution: 8
Program received signal SIGSEGV, Segmentation fault.
0x0000000000401af3 in initGif (filename=0x7fffffffe2be "bsd.gif") at main.cpp:40
40 Mat color = Mat(Size(gif.w, gif.h), CV_8UC1, img->RasterBits);
(gdb)
I also don't find how to get color back using the colorGlobal table.
Can someone Help me ?
Thanks
I just get back to version 4.1.6 and everything is good. The v5 is probably bugged.
To get the color information , you can use the rasterbits of the image as index to the colormap hence getting the RGB information for the corresponding pixel.Then you can simply use CV_8UC3 data type for storing the gif frame as a colored png .