opencv 3.2 C++ VS2015 imshow access violation executing - c++

I am trying to use the sample code to get image via the embedded camera of my laptop. I can get the image. But the imshow statement throw Access violation executing
Anyone can help me to figure out the reason?
Deeply appreciate!
Environment: Visual Studio 2015, Opencv 3.2
c++ code:
using namespace cv;
using namespace std;
int main(int, char**)
{
Mat frame;
VideoCapture cap;
cap.open(0);
if (!cap.isOpened())
{
cerr << "ERROR, unable to open the camera\n";
return -1;
}
std::cout << "start grapping" << endl;
for (;;)
{
cap.read(frame);
if (frame.empty())
{
cerr << "ERROR, blank frame grabbed \n";
break;
}
// I added below statement to save the frame.
// check the frame from the camera.
imwrite("D:\\CapturedImage.jpg", frame);
// Below line throw Access violation executing location error
imshow("Live", frame);
}
}

Related

OpenCV C++ VideoCapture on MacOS Monterey Not working

Hi I'm using M1 Macbook Pro 2021 with Monterey OS.
I've been trying to use my mac's internal webcam with OpenCV C++ VideoCapture class on Visual Studio Code, but i keep getting this weird errors. I've given both terminal and iTerm access to the Camera on my Mac's Preferences, but it still keeps giving me this error.
This is my Code,
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
void camera_in()
{
VideoCapture cap;
cap.open(2, CAP_AVFOUNDATION);
if (!cap.isOpened())
{
cerr << "Camera open failed!" << endl;
return;
}
cout << "Frame width: " << cvRound(cap.get(CAP_PROP_FRAME_WIDTH)) << endl;
cout << "Frame height: " << cvRound(cap.get(CAP_PROP_FRAME_HEIGHT)) << endl;
Mat frame, inversed;
while (true)
{
cap >> frame;
if (frame.empty())
break;
inversed = ~frame;
imshow("frame", frame);
imshow("inversed", inversed);
if (waitKey(10) == 27)
break;
}
destroyAllWindows();
}
int main()
{
camera_in();
}
And this is the error i get from executing it.
2022-08-05 18:15:01.284398+0900 video[7664:45504] [plugin] AddInstanceForFactory: No factory registered for id <CFUUID 0x10b54c320> F8BB1C28-BAE8-11D6-9C31-00039315CD46
2022-08-05 18:15:01.291647+0900 video[7664:45504] HALC_ProxyObjectMap::_CopyObjectByObjectID: failed to create the local object
2022-08-05 18:15:01.291664+0900 video[7664:45504] HALC_ShellDevice::RebuildControlList: couldn't find the control object
2022-08-05 18:15:01.316885+0900 video[7664:45504] [plugin] AddInstanceForFactory: No factory registered for id <CFUUID 0x10c50bb40> 30010C1C-93BF-11D8-8B5B-000A95AF9C6A
I ran this code on my macbook pro m1 14" and it was working, I had to change:
cap.open(2, CAP_AVFOUNDATION);
to:
cap.open(0, CAP_AVFOUNDATION);
for it to work though (0 is the index of the built in webcam).

How to a load video file by using OpenCV with C++ on Raspberry Pi?

I run the code (as below) that can play video stream on windows platform(Visual Studio 13 C++).
The code compile successfully on Raspberry Pi but it can not show video.
The video file is in the same document. I had try VideoCapture cap('/home/pi/1.mov') that also doesn't work.
How should I modify the code?
PS. If VideoCapture cap("1.mov") change to VideoCapture cap(0) which open USB camera successfully.
Thanks!
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int, char** argv)
{
Mat frame;
VideoCapture cap("1.mov");
if (!cap.isOpened())
cout << "Error when reading stream";
namedWindow("Frame", 1);
for (;;)
{
cap >> frame;
if (frame.empty())
break;
imshow("Frame", frame);
if (waitKey(10) >= 0)
{
break;
}
}
return(0);
}
Result: Error when reading stream
You need to install proper codecs. See a similar question (and code) here: http://answers.opencv.org/question/9692/mp4-reading/

opencv_ffmpeg**.dll error in OpenCV 2.4.11 for IP Camera Access?

I try to get the live stream from my IP camera by using OpenCv 2.4.11 on Windows. However, it raises error and when I debug the an access occured as
Unhandled exception at 0x0000000066E5377F (opencv_ffmpeg2411_64.dll) in opencvAxisExp.exe: 0xC0000005: Access violation reading location 0x0000000000000000.
I use the code below to do so;
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <iostream>
int main(int, char**) {
cv::VideoCapture vcap;
cv::Mat image;
// This works on a D-Link CDS-932L
//const std::string videoStreamAddress = "http://user:pass#ip_address:port/axis-cgi/jpg/image.cgi?camera=1&resolution=320x240&compression=25 ";
const std::string videoStreamAddress = "http:/user:pass#ip_address:port/axis-cgi/mjpg/video.cgi";
//open the video stream and make sure it's opened
if (!vcap.open(videoStreamAddress)) {
std::cout << "Error opening video stream or file" << std::endl;
return -1;
}
for (;;) {
if (!vcap.read(image)) {
std::cout << "No frame" << std::endl;
cv::waitKey();
}
cv::imshow("Output Window", image);
if (cv::waitKey(1) >= 0) break;
}
}
Is this a dll error or what is missing here? Do you have any suggestion? Thanks for any comment.
I'd guess you might be missing the mjpg suffix. See this answer.

Getting opencv error in c++

I'm try to get the error of opencv! say I have this program:
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
int main (){
cv::Mat frame;
cv::VideoCapture cap(1); // I don't have a second videoinput device!
int key = 0;
while(key !=27){
cap >> frame;
cv::imshow("frame",frame);
key = cv::waitKey(10);
}
cap.release();
return 0;
}
when I run this program I get in the console this message :
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in unknown functi
on, file ..\..\..\opencv\modules\highgui\src\window.cpp, line 261
My question is how can I get this message and save it in a string for every error that I get!
and if it'S possible escaping the program crash!
thanks in advance!
It uses C++ exceptions. See here in the doc for more.
try
{
... // call OpenCV
}
catch( cv::Exception& e )
{
const char* err_msg = e.what();
std::cout << "exception caught: " << err_msg << std::endl;
}
A CV_Assert in the OpenCV code is a macro which calls the OpenCV function error. That function can be seen here. It will always print the error text on stderr unless you don't have the customErrorCallback set. You do that via cvRedirectError, see here.
You have to check whether OpenCV function calls in your code is successfully executed or not. Then you can understand the exact problem. Here is the modified code.
int main (){
cv::Mat frame;
cv::VideoCapture cap(1); // I don't have a second videoinput device!
if ( !cap.isOpened() ) // if not success, exit program
{
cout << "Cannot open the video cam" << endl;
return -1;
}
int key = 0;
while(key !=27){
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read the frame from video cam" << endl;
break;
}
cv::imshow("frame",frame);
key = cv::waitKey(10);
}
cap.release();
return 0;
}

OpenCV 2.4.6.1: Error while grabbing frame from camera [duplicate]

This question already has answers here:
Getting opencv error in c++
(2 answers)
Closed 6 years ago.
I am using XCode (OS X Mountain Lion) with OpenCV. OpenCV is installed via homebrew (version 2.4.6.1)
My program should just access the camera.
Here is my code sofar:
using namespace cv;
int main(int argc, const char * argv[])
{
Mat frame;
VideoCapture cap(CV_CAP_ANY);
if (!cap.isOpened())
{
std::cerr << "Webcam error. Was not able to open webcam!\n";
exit(1);
}
namedWindow("webcam", CV_WINDOW_AUTOSIZE);
while (cap.isOpened())
{
cap >> frame;
if (frame.empty())
{
std::cerr << "Frame data error.\n";
}
imshow("webcam", frame);
if(waitKey(50) >= 0)
{
cap.release();
std::cout << "Webcam closed.\n";
}
}
std::cout << "The Program has finished.";
return 0;
}
But I am getting the output:
Frame data error.
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /tmp/default-mebu/opencv-2.4.6.1/modules/highgui/src/window.cpp, line 261
libc++abi.dylib: terminate called throwing an exception
(lldb)
I think that my program is not accessing the camera properly. It is somehow not able to get the data.
I know there have been some problems with Linux but I thought they have been fixed and I am not sure how they influenced OS X.
Does anybody know a solution to my problem?
Edit:
So I found a solution. I added a try {} catch {} for the imshow. Now my program does not exit when it hits the imshow. Instead it just throughs an error and keeps the while loop running. It misses a few frames but still gets enough to maintain a good videostream.
try
{
imshow("webcam", frame);
}
catch (Exception& e)
{
const char* err_msg = e.what();
std::cout << "exception caught: imshow:\n" << err_msg << std::endl;
}
The error thrown is still the same one:
frame data error.
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /tmp/default-mebu/opencv-2.4.6.1/modules/highgui/src/window.cpp, line 261
exception caught: imshow:
/tmp/default-mebu/opencv-2.4.6.1/modules/highgui/src/window.cpp:261: error: (-215) size.width>0 && size.height>0 in function imshow
VideoCapture cap(CV_CAP_ANY);
Sleep(1000); // Wait for response of camera, don't forget to #include <windows.h>
I had similar problem .You can add similar this code, maybe solve it. Because capture size cause this problem
VideoCapture cap;
cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
then
cap.read(image);
So I found a workaround. I added a try {} catch {} for the imshow. Now my program does not exit when it hits the imshow. Instead it just throughs an error and keeps the while loop running. It misses a few frames but still gets enough to maintain a good videostream.
try
{
imshow("webcam", frame);
}
catch (Exception& e)
{
const char* err_msg = e.what();
std::cout << "exception caught: imshow:\n" << err_msg << std::endl;
}