给出10个opencv c++示例代码
时间: 2023-05-24 10:03:19 浏览: 244
1. 读取并显示图像:
```
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat img = imread("image.jpg");
imshow("image", img);
waitKey(0);
return 0;
}
```
2. 灰度图像转换:
```
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat img = imread("image.jpg");
Mat gray;
cvtColor(img, gray, COLOR_BGR2GRAY);
imshow("gray image", gray);
waitKey(0);
return 0;
}
```
3. 图像裁剪:
```
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat img = imread("image.jpg");
Rect roi(100, 100, 200, 200);
Mat crop = img(roi);
imshow("cropped image", crop);
waitKey(0);
return 0;
}
```
4. 图像缩放:
```
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat img = imread("image.jpg");
Mat resize;
cv::resize(img, resize, cv::Size(), 0.5, 0.5);
imshow("resized image", resize);
waitKey(0);
return 0;
}
```
5. 图像旋转:
```
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat img = imread("image.jpg");
Mat rotate;
Point2f center(img.cols/2, img.rows/2);
Mat rot_mat = getRotationMatrix2D(center, 45, 1.0);
warpAffine(img, rotate, rot_mat, img.size());
imshow("rotated image", rotate);
waitKey(0);
return 0;
}
```
6. 图像阈值化:
```
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat img = imread("image.jpg", 0);
Mat threshold;
threshold(img, threshold, 128, 255, THRESH_BINARY);
imshow("threshold image", threshold);
waitKey(0);
return 0;
}
```
7. 图像边缘检测:
```
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat img = imread("image.jpg", 0);
Mat edges;
Canny(img,
阅读全文