Hi there,
I'm quite new to OpenCV and although I'm having fun learning it, I also have a problem which only occurs occasionally.
I've opened up a USB microscope and I want to be able to capture still images. I created some code and in my initial tests everything seem to be working, I then put it into it's own method and that is where the problems started. Sometimes when I press the space bar to capture an image, the image is sometimes set at an extremely low resolution and then occasionally throw an error, so I took the code out of the function and place it back into my main code.
This has sorted out the resolution problem but I still get the error and I can't figure out why. It seems to be something to do with when I'm writing the image to file (imwrite)
I've had a look on this forum and google and it might be something to do with the fact I'm using a 64-Bit system but I could be making some kind of amateurish mistake.
I've posted the code below....
int main(int argc, char** argv){
VideoCapture Microscope(0);
if (!Microscope.isOpened())
{
cout << "Microscope wouldn't open" << endl;
}
double dWidth = Microscope.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
double dHeight = Microscope.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
namedWindow("Microscope", CV_WINDOW_AUTOSIZE);
namedWindow("Tools", CV_WINDOW_AUTOSIZE);
setMouseCallback("Microscope", MousePositionFunc, NULL);
cvCreateTrackbar("Brightness", "Tools", &g_slider_position, 100, onTrackbarSlide);
while (1)
{
Mat frame; //Matrix Storage for frames
bool bSuccess = Microscope.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "ERROR: Cannot read a frame from video file" << endl;
break;
}
imshow("Microscope", frame);
Mat output = frame + Scalar(g_slider_position, g_slider_position, g_slider_position);
namedWindow("Output", CV_WINDOW_AUTOSIZE);
imshow("Output", output);
int mKey;
mKey = waitKey(33);
if (mKey == 27)
{
cout << "User Pressed Escape" << endl;
break;
}
else if (mKey == 32)
{
Mat img(dHeight, dWidth, CV_16UC3, Scalar(0, 50000, 50000));
vector<int> compression_params; //vector that stores the compression parameters of the image
compression_params.push_back(CV_IMWRITE_JPEG_QUALITY); //specify the compression technique
compression_params.push_back(98); //specify the compression quality
img = frame;
string String = ("Screengrab" + static_cast<ostringstream*>(&(ostringstream() << counterpress))->str() + ".jpg"); // create string and cast integer to assign a value
imwrite(String, img, compression_params); //write the image to file
cout << "Image Saved " << counterpress << " times" << endl;
counterpress++;
}
else
{
cout << mKey << " is pressed" << endl;
}
}
destroyAllWindows();
return 0;}
I would appreciate the help of anyone who can figure out what I am doing wrong, if anything??
Thanks. :)