qimage 根据横纵坐标绘制轮廓
时间: 2023-11-23 21:06:50 浏览: 165
您可以使用OpenCV中的findContours函数来查找图像中的轮廓,并使用drawContours函数在QImage上绘制这些轮廓。以下是一个简单的示例代码:
```cpp
// 加载图像
QImage image("image.png");
// 将QImage转换为OpenCV的Mat格式
cv::Mat mat(image.height(), image.width(), CV_8UC4, image.bits(), image.bytesPerLine());
cv::cvtColor(mat, mat, cv::COLOR_BGRA2GRAY);
// 查找轮廓
std::vector<std::vector<cv::Point>> contours;
cv::findContours(mat, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
// 在QImage上绘制轮廓
QPainter painter(&image);
painter.setPen(QPen(Qt::red, 2));
for (const auto& contour : contours)
{
QVector<QPoint> qpoints;
for (const auto& point : contour)
{
qpoints.append(QPoint(point.x, point.y));
}
painter.drawPolyline(qpoints);
}
// 显示结果
QLabel label;
label.setPixmap(QPixmap::fromImage(image));
label.show();
```
此代码将在QImage中绘制图像中的所有轮廓。请注意,此代码仅为示例,可能需要根据您的实际需求进行修改。
阅读全文
相关推荐














