C++ code Capturing image from IP / Ethernet Cameras (AXIS Cam) - c++

I've 4 Axis IP cams. I need a code to capture image from those cams. I've opencv code to capture image from USB cams but I don't kno how to capture from IP cams.

int main()
{
Mat frame;
namedWindow("video", 1);
VideoCapture cap("http://150.214.93.55/mjpg/video.mjpg");
while ( cap.isOpened() )
{
cap >> frame;
if(frame.empty()) break;
imshow("video", frame);
if(waitKey(30) >= 0) break;
}
return 0;
}
no idea, how your urls look like, but opencv seems to insist, it has to end with mjpg.
so if it doesn't, the trick is to append a dummy parameter:
http://my/cool/ip-cam.ie?dummy=video.mjpg
if you need to open all 4 cams at once, you need a VideoCapture for each one:
VideoCapture cap1("url1");
VideoCapture cap2("url2");
VideoCapture cap3("url3");
VideoCapture cap4("url4");

Related

Picture and text on video capture

I would like to put an image on video and i'm wondering if it's possible in opencv without multithreading.
I would like to avoid it because in my project i am operating on RPI 0W(that's whyi don't want multithreading) .
i can't find anything about it on internet. I got some basic code in c++ . I'm new to open cv.
int main(){
VideoCapture cap(0);
if (!cap.isOpened())
{
cout << "error"<<endl;
return -1;
}
Mat edges;
namedWindow("edges", 1);
Mat img = imread("logo.png");
for (;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
imshow("edges", WINDOW_AUTOSIZE );
imshow("edges", img);
imshow("edges", frame);
if (waitKey(30) >= 0) break;
}
}
In OpenCV showing two things in the same window overwrites the previous one which I think is happening in your case.
You can use OpenCV addWeighted() function or bitwise operations.
OpenCV has good documentation on this. You can find it here

I can't open 2 gige(basler) cameras at the same time with opencv

I'm using two GigE (basler aca2500-14gm) cameras on win10 opencv3.4, I connect the wires of the 2 cameras to the switch, and then connect it to my computer.but I can't open the camera and get the frames at the same time。
my code:
`
int main()
{
PylonInitialize();
VideoCapture cap(0);
VideoCapture cap1(2);
if (!cap.isOpened())
{
cout << "Camera 1 unsuccessfully opened" << endl;
}
if (!cap1.isOpened())
{
cout << "Camera 2 unsuccessfully opened" << endl;
}
bool stop = false;
while (!stop)
{
Mat frame;
Mat frame1;
cap >> frame;
cap1 >> frame1;
if (frame.empty() || frame1.empty())
{
break;
}
imshow("Open the camera 1", frame);
imshow("Open the camera 2", frame1);
if (waitKey(100) >= 0)
{
PylonTerminate();//
stop = true;
}
}
}`
by the way,when I tried to run the sample of basler SDK :Grab_MultipleCameras.cpp,
I can open the camera but the image in the window is grey.
is there anyone help me to solve this problem?
Thanks in advance.
When you run this sample of basler SDK it might happen that the second camera still could not be open but it just show you the window with defualt color (grey).
Another likely thing is that your are passing wrong device ID for VideoCapture to work, See this
OpenCv VideoCap documentation. Also from what I know if you are using GigE cameras it will be better to pass ip address of each camera to VideoCapture
So I would say only to try change one think in your code:
From
VideoCapture cap(0);
VideoCapture cap1(2);
To:
VideoCapture cap(/*camera Ip Address*/); //or try with different IDs
VideoCapture cap1(/*camera Ip Address*/);
Also take a look at this answer VideoCapture and GigE camera . It is stated there that when there is more than one camera you would be better with passing IP address.
Another thing to check will be if you can see both cameras in your device manager.
EDIT:
Hey I found nice documentation about working with Pylon SDK( from camera vendor) andOpenCV (it is probably older version of OpenCV but still can be useful)

OpenCV 3.2 - Can't create VideoCapure from IP

I want to catch video stream from IP camera in OpenCV, but OpenCV can't create VideoCapture from url, but I've got EmguCV project, where I can capture video using this url. Code:
const std::string url = "rtsp://admin:12345#192.168.6.206:554/RVi/1/1";
VideoCapture cap(url);
if (!cap.isOpened())
return -1;
namedWindow("frame", 1);
while (true)
{
Mat frame;
cap >> frame;
imshow("frame", frame);
if (waitKey(30) >= 0) break;
}
return 0;
Just for test I install OpenCV 2.9.11, and even theve everything is works.
What I do wrong?
what you need is to provide a file extension, instead of a URL. You can add the same to your URL as:
std::string url = "rtsp://admin:12345#192.168.6.206:554/RVi/1/1/x.mjpeg";

How to close camera (OpenCv Beaglebone)

Good Day,
I am trying to figure out how to close the camera on a beaglebone in openCV. I have tried numerous commands such as release(&camera) but none exist and the camera continues to stay on when I don't want it to.
VideoCapture capture(0);
capture.set(CV_CAP_PROP_FRAME_WIDTH,320);
capture.set(CV_CAP_PROP_FRAME_HEIGHT,240);
if(!capture.isOpened()){
cout << "Failed to connect to the camera." << endl;
}
Mat frame, edges, cont;
while(1){
cout<<sending<<endl;
if(sending){
for(int i=0; i<frames; i++){
capture >> frame;
if(frame.empty()){
cout << "Failed to capture an image" << endl;
return 0;
}
cvtColor(frame, edges, CV_BGR2GRAY);
Code is something like this, at the end of the for loop, I want to close the camera, but of course it still stays open
The camera will be deinitialized automatically in VideoCapture destructor.
Check this example from opencv docu:
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat edges;
namedWindow("edges",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
cvtColor(frame, edges, CV_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);
if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
Also
cvQueryFrame
Grabs and returns a frame from camera or file
IplImage* cvQueryFrame( CvCapture* capture );
capture video capturing structure.
The function cvQueryFrame grabs a frame from camera or video file, decompresses and > returns it. This function is just a combination of cvGrabFrame and cvRetrieveFrame in one > call. The returned image should not be released or modified by user.
Also check: http://derekmolloy.ie/beaglebone/beaglebone-video-capture-and-image-processing-on-embedded-linux-using-opencv/
I hope this works for you. Best of luck.

Cannot grab frame in OpenCV

I have been trying to get openCV to read an image from my computer's webcam. The code below successfully opens the webcam (green light turns on). However, attempts to grab a frame and hence read a frame fail. I am at a loss here. Can anyone help?
Many Thanks,
Hillary
P.S. I am running Mac OS X 10.9 on a MacBook Pro. And my opencv version is 2.4.6.1
And here is the code:
#include "opencv.hpp"
using namespace cv;
int main(int, char**) {
VideoCapture cap = VideoCapture(0);
if(!cap.isOpened()){
printf("failed to open camera\n");
return -1;
}
namedWindow("edges",1);
for(;;){
if(waitKey(50) >= 0 ) break;
if(!cap.grab()){
printf("failed to grab from camera\n");
}
}
return 0;
}
You forgot to read new frames in your loop and show them! There:
for(;;){
if(waitKey(50) >= 0 ) break;
Mat frame;
if(!cap.grab()){
printf("failed to grab from camera\n");
break;
}
cap >> frame;
if(frame.empty()){
printf("failed to grab from camera\n");
break;
}
imshow("edges", frame);
}