WARNING
📌 The installation process requires an internet connection! Please connect the Taishan board to the network!
Install Python-OpenCV
- Update the libraries:
bash
sudo apt update && sudo apt upgrade
1
- Install aptitude:
bash
sudo apt-get install aptitude
1
- Install python3-opencv:
bash
sudo aptitude install python3-opencv
1
- Verification:
bash
python3
1
Then enter:
bash
import cv2
1
Finally, enter the following command to print the information.
If the version number appears, it means the installation was successful.
bash
print(cv2.__version__)
1
Install Libopencv
- Update the libraries:
bash
sudo apt update && sudo apt upgrade
1
- Install the library:
bash
sudo apt-get install *libopencv*
1
If this doesn't work, use the following command: sudo apt-get install libopencv-*
Code Demonstration
Objective: We will flip a JPG image. First, select an image on your computer (I'll choose the one shown below).
Now, create a cpp file calledflip_image.cppc++
#include <opencv2/opencv.hpp>
#include <iostream>
int main(int argc, char** argv)
{
if(argc != 3)
{
std::cout << "Usage: " << argv[] << " <input_image_path> <output_image_path>" << std::endl;
return -1;
}
// Load the image
cv::Mat img = cv::imread(argv[], cv::IMREAD_COLOR);
if(img.empty())
{
std::cout << "Image not found or failed to open!" << std::endl;
return -1;
}
// Flip the image
cv::Mat flipped;
cv::flip(img, flipped, 0); // 0 means vertical flip, 1 means horizontal flip, -1 means both horizontal and vertical flip
/// Save the image
bool success = cv::imwrite(argv[], flipped);
if(!success)
{
std::cout << "Error writing the image!" << std::endl;
}
else
{
std::cout << "Image written and saved successfully!!" << std::endl;
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
- Transfer the image and C++ file to the Taishan board:
We'll transfer them to the /home/lckfb directory.
- Compile the cpp file
Install the G++ compiler:
bash
sudo apt-get install g++
1
Then, compile the file:
bash
Now, you should see the executable file flip_image in the current directory. 3. Flip the image Run the file to flip the image: g++ flip_image.cpp -o flip_image `pkg-config --cflags --libs opencv4`
1
bash
After running this command, the program will read LCKFB.jpg, perform a vertical flip, and save the result as LCKFB_flipped.jpg in the same directory. 4. Check the image Transfer the LCKFB_flipped.jpg image to your computer for viewing to check if it's flipped. As shown, the image has been successfully flipped, so the Libopencv installation was successful! sudo ./flip_image LCKFB.jpg LCKFB_flipped.jpg
1
Common Issues
- If you encounter network issues, try switching to the Tsinghua mirror (but be sure to back up the original files first).
https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu-ports/