使用c++来写你上一个给我代码
时间: 2024-03-14 11:44:33 浏览: 17
好的,这是使用C++和OpenCV的代码,实现找到两幅图片的不同并用矩形标出:
```c++
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat img1 = imread("image1.png");
Mat img2 = imread("image2.png");
Mat gray1, gray2;
cvtColor(img1, gray1, COLOR_BGR2GRAY);
cvtColor(img2, gray2, COLOR_BGR2GRAY);
Mat diff;
absdiff(gray1, gray2, diff);
Mat thresh;
threshold(diff, thresh, 30, 255, THRESH_BINARY);
Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5));
morphologyEx(thresh, thresh, MORPH_CLOSE, kernel);
vector<vector<Point>> contours;
findContours(thresh, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
for (size_t i = 0; i < contours.size(); i++)
{
Rect rect = boundingRect(contours[i]);
rectangle(img1, rect, Scalar(0, 0, 255), 2);
}
imshow("diff", img1);
waitKey(0);
destroyAllWindows();
return 0;
}
```
这里使用了OpenCV的C++接口,与Python的实现非常类似,只是函数名称和参数略有不同。