#include <opencv2/opencv.hpp>
#include <iostream>
#include <opencv2/imgproc/types_c.h>
using namespace cv;
using namespace std;
int main()
{
Mat Img = Mat(500, 500, CV_8UC1, cv::Scalar(0));
Mat image = Mat(500, 500, CV_8UC1, cv::Scalar(50));
Point A = Point(150, 150);
Point B = Point(280, 280);
LineIterator line_iter(image, A, B);
vector<Point> v_pnts;
v_pnts.reserve(line_iter.count + 2);
for (int i = 0; i < line_iter.count; ++i, ++line_iter)
{
v_pnts.emplace_back(line_iter.pos());
}
v_pnts.emplace_back(A);
v_pnts.emplace_back(B);
for (int i = 0; i < v_pnts.size(); i++)
{
Img.at<uchar>(v_pnts[i]) = 255;
}
imshow("image_color123", Img);
Mat image_color = image.clone();
cvtColor(image_color, image_color, CV_GRAY2BGR);
for (const auto& kn : v_pnts)
{
image_color.ptr<Vec3b>(kn.y)[kn.x] = Vec3b(0, 0, 255);
}
imshow("image_color", image_color);
waitKey();
return 0;
}
所有的坐标顶点都存放在vector v_pnts内;
结果如下图: