I'm new to using OpenCV. I'm trying to convert a double type matrix to an image, like using imagesc function in MATLAB. I know that we can use imshow to do that. But it shows 'Access Violation' error when I tried to use a matrix of size 600 * 900. I'm using Visual Studio 2010 on Windows 8 with OpenCV 2.4.5 .
#include <stdio.h>
#include "device_launch_parameters.h"
#include "cuda_runtime_api.h"
#include <stdlib.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/gpu/gpu.hpp"
#include <iostream>
int main()
{
int iter1;
float *A;
A=(float*)malloc(sizeof(float) * 50 * 50);
for(iter1=0; iter1<(50 * 50); iter1++)
{
A[iter1] = iter1;
}
cv::Mat test(50, 50, CV_32F, &A);
cv::imshow("test", test);
cv::waitKey();
cudaDeviceReset();
return 0;
}
The above is the code I used for producing an image out of 50 * 50 matrix which worked fine. Kindly let me know if you find the problem with bigger matrix or give me some sample code if you have any.
Related
I am currently doing some C++ image processing with openCV. I developed the application on a Mac with Xcode 6.3.2 and it works perfectly in both debug and release. In order to have a Windows executable program, I am now working on Windows with Visual Studio Express 2013. The program is running well on debug mode but crashes in release mode on this part of the code :
#include "stdafx.h"
#include "math.h"
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/core/core_c.h"
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/photo/photo.hpp"
#include "opencv2/features2d/features2d.hpp"
using namespace cv;
using namespace std;
int main(int argc, const char** argv)
{
vector<Mat> stacked_images;
Mat medianr_eq, mediang_eq, medianb_eq, objrgb;
medianr_eq = imread("C:\\Path\\medianr_eq.png", CV_LOAD_IMAGE_GRAYSCALE);
mediang_eq = imread("C:\\Path\\mediang_eq.png", CV_LOAD_IMAGE_GRAYSCALE);
medianb_eq = imread("C:\\Path\\medianb_eq.png", CV_LOAD_IMAGE_GRAYSCALE);
objrgb = Mat(medianr_eq.size(), CV_16UC3);
stacked_images.clear();
stacked_images.push_back(medianb_eq); /*B*/
stacked_images.push_back(mediang_eq); /*G*/
stacked_images.push_back(medianr_eq); /*R*/
merge(stacked_images, objrgb);
}
The error I get is :
OpenCV Error : Assertion failed <mv && n > 0> in cv::merge, file C:\builds\master_PackSlave_Win64-vc12-shared\opencv\modules\core\src\convert.cpp, line 941
I can't see where I could have done something wrong... Indeed, it is pretty basic OpenCV !
The images I used are downloadable with this link : https://transfert.u-psud.fr/gs67
For astronomy lovers it is the Stephan's Quintet, taken with Calar Alto Observatory's 1.23m telescope where I currently am an intern.
Thank you in advance for your help,
Arnaud.
I recently had the exact same error, appearing with a valid openCV code running perfectly fine in debug mode but not in release mode.
Looking into the openCV source code, one can find that the function called has this code (modules/core/src/convert.cpp, line 341):
void cv::merge(InputArrayOfArrays _mv, OutputArray _dst)
{
CV_OCL_RUN(_mv.isUMatVector() && _dst.isUMat(),
ocl_merge(_mv, _dst))
std::vector<Mat> mv;
_mv.getMatVector(mv);
merge(!mv.empty() ? &mv[0] : 0, mv.size(), _dst);
}
the last line here calls the function 'void cv::merge(const Mat mv, size_t n, OutputArray _dst)*', which has the infamous CV_Assert( mv && n > 0 ); instruction in its first line, causing the crash at runtime.
This error tells us that the vector of Mat is either a null pointer/reference or empty, which it clearly isn't in your code. I strongly suspect that the error is in the getMatVector function call, not copying the contents of _mv into mv. This leaves an empty array that is passed to the merge function, causing the error raised by CV_Assert.
In my case, the fix was to use directly the prototype of the function defined at line 200 in convert.cpp. For you, that would mean (copying only the last few lines of your code):
stacked_images.clear();
stacked_images.push_back(medianb_eq); /*B*/
stacked_images.push_back(mediang_eq); /*G*/
stacked_images.push_back(medianr_eq); /*R*/
merge(&stacked_images[0], stacked_images.size(), objrgb);
This solution worked in my case, and my code is now happily running in debug and release mode !
PS: I know it has been a while, but thought I would post the answer anyway in case somebody running into the same problem would arrive on this SO question.
PS2: Here is a full example code:
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main( int argc, char** argv )
{
namedWindow( "Display window", WINDOW_AUTOSIZE );
Mat result;
Mat R = imread("Lenna.png", CV_LOAD_IMAGE_GRAYSCALE);
Mat G = imread("Lenna.png", CV_LOAD_IMAGE_GRAYSCALE);
Mat B = imread("Lenna.png", CV_LOAD_IMAGE_GRAYSCALE);
// Changing channel values for final result that should look largely blue
R = 0.1*R;
G = 0.1*G;
B = 1.5*B;
std::vector<cv::Mat> array_to_merge ;
array_to_merge.push_back(B);
array_to_merge.push_back(G);
array_to_merge.push_back(R);
// This line triggers a runtime error in Release mode
//cv::merge(array_to_merge,result);
// This line works both in Release and Debug mode
cv::merge(&array_to_merge[0], array_to_merge.size(), result);
cv::imshow( "Display window", result );
cv::waitKey(0);
}
Although you could write it in a lot fewer lines , this code is valid.
I'm trying to test some motion estimation in Visual Studio 2013, using OpenCV v3.0 (which is probably my 1st mistake!). I got an unhandled exception trying to use createOptFlow_DualTVL1() and createOptFlow_Farneback(), and then, for testing, tried cv::accumulate(), which threw the same exception.
It seems that OpenCV can't write to the Mat object that I'm passing these functions. I can't read the actual cvException because I don't have the PDB files, because I didn't compile this version myself. That might be my next stop, but before I do I figured I'd see if anyone's seen this behaviour before.
Here's a minimal working example:
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/video/tracking.hpp>
#include <opencv2/videoio/videoio.hpp>
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <string>
#include <regex>
using namespace std;
using namespace cv;
int main(int argc, const char** argv)
{
VideoCapture captureDevice;
std::string videoname = "example.mp4";
captureDevice.open(videoname);
//setup image files used in the capture process
Mat currFrame, dst;
captureDevice >> currFrame;
accumulate(currFrame, dst, cv::noArray());
imshow("outputCapture", dst);
//pause for 33ms
waitKey(33);
return 0;
}
dst should be of same size as that currFrame and of type CV_32FC3.
So, add this line of code before calling accumulate -
dst.create(currFrame.size(), CV_32FC3);
Since dst is of float type, you will need to convert it to uchar to display it. For that, convert as shown below -
Mat dst_disp;
dst.convertTo(dst_disp, CV_8UC3);
imshow("outputCapture",dst_disp );
Additionally, as you accumulate more frames in dst, you will need to normalize by number of frames(let's say N) cached in dst. Simply, divide dst by the N, then convert the result into CV_8UC3 and display. For example, if you accumulated 1000 frames in dst do as shown below,
// Accumulate 1000 frames
for(int i = 0; i < 1000; i++)
accumulate(currFrame, dst, cv::noArray());
// Normalize
dst = dst/ 1000;
// Display the frame
Mat dst_disp;
dst.convertTo(dst_disp, CV_8UC3);
imshow("outputCapture",dst_disp );
else, you might get an all white image.
UPDATE
From #berak's comment below.
For normalization, simply use
dst.convert(dst_disp, CV_8UC3, 1.0/N);
where N in example above will be 1000.
I am trying to get OpenCV 2.4.5 to recognize a checkerboard pattern from my webcam. I couldn't get that working, so I decided to try to get it working just using a "perfect" image:
but it still won't work--patternFound returns false every time. Does anyone have any idea what I'm doing wrong?
#include <stdio.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main(){
Size patternsize(8,8); //number of centers
Mat frame = imread("perfect.png"); //source image
vector<Point2f> centers; //this will be filled by the detected centers
bool patternfound = findChessboardCorners(frame,patternsize,centers);
cout<<patternfound<<endl;
drawChessboardCorners(frame, patternsize, Mat(centers), patternfound);
cvNamedWindow("window");
while(1){
imshow("window",frame);
cvWaitKey(33);
}
}
Through trial and error, I realized that patternsize should be 7x7 since it is counting internal corners. This parameter has to be exact--8x8 won't work, but neither will anything less than 7x7.
Instead of using
Size patternsize(8,8);
use
Size patternsize(7,7);
Width and height of the chessboard can't be of the same length, i.e. it needs to be assymetric. This might be the source of your problem.
Here is a very good tutorial about camera calibration with OpenCV.
Just below is the code I use for my calibration (tested and fully functional, HOWEVER I call it in some processing thread of my own, you should call it in your processing loop or whatever you are using to catch your frames) :
void MyCalibration::execute(IplImage* in, bool debug)
{
const int CHESSBOARD_WIDTH = 8;
const int CHESSBOARD_HEIGHT = 5;
const int CHESSBOARD_INTERSECTION_COUNT = CHESSBOARD_WIDTH * CHESSBOARD_HEIGHT;
//const bool DO_CALIBRATION = ((BoolProperty*)getProperty("DoCalibration"))->getValue();
if(in->nChannels == 1)
cvCopy(in,gray_image);
else
cvCvtColor(in,gray_image,CV_BGR2GRAY);
int corner_count;
CvPoint2D32f* corners = new CvPoint2D32f[CHESSBOARD_INTERSECTION_COUNT];
int wasChessboardFound = cvFindChessboardCorners(gray_image, cvSize(CHESSBOARD_WIDTH, CHESSBOARD_HEIGHT), corners, &corner_count);
if(wasChessboardFound) {
// Refine the found corners
cvFindCornerSubPix(gray_image, corners, corner_count, cvSize(5, 5), cvSize(-1, -1), cvTermCriteria(CV_TERMCRIT_ITER, 100, 0.1));
// Add the corners to the array of calibration points
calibrationPoints.push_back(corners);
cvDrawChessboardCorners(in, cvSize(CHESSBOARD_WIDTH, CHESSBOARD_HEIGHT), corners, corner_count, wasChessboardFound);
}
}
Just in case you wondered about the class members, here is my class (IplImage was still around at the time I wrote it) :
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv/cv.h>
class MyCalibration
{
private:
std::vector<CvPoint2D32f*> calibrationPoints;
IplImage *gray_image;
public:
MyCalibration(IplImage* in);
void execute(IplImage* in, bool debug=false);
~MyCalibration(void);
};
And finally the constructor :
MyCalibration::MyCalibration(IplImage* in)
{
gray_image = cvCreateImage(cvSize(in->width,in->height),8,1);
}
i'm working on a project of image stitching using OpenCV 2.3.1 on Visual Studio 2012.
For an unknown reason my programme don't work !!
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.h"
#include <stdio.h>
#include <iostream>
using namespace cv;
using namespace std;
Mat getHomography()
{
vector<Point2f> points1;
vector<Point2f> points2;
points1.push_back(Point2f(10, 100));
points1.push_back(Point2f(100, 10));
points1.push_back(Point2f(200, 20));
points1.push_back(Point2f(80, 30));
points2.push_back(Point2f(220, 20));
points2.push_back(Point2f(10, 220));
points2.push_back(Point2f(90, 120));
points2.push_back(Point2f(100, 20));
Mat H = findHomography(Mat(points1), Mat(points2), 8, 3.0);
return H;
}
int main(){
Mat HH = getHomography();
cout<<HH;
system("pause");
return 0;
}
my problem :
any help plzzz
you need at least 4 points in each vec. 2 just aren't enough
I solved my problem by changing from vs2012 to vs 2010 :D
I'm just now starting to learn how to use the openCV libraries. I've downloaded and installed openCV 2.4.0, and have run a few example projects. In this block of code, I'm trying to get the output from goodFeaturesToTrack and plot the points on an image. The code compiles, but every time I run it, it crashes, and I get the following error:
Windows has triggered a breakpoint in Corner.exe.
This may be due to a corruption of the heap, which indicates a bug in Corner.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while Corner.exe has focus.
The output window may have more diagnostic information.
The output window does not have more diagnostic information. I've traced the error to the goodFeaturesToTrack function. Here's the offending code:
// Corner.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <opencv.hpp>
#include <opencv_modules.hpp>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace cv; //If you don't have this, you won't be able to create a mat...
using namespace std;
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include <math.h>
//Whole bunch of #defines to make editing the code a lot easier
#define MAX_FEATURES 5
#define FILENAME "C:/Users/Mitchell/Desktop/lol.jpg"
int main(void)
{
namedWindow("Out", CV_WINDOW_AUTOSIZE);
namedWindow("In", CV_WINDOW_AUTOSIZE);
Mat Img;
Img = cvLoadImage(FILENAME, CV_LOAD_IMAGE_GRAYSCALE);
if(!Img.data)
{
fprintf(stderr, "ERROR: Couldn't open picture.");
waitKey();
return -1;
}
else
{
imshow("In", Img);
waitKey();
}
std::vector<cv::Point2f> Img_features;
int number_of_features = MAX_FEATURES;
Mat Out = Mat::zeros(Img.cols, Img.rows, CV_32F);
goodFeaturesToTrack(Img, Img_features, MAX_FEATURES, .01, .1, noArray(), 3, false);
fprintf(stdout, "Got here...");
/*for (int i = 0; i < MAX_FEATURES; i++)
{
Point2f p = Img_features[i];
ellipse(Img, p, Size(1,1), 0, 0, 360, Scalar(255,0,0));
}*/
imshow("Out", Out);
waitKey(0);
return 0;
}
Is this a bug in the library, or am I doing something dumb?
May be Img_features vector should have MAX_FEATURES items before calling goodFeatures? i.e. try Img_features.resize(MAX_FEATURES) before goodFeatures call.