文章目录
- 创建空白图片
- 创建一张渐变色彩色
- 绘制多边形
- 绘制多线
- 改变像素点颜色
创建空白图片
bool tool_class::creatEmpty(int width, int height, std::string image_p)
{
cv::Mat blankImage(height, width, CV_8UC3, cv::Scalar(255, 255, 255));
cv::imwrite(image_p.c_str(), blankImage);
cv::imshow("Blank Image", blankImage);
cv::waitKey(0);
cv::destroyAllWindows();
return true;
}
创建一张渐变色彩色
bool tool_class::creatColor(std::string image_p)
{
int width = 640;
int height = 480;
cv::Mat gradientImage(height, width, CV_8UC3);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
uchar blue = static_cast<uchar>(x * 255 / width);
uchar green = static_cast<uchar>((x + y) * 255 / (width + height));
uchar red = static_cast<uchar>(y * 255 / height);
gradientImage.at<cv::Vec3b>(y, x) = cv::Vec3b(blue, green, red);
}
}
cv::imwrite(image_p.c_str(), gradientImage);
cv::imshow("Gradient Image", gradientImage);
cv::waitKey(0);
cv::destroyAllWindows();
return true;
}
绘制多边形
bool tool_class::drawPolygon(std::string image_p, std::vector<cv::Point> points)
{
cv::Mat ima = cv::imread(image_p.c_str());
cv::Scalar red = cv::Scalar(0, 0, 255);
cv::Scalar blue = cv::Scalar(255, 0, 0);
int thickness = 2;
cv::polylines(ima, points, true, red, thickness, 8, 0);
cv::fillPoly(ima, std::vector<std::vector<cv::Point>>{points}, blue, 8, 0);
cv::imshow("Image with line", ima);
cv::imwrite(image_p.c_str(), ima);
return true;
}
绘制多线
bool tool_class::drawLines(std::string image_p, std::vector<cv::Point> points)
{
cv::Mat ima = cv::imread(image_p.c_str());
cv::Scalar red = cv::Scalar(0, 0, 255);
int thickness = 2;
for (size_t i = 0; i < points.size() - 1; i++)
{
cv::Point2f start = points[i];
cv::Point2f end = points[i + 1];
cv::line(ima, start, end, red, thickness);
}
cv::imwrite(image_p.c_str(), ima);
return true;
}
改变像素点颜色
bool tool_class::changeColor(std::string image_p, int width, int height)
{
cv::Mat ima = cv::imread(image_p.c_str());
cv::Scalar Red = cv::Scalar(0, 0, 255);
ima.at<cv::Vec3b>(height, width)[0] = 0;
ima.at<cv::Vec3b>(height, width)[1] = 0;
ima.at<cv::Vec3b>(height, width)[2] = 255;
cv::imwrite(image_p.c_str(), ima);
return true;
}