改变像素点的颜色
#include
#include
#include
bool opencvTool::changeColor(const std::string image_p, int x_coor, int y_coor, const cv::Scalar color)
{
std::filesystem::path file(image_p);
if (!std::filesystem::exists(file))
{
std::cout << image_p << " is not exist" << std::endl;
return false;
}
cv::Mat ima = cv::imread(image_p.c_str());
cv::Scalar Red = cv::Scalar(0, 0, 255);
if (x_coor> ima.cols || x_coor < 0)
{
printf("Input x_coor: %d which exceeds width range of the image: %d \n", x_coor, ima.cols);
return false;
}
if (y_coor > ima.rows || y_coor< 0)
{
printf("Input y_coor: %d which exceeds height range of the image: %d \n", y_coor, ima.rows);
return false;
}
ima.at<cv::Vec3b>(y_coor, x_coor)[0] = 0;
ima.at<cv::Vec3b>(y_coor, x_coor)[1] = 0;
ima.at<cv::Vec3b>(y_coor, x_coor)[2] = 255;
cv::imwrite(image_p.c_str(), ima);
return true;
}
bool opencvTool::changeColor(cv::Mat& image, int x_coor, int y_coor, const cv::Scalar color)
{
if (image.empty())
{
std::cout << "Error: empty mat" << std::endl;
return false;
}
if (x_coor > image.cols || x_coor < 0)
{
printf("Input x_coor: %d which exceeds width range of the image: %d \n", x_coor, image.cols);
return false;
}
if (y_coor > image.rows || y_coor < 0)
{
printf("Input y_coor: %d which exceeds height range of the image: %d \n", y_coor, image.rows);
return false;
}
image.at<cv::Vec3b>(y_coor, x_coor) = cv::Vec3b(color[0], color[1], color[2]);
return true;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66