Opencv cvThreshold bug - c++

Hello I don t know if I am doing something wrong or not but when I do the following:
IplImage *testimage;
testimage = cvCreateImage(cvSize(10,10),IPL_DEPTH_8U,1);
cvThreshold(testimage,testimage,127,127,CV_THRESH_TRUNC);
everything works fine then when I try to use unsigned short values:
IplImage *testimage;
testimage = cvCreateImage(cvSize(10,10),IPL_DEPTH_16U,1);
cvThreshold(testimage,testimage,127,127,CV_THRESH_TRUNC);
my program crashes... I use opencv 2.4.2 I think this could maybe be a bug in opencv.
Somehow if I try the following:
IplImage *testimage;
testimage = cvCreateImage(cvSize(10,10),IPL_DEPTH_16S,1);
cvThreshold(testimage,testimage,127,127,CV_THRESH_TRUNC);
it doesn t crash anymore

It is not crashing. It is throwing an exception and since you are not catching it, your program is aborting.
OpenCV Thresholding does not support 16U.
The supported ones are: 8U, 16S and 32F

See the OpenCV documentation for function threshold. The first parameter is 8 or 32 bit image
P.S. In crash message you should see the reason for crashing

Related

why this code is crashing on cvtColor?

I have this code:
void * imageBuffer = reinterpret_cast<void *>(exposureBuffer + imageHeader->imgoffset);
cv::Mat imageRaw(imageHeader->height, imageHeader->width, CV_8UC1, imageBuffer);
cv::Mat imageColour;
cv::cvtColor(imageRaw, imageColour, cv::COLOR_BayerGR2BGR);
when I run this and stops debugger on this line:
cv::Mat imageColour;
I can see that imageRaw has a valid image in it (I can see the image in image view and it is a valid image.)
but then the application crashes on this line:
cv::cvtColor(imageRaw, imageColour, cv::COLOR_BayerGR2BGR);
and it seems that a mat file was created but not enough memory allocated for it.
The error message is:
Unhandled exception at 0x00007FF7503F992B in test_PictureProcessing.exe: 0xC0000005: Access violation reading location 0x0000000000000023.
I am using OpenCv 3. I have similar code which runs successfully on openCV 2.
Edit1
I changed the code to this one to make sure that imagebuffer is a valid buffer and the fact that I am not initializing imageColour is not the problem:
void *imageBuffer = new char[imageHeader->height* imageHeader->width];
cv::Mat imageRaw(imageHeader->height, imageHeader->width, CV_8UC1, imageBuffer);
cv::Mat imageColour = imageRaw.clone();
but I am still getting error on this line:
cv::Mat imageColour = imageRaw.clone();
Edit 2
This is also crashing!
cv::Mat imageRaw(imageHeader->height, imageHeader->width, CV_8UC1);
cv::Mat imageColour = imageRaw.clone();
Why this simple code crashing?
I found the problem which is very strange!
I forgot to include opencv.hpp to my source file after adding it, it worked perfectly.
It is strange as I did not get any compile error, but I got run time error.
If you see your openCV behave strangely, make sure that you included opencv.hpp to your source code. it may help you to solve your problem! Not all problems are coming from missing this header as CroCo mentioned.

OpenCV imwrite params read access violation

A very simple question...why am I getting a read access violation error with this code?
cv::Mat laserSpeckle = Mat::zeros(100,100,CV_8UC1);
imwrite( "C://testimage.jpg", laserSpeckle );
When i attach a debugger and look into it further, it throws the exception at this snippet in grfmt.cpp.
if( params[i] == CV_IMWRITE_JPEG_QUALITY )
{
quality = params[i+1];
quality = MIN(MAX(quality, 0), 100);
}
It occurs with .png and .tiff too. Im an OpenCV newbie, my apologies if this is something really simple. I am using Qt for what its worth.
Do you build OpenCV yourself? If yes, make sure that the option WITH_JPEG is enabled when you configure your build files:
cmake ... -DWITH_JPEG=ON ...
If you want to save image with alpha channel you should use png format. It is described here
It should work with bmp format:
cv::Mat laserSpeckle = cv::Mat::zeros(100,100,CV_8UC1);
cv::imwrite( "C://testimage.bmp", laserSpeckle );
Your code also works on my computer. However, it seems that on some systems it works only for bmp images. I saw similar issues reported here and here.
The problem is with the debugger version (x64) if you build the code using the release version (x64) it works fine for me.
In my case c++ Code Generation settings were wrong
Should have been Multithreaded DEBUG dll MD

Opencv Mosaic from video

I need to do the same thing of this video https://youtu.be/59RJeLlDAxQ but in Opencv. For now I'm doing this thing http://ramsrigoutham.com/2012/11/22/panorama-image-stitching-in-opencv/ with a little modify in the final image merging, but it doesn't work very well. How can I proceed?
EDIT
For testing I'm using the video lab from this page http://www.cs.ucsb.edu/~holl/CS290I/Assignments/Assignments-3/Assignment3Mosaicing.html
I ran my code on that video and I obtain this:
It's not very accurate but its ok.If I let the program run, at a certain point my stitcher produce this:
.
For the stitching ROIs instead of the ramsrigoutham.com ones I'm using this:
warpPerspective(current_frame, rImg, H, Size(current_frame.cols, current_frame.rows), INTER_NEAREST);
Mat roi1(final_img, Rect(img_loop.cols, img_loop.rows, vImg[1].cols, vImg[1].rows));
Mat roi2(final_img, Rect(img_loop.cols, img_loop.rows, rImg.cols, rImg.rows));
rImg.copyTo(roi2);
vImg[1].copyTo(roi1);
Why not using: http://docs.opencv.org/modules/stitching/doc/high_level.html#stitcher-composepanorama
It's available on 2.4.11 and 3.0.0.
The link you mentioned is access deniedhttp://www.cs.ucsb.edu/~holl/CS290I/Assignments/Assignments-3/Assignment3Mosaicing.html
.
what are 'img_loop' and 'Vimg' and 'rimg' in your code? there is some difference between your code and the code you linked.if it is possible explain a little so I can work on your problem , cause Im doing the same thing in opencv

Heap corruption on Windows but not Linux

Below is some simple OpenCV code to create a frame from a video file and run the SURF feature detector and extractor on the frame. When I run this code on Linux and OSX it runs fine, however on windows I am getting a heap corruption error on the two commented lines.
VideoCapture capture(vidFilename.c_str());
Mat frame;
capture >> frame;
SurfFeatureDetector *detector = new SurfFeatureDetector(minHessian);
vector<KeyPoint> frameKeypoints;
detector->detect(frame, frameKeypoints);
delete(detector); // Heap Corruption Detected
SurfDescriptorExtractor *extractor = new SurfDescriptorExtractor();
Mat frameDescriptors;
extractor->compute(frame, frameKeypoints, frameDescriptors);
delete(extractor); // Heap Corruption Detected
I have no clue what could cause this in the code. I am using VS 2010 to compile the code, is there something in VS that could cause this to happen?
As mentioned above that you do not get any exception related to heap corruption does not mean that it did not happen.There would be problem in your code and not in VS or compiler.My previous post on the similar article would be useful here as well.
https://stackoverflow.com/a/22074401/2724703
Probably you should also try to run some dynamic tool(Valgrind) on your linux. There is high possibility that, you would find the same bug using Valgrind as well.
These dynamic tools would give you the root cause of these problem.

Application crashes on equalizeHist of OpenCV

My MFC app runs various API from OpenCV2. Everything else is working fine. But when my program runs
cv::Mat result;
cv::equalizeHist(m_cvImage,result);
I get following runtime exception.
Unhandled exception at 0x7727fbae in OpenCVTest.exe: Microsoft C++ exception: cv::Exception at memory location 0x0029e944..
"C:\slave\WinInstallerMegaPack\src\opencv\modules\imgproc\src\histogram.cpp:2430: error: (-215) CV_ARE_SIZES_EQ(src, dst) && CV_ARE_TYPES_EQ(src, dst) && CV_MAT_TYPE(src->type) == CV_8UC1"
According to debugger, the exception was thrown in the middle of processing (about 40%) the image in equalizeHist. Is there anything I need to do? FYI: I am using binary OpenCV provided by its web site.
UPDATE:
I've resolved this issue by converting images to gray-level before equalizing it. I just didn't know
the function only works with gray-level image
images that look like gray-level can be non-gray.
I imagine the problem you are encountering is that m_cvImage is a 3-channel image. So, you need to convert it to a grayscale image before you can call equalizeHist.
cvtColor(m_cvImage, m_cvImage, CV_BGR2GRAY);
cv::Mat result;
cv::equalizeHist(m_cvImage, result);
Also, have a look at the EqualizeHist_Demo.cpp tutorial sample to see how it is used.