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;
}
相关推荐
海绵宝宝贾克斯儿36 分钟前
C++中如何实现一个单例模式?
开发语言·c++·单例模式
Epiphany.55639 分钟前
素数筛(欧拉筛算法)
c++·算法·图论
龙湾开发42 分钟前
计算机图形学编程(使用OpenGL和C++)(第2版)学习笔记 10.增强表面细节(二)法线贴图
c++·笔记·学习·图形渲染·贴图
whoarethenext1 小时前
c/c++的opencv的轮廓匹配初识
c语言·c++·opencv
爱吃涮毛肚的肥肥(暂时吃不了版)1 小时前
项目班——0510——JSON网络封装
c++·算法·json
apocelipes1 小时前
使用libdivide加速整数除法运算
c语言·c++·性能优化·linux编程
新手村领路人1 小时前
qt5.14.2 opencv调用摄像头显示在label
qt·opencv·命令模式
虾球xz1 小时前
游戏引擎学习第290天:完成分离渲染
c++·人工智能·学习·游戏引擎
易只轻松熊1 小时前
C++(20): 文件输入输出库 —— <fstream>
开发语言·c++·算法
虾球xz1 小时前
游戏引擎学习第285天:“Traversables 的事务性占用”
c++·学习·游戏引擎