I don’t know, maybe I’m terribly slowing down, but I don’t quite understand how to fix it.

There is some class. Let's say it's called MyClass . There is a cv::VideoCapture camera; declared in MyClass . The cv::VideoCapture is used to capture video from video files and video streams from video cameras.

There is, for example, in MyClass some open method camera_open(); . And there is a main entry point.

The question is this:

  • If you directly register camera.open(0); in main camera.open(0); and then camera.release(); , to clean, then all is well;
  • If in the main call camera_open() , which already contains camera.open(0); and camera.release(); , then an error occurs during the execution of the program related to read access rights.

Why and how to get rid of it, leaving the scheme with a call to the camera_open() method?

That is, it should be like this:

 int _tmain(int argc, _TCHAR* argv[]) { MyClass mc; mc.camera_open(); return 0; } void MyClass::camera_open() { camera.open(0); // действия camera.release(); } 

More specifically, I now have this:

 int _tmain(int argc, _TCHAR* argv[]) { MyClass mc; mc.ComputerVision(); // это и есть абстрактный camera_open return 0; } // не указан сервер, не указана директория, не указано время void MyClass::ComputerVision() { if (camera_open()) Start(desktop_directory()); } void MyClass::ComputerVision(std::string str, short mode) { if (mode == 0) // указан сервер, не указана директория, не указано время { if (camera_open(str)) Start(desktop_directory()); } else // указана директория, не указан сервер, не указано время { if (camera_open()) Start(str); } } // указаны сервер и директория, не указано время void MyClass::ComputerVision(std::string server, std::string directory) { if (camera_open(server)) Start(directory); } // указано время, не указан сервер, не указана директория void MyClass::ComputerVision(double time) { userTime = time; ComputerVision(); } // указано время и указана либо директория, либо сервер void MyClass::ComputerVision(double time, std::string str, short mode) { userTime = time; ComputerVision(str, mode); } // указано время, указан сервер, указана директория void MyClass::ComputerVision(double time, std::string server, std::string directory) { userTime = time; ComputerVision(server, directory); } // открытие камеры по умолчанию boolean MyClass::camera_open() { try { camera.open(0); server = ""; } catch (std::exception) { std::cout << std::endl << "Failed to connect to your web camera"; return false; } return true; } // открытие камеры по указанному серверу boolean MyClass::camera_open(std::string server) { try { camera.open(server); this->server = server; } catch (std::exception) { std::cout << std::endl << "Failed to connect to IP camera"; return false; } return true; } 

Both camera_open(...) private, the rest is public. An error occurs on camera.open(...); .

  • Something is wrong here, because errors with access rights to the device usually appear only when this device is occupied by another connection. This can also occur if you connect to the device, but do not release resources (who exactly did not release them is another matter). - alexis031182
  • The point is that I tried to connect both from the main function and from the class method to different video cameras. From main everything worked fine, and just recently decided to move this functionality to a method, having encountered this problem. - brenoritvrezorkre
  • Unfortunately, there is little information to suggest something. In main() or in the method of an arbitrary class - there is no difference, it should work the same. Update the question and provide a full listing of the minimum project at which the error occurs. - alexis031182
  • The fact is that there really is nothing else special about the problem. However, I will try. - brenoritvrezorkre

1 answer 1

Unfortunately, there is still no code in the code (see the discussion in the comments to the question) where there could be an error. Nevertheless, the minimal example that reproduces the problem situation should look a little different. For example, so.

 class CaptureWrapper { public: CaptureWrapper() {} ~CaptureWrapper() {} bool open() { return _vid_cap.open(0); } void close() { if(_vid_cap.isOpened()) _vid_cap.release(); } private: cv::VideoCapture _vid_cap; }; int main(int argc, char *argv[]) { CaptureWrapper wrapper; if(wrapper.open() == false) { std::cout << "Failed!"; return -1; } wrapper.close(); return 0; } 

In that case, if the minimum example works, you can gradually increase its functionality, such as the ability to select a device by index or path to the video file, each time checking the correctness of the application. Thus, it will be possible to easily localize the problem space in the code if it suddenly appears.

  • Thanks and on it, I will try still, I will add your introductions. The code given by me really fully shows everything that is surrounded by an error. That is, the work of the program does not go further, since when calling camera_open(...) this method tries to make camera.open(...) (it is possible without an additional method, but I chose to enter try-catch ), and a problem occurs . I thought that the matter could be in some intricacies of initialization. But it is cv::VideoCapture::open(...) that does this. Could it be an OpenCV bug, compiler weird (MVSC) or something else like that? - brenoritvrezorkre
  • If there is a problem in OpenCV (the possibility of strangeness in the behavior of the compiler can not be considered a priori), then it is rather a consequence of the possible incompatibility of the device and OpenCV. But this is quite a rare case, since OpenCV more often works as a wrapper over FFmpeg, and the latter doesn't only support devils. If your minimal example doesn't work, try try-catch right in main() if you haven't tried it. If it turns out that everything is normal there, then it will be, to put it mildly, nonsense. - alexis031182
  • I have already mentioned. Creating a video camera, its initialization and closing right in the main works great. Moreover, it works if you create an instance of the class MyClass at the entry point and, through this instance, run there and so on. But if you do everything from inside the class, then - alas. I ran into this only when I decided to move to a class, to a private zone, everything related to computer vision, and in main there were just about that. - brenoritvrezorkre
  • Then there is only the option of a gradual increase from a minimal example into something more functional. The very fact of placing the cv::VideoCapture and its open() method in the private section changes absolutely nothing. - alexis031182