OpenCV matchTemplate throws memory fault but only first time - c++

I am processing a set of >3000 images of same size changing template every 300 images.
code snippet:
cv::Mat inTplate, cFrame, Cresult;
Cresult.create(resultH, resultW, IPL_DEPTH_32F);
cFrame(rect).copyTo(inTplate);
...
// this part executed for every frame
matchTemplate(cFrame, inTplate, Cresult, CV_TM_CCORR_NORMED);
minMaxLoc(Cresult, &minVal, &maxVal, &minLoc, &maxLoc, Mat());
rect = ( 250, 20, 1420, 1040); and resultH = 41; resultW = 501;
the very first time thru the code, the call to matchTemplate throws a memory fault that i believe comes from combase.dll and references an address that is not in the space for any of the three matrices: cFrame, inTplate or Cresult.
Also the sizes for the three matrices are consistent: cFrame 1080 rows X 1920 cols, inTplate 1040 rows X 1420 cols; Cresult is 41 rows X 501 cols. yes the first time inTplate is a region of cFrame; thereafter cFrame is the next image read in.
i verified that the answsers coming back from matchTemplate are correct -- the matching is correct. And the memory fault occurs ONLY on the very first call, not on any of the subsequent frames.
Am I doing something wrong or am i looking at a bug in OpenCV ?
thanks for taking the time.

A sort of answer:
I modified my call to matchTemplate to use a try ... catch block. but it wouldnt catch the exception.
Then I went into Debug | Windows | Exception Settings and turned off the checkbox for cv::Exception.
Now the program runs without stopping with a memory exception. It would seem that i have now enabled OpenCV to catch the exception and deal with it. So the underlying issue still exists but OpenCV is taking care of it. i would still like to understand why the exception is being thrown in the first place, though.

Related

How to fix RuntimeError & Segmentation fault while running demo codes in auto-07p

I was running demo codes in AUTO-07p, which is mainly based on fortran but also following python syntax, but I was getting same error when I run most of demo codes. Since they're not written by me, but written by experts and has been released for a long time, so there must NOT an error. Maybe I don't have to modify the codes.
So when I run codes containing either bifurcation analysis part or plotting, I kept getting an error message RuntimeError: maximum recursion depth exceeded while calling a Python object.
So, I tried to change recursion limit to 10^7, but then I got the error message "Segmentation fault (core dumped)
I'm using ubuntu 16.04 LTS
demo('bvp')
bvp =run('bvp')
branchpoints = bvp("BP")
for solution in branchpoints:
bp = load(solution,ISW=-1,NTST=50)
# Compute forwards
print "Solution label",bp["LAB"],"forwards"
fw = run(bp)
# Compute backwards
print "Solution lavel", bp["LAB"], "backwards"
bw = run(bp,DS='-')
both = fw + bw
merged = merge(both)
bvp = bvp + merged
bvp=relavel(bvp)
save(bvp, 'bvp')
plot(bvp)
wait()
The error message says (excluding seemingly unnecessary lines)
/usr/local/lib/python2.7/dist-packages/numpy/core/numeric.pyc in flatnonzero(a)
924
925
-->926 return np.nonzero(np.ravel(a))[0]
927
928
... last 1 frames repeated, from the frame below ...
/usr/local/lib/python2.7/dist-packages/numpy/core/numeric.pyc in flatnonzero(a)
924
925
-->926 return np.nonzero(np.ravel(a))[0]
927
928
RuntimeError: maximum recursion depth exceeded while calling a Python object
So it seems the code is stuck in an infinite loop, but that flatnonzero function is simply indexing nonzero entries... I'm not sure what should I do

Reaching limitations creating a gallery of plots

In a script I'm using, the code generates a figure where a number of subplots are generated. Usually it creates a rectangular grid of plots, but for it's current use, the horizontal parameter only has 1 value, and the vertical parameter has considerably more values than it has had previously. This is causing my program to crash while running, because (presumably) the vertical dimension is too large. The code that's causing the issue is:
#can't get past the first line here
self.fig1 = plt.figure('Title',figsize=(4.6*numXparams,2.3*numYparams))
self.gs = gridspec.GridSpec(numYparams,numXparams)
self.gs.update(left=0.03, right=0.97, top=0.9, bottom=0.1, wspace=0.5, hspace=0.5)
and then later in a nested for loop running over both params:
ax = plt.subplot(self.gs[par0, par1])
The error I'm getting is:
X Error of failed request: badAlloc (insufficient resources for operation)
Major opcode of failed request: 53 (X_CreatePixmap)
Serial number of failed request: 295
Current serial number in output stream: 296
My vertical parameter currently has 251 values in it, so I can see how 251*2.3 inches could lead to trouble. I added in the 2.3*numYparams because the plots were overlapping, but I don't know how to create the figure any smaller without changing how the plots are arranged in the figure. It is important for these plots to stay in a vertically oriented column.
There are a couple of errors in your code. Fixing them allowed me to generate the figure you are asking for.
# I needed the figsize keyword here
self.fig1 = plt.figure(figsize=(4.6*numXparams,2.3*numYparams))
# You had x and y switched around here
self.gs = gridspec.GridSpec(numYparams,numXparams)
self.gs.update(left=0.03, right=0.97, top=0.9, bottom=0.1, wspace=0.5, hspace=0.5)
# I ran this loop
for i in range(numYparams):
ax = fig1.add_subplot(gs[i, 0]) # note the y coord in the gridspec comes first
ax.text(0.5,0.5,i) # just an identifier
fig1.savefig('column.png',dpi=50) # had to drop the dpi, because you can't have a png that tall!
and this is the top and bottom of the output figure:
Admittedly, there was a lot of space above the first and below the last subplot, but you can fix that by playing with the figure dimensions or gs.update

XCode 6: Unable to create watchpoint

I am trying to watch the following variable
vector<Vec3f> lines[2];
in XCode (where Vec3f is an OpenCV datatype, a vector of 3 floats).
But when I right-click the variable in Variable View and choose Watch "lines", I am being yelled at by XCode:
error: Watchpoint creation failed (addr=0x16fd92d48, size=48, variable
expression='lines'). error: watch size of 48 is not supported
This seems to happen with other variables of type vector<T> as well, but only if it is a local variable. I can watch the vector passed in as a method parameter just fine.
double computeReprojectionError(vector<Point2f>& imgpts1, vector<Point2f>& imgpts2, Mat& inlier_mask, const Mat& F)
{
// ^ I can watch this guy
vector<Vec3f> lines[2]; // <- I cannot watch this guy (size 48)
vector<Point2f> imgpts1_copy(npt), // <- I cannot watch this guy (size 24)
imgpts2_copy(npt);
...
I googled the error with no success. Can somebody shed light on the matter?
Watchpoints are in general fairly limited resources. You didn't say what architecture you were debugging, but x86_64, for instance, has only 4 hardware watchpoint registers, which can at most watch 8 bytes each. So you wouldn't be able to watch a 48 byte region on x86_64 in any case.
But you should be able to watch a 24 byte region by using 3 8-byte watches. I tried this locally, and it looks like there is a bug in the watchpoint setting - it doesn't divvy up a request larger than the native watchpoint size into several smaller watches. So you have to break up the request into 1/2/4/8 byte chunks by hand.
I filed a bug to track this with the Apple bug reporter. But if you want to track it feel free to file one at Apple's http://bugreporter.apple.com site if you want to track the resolution of this, and I'll dup mine to it.

OpenCV 3 SVM train with large Data

I recently switched from OpenCV 2.4.6 to 3.0.
My Code looks like this:
Ptr<ml::SVM> pSVM = ml::SVM::create();
pSVM->->setType(cv::ml::SVM::C_SVC);
pSVM->setKernel(cv::ml::SVM::LINEAR);
pSVM->->setC(1);
cv::Ptr<cv::ml::TrainData> TrainData = cv::ml::TrainData::create(TrainMatrix, cv::ml::ROW_SAMPLE, Labels);
//TrainMatrix is a cv::Mat with 35000 rows and 1900 cols and float values in it. One Feature per row.
//Labels is a std::vector<int> with 35000 Elements with 1 and -1 in it.
pSVM->trainAuto(TrainData, 10, cv::ml::SVM::getDefaultGrid(cv::ml::SVM::C), cv::ml::SVM::getDefaultGrid(cv::ml::SVM::GAMMA), cv::ml::SVM::getDefaultGrid(cv::ml::SVM::P),
cv::ml::SVM::getDefaultGrid(cv::ml::SVM::NU), cv::ml::SVM::getDefaultGrid(cv::ml::SVM::COEF), cv::ml::SVM::getDefaultGrid(cv::ml::SVM::DEGREE), false);
When my program reaches the trainAuto method is crashes and in the error message stands that it cannot allocate 524395968 bytes. This number seems a little bit high. Before the crash the program consumes about 400 MB in Debug Mode.
If I put a smaller matrix (about 500 rows) in the method everything runs normally.
Has anyone same problems and knows a solution to it?

Getting OpenCV Error: Insufficient memory while running OpenCV Sample Program: "stitching_detailed.cpp"

I recently starting working with OpenCV with the intent of stitching large amounts of images together to create massive panoramas. To begin my experimentation, I looked into the sample programs that come with the OpenCV files to get an idea about how to implement the OpenCV libraries. Since I was interested in image stitching, I went straight for the "stitching_detailed.cpp." The code can be found at:
https://code.ros.org/trac/opencv/browser/trunk/opencv/samples/cpp/stitching_detailed.cpp?rev=6856
Now, this program does most of what I need it to do, but I ran into something interesting. I found that for 9 out of 15 of the optional projection warpers, I receive the following error when I try to run the program:
Insufficient memory (Failed to allocate XXXXXXXXXX bytes) in unknown function,
file C:\slave\winInstallerMegaPack\src\opencv\modules\core\src\alloc.cpp,
line 52
where the "X's" mark integer that change between the different types of projection (as though different methods require different amounts of space). The full source code for "alloc.cpp" can be found at the following website:
https://code.ros.org/trac/opencv/browser/trunk/opencv/modules/core/src/alloc.cpp?rev=3060
However, the line of code that emits this error in alloc.cpp is:
static void* OutOfMemoryError(size_t size)
{
--HERE--> CV_Error_(CV_StsNoMem, ("Failed to allocate %lu bytes", (unsigned long)size));
return 0;
}
So, I am simply lost as to the possible reasons that this error may be occurring. I realize that this error would normally occur if the system is out of memory, but I when running this program with my test images I am never using more that ~3.5GB of RAM, according to my Task Manager.
Also, since the program was written as an sample of the OpenCV stitching capabilities BY OpenCV developers I find it hard to believe that there is a drastic memory error present within the source code.
Finally, the program works fine if I use some of the warping methods:
- spherical
- fisheye
- transverseMercator
- compressedPlanePortraitA2B1
- paniniPortraitA2B1
- paniniPortraitA1.5B1)
but as ask the program to use any of the others (through the command line tag
--warp [PROJECTION_NAME]):
- plane
- cylindrical
- stereographic
- compressedPlaneA2B1
- mercator
- compressedPlaneA1.5B1
- compressedPlanePortraitA1.5B1
- paniniA2B1
- paniniA1.5B1
I get the error mentioned above. I get pretty good results from the transverseMercator project warper, but I would like to test the stereographic in particular. Can anyone help me figure this out?
The pictures that I am trying to process are 1360 x 1024 in resolution and my computer has the following stats:
Model: HP Z800 Workstation
Operating System: Windows 7 enterprise 64-bit OPS
Processor: Intel Xeon 2.40GHz (12 cores)
Memory: 14GB RAM
Hard Drive: 1TB Hitachi
Video Card: ATI FirePro V4800
Any help would be greatly appreciated, thanks!
When I run OpenCV's APP traincascade, i get just the same error as you:
Insufficient memory (Failed to allocate XXXXXXXXXX bytes) in unknown function,
file C:\slave\winInstallerMegaPack\src\opencv\modules\core\src\alloc.cpp,
line 52
at the time, only about 70% pecent of my RAM(6G) was occupied. And when runnig trainscascade step by step, I found that the error would be thrown.when it use about more than 1.5G RAM space.
then, I found the are two arguments which can control how many memory should be used:
-precalcValBufSize
-precalcIdxBufSize
so i tried to set these two to 128, it run. I hope my experience can help you.
I thought this problem is nothing about memory leak, it is just relate to how many memory the OS limits a application occupy. I expect someone can check my guess.
I've recently had a similar issue with OpenCV image stitching. I've used create method to create stitcher instance and provided 5 images in vertical order to stitch method, but I've received insufficient memory error.
Panorama was successfully created after setting:
setWaveCorrection(false)
This solution will not be applicable if you need wave correction.
This may be related to the sequence of the stitching, I split a big picture into 3*3, and firstly I stitch them row by row and there is no problem, when I stitch them column by column, there is the problem same as you.