如何在Ubuntu 16.04虚拟机中通过Code::Blocks+OpenCV3访问IP摄像头?
Hey there! Since you already got the Edimax IC-3110P stream working in Visual Studio on Windows, let's get that same functionality up and running in your Ubuntu 16.04 VM with Code::Blocks and OpenCV 3. Here's a step-by-step breakdown:
1. Confirm OpenCV 3 is Properly Installed on Ubuntu
First, make sure your VM has a working OpenCV 3 installation—this is often the root cause of cross-platform issues.
- Check your current OpenCV version with this terminal command:
You should see a 3.x number (like 3.4.16, which is fully compatible with Ubuntu 16.04).pkg-config --modversion opencv - If you need to install or upgrade OpenCV 3:
- Install required dependencies:
sudo apt-get update && sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev - Grab the stable OpenCV 3.4.x source branch:
git clone https://github.com/opencv/opencv.git cd opencv && git checkout 3.4.16 mkdir build && cd build - Compile and install:
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local .. make -j$(nproc) # Uses all CPU cores for faster build sudo make install sudo ldconfig
- Install required dependencies:
2. Configure Code::Blocks to Link OpenCV 3
Code::Blocks needs to know where to find OpenCV's headers and libraries to compile your project correctly:
- Open your project, go to Settings > Compiler > Linker settings
- In the Other linker options field, add:
This automatically pulls in all necessary OpenCV libraries without manual entry.pkg-config --libs opencv - Next, go to Search directories > Compiler and add these paths:
/usr/local/include/opencv/usr/local/include/opencv2
- Then, in Search directories > Linker, add:
/usr/local/lib
3. Adapt Your Working Windows Code for Ubuntu
Your existing code is almost ready—just tweak a few Linux-specific details:
- Network Setup: Set your VirtualBox network to Bridge Adapter (Settings > Network) so your Ubuntu VM can directly reach the camera's IP on your local network.
- RTSP URL: Edimax IC-3110P uses a standard RTSP stream URL. Replace the placeholder with your camera's credentials and IP:
Ifconst std::string rtsp_url = "rtsp://your_username:your_password@camera_ip:554/h264";/h264doesn't work, try/mpeg4—some firmware versions use that alternative path. - Full Working Code (completing your partial snippet):
#include <stdio.h> #include <opencv2/opencv.hpp> #include <iostream> int main(int, char**) { cv::VideoCapture vcap; // Update this with your camera's actual RTSP URL const std::string rtsp_url = "rtsp://admin:12345@192.168.1.100:554/h264"; // Open the RTSP stream if (!vcap.open(rtsp_url)) { std::cerr << "Error: Could not open video stream" << std::endl; return -1; } cv::Mat frame; while (true) { // Grab a frame from the stream if (!vcap.read(frame)) { std::cerr << "Error: No frame captured" << std::endl; break; } // Display the frame cv::imshow("Edimax IC-3110P Live Stream", frame); // Exit when 'q' is pressed if (cv::waitKey(1) == 'q') { break; } } // Clean up resources vcap.release(); cv::destroyAllWindows(); return 0; }
4. Troubleshooting Common Hiccups
- Can't reach the camera: Ping the camera's IP from Ubuntu (
ping camera_ip) to confirm connectivity. If it fails, double-check your VirtualBox network mode and router settings. - OpenCV can't open the stream: Verify the RTSP URL is correct with
ffplay(install withsudo apt-get install ffmpeg) to rule out OpenCV-specific issues:ffplay rtsp://your_username:your_password@camera_ip:554/h264 - Code::Blocks build errors: If the
pkg-configline doesn't work, manually add the OpenCV libraries to linker options:-lopencv_core -lopencv_highgui -lopencv_videoio -lopencv_imgproc - Laggy stream: Enable 3D acceleration in VirtualBox (Settings > Display) and lower the camera's resolution via its web interface to reduce VM resource usage.
内容的提问来源于stack exchange,提问作者Winbuntu
相关产品推荐
相关产品推荐

