opencv改变像素点的颜色---------c++

改变像素点的颜色

cpp 复制代码
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <filesystem>

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);  // Red color  

	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;

	// 或者
	//uchar blue = 0;
	//uchar green = 0;
	//uchar red = 255;
	//ima.at<cv::Vec3b>(y_coor, x_coor) = cv::Vec3b(blue, green, red);

	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;
}
相关推荐
blueman88883 小时前
Qt5通过vcpkg中调用时,在debug模式下调试时总是调用release的plugins文件夹中的dll
c++·qt·cmake
m沐沐6 小时前
【深度学习】dlib 人脸关键点检测
人工智能·深度学习·opencv·计算机视觉·pycharm·人脸识别·关键点检测
jinyishu_6 小时前
C++ string使用方法
开发语言·c++
乐观勇敢坚强的老彭6 小时前
C++浮点数使用注意事项
开发语言·c++
库克克7 小时前
【C++ 】内联函数
java·开发语言·c++
盐焗鹌鹑蛋7 小时前
【C++】红黑树
c++
aaPIXa6229 小时前
C++采样引导优化SPGO——比PGO更智能的编译器决策新方案
java·c++·人工智能
Starmoon_dhw10 小时前
题解:P17078 夏日甜点
c++·学习·算法·图论
某不知名網友10 小时前
C/C++动态内存管理,智能指针及RAlI思想。
java·c语言·c++
hold?fish:palm11 小时前
从源码到可执行文件:C++程序的编译过程
开发语言·c++