c++视觉–通道分离,合并处理
通道分离: split()函数
#include <opencv2/opencv.hpp>
int main() {
cv::Mat image = cv::imread("1.jpg");
if (image.empty()) {
std::cerr << "Error: Could not read the image." << std::endl;
return -1;
}
std::vector<cv::Mat> channels;
cv::split(image, channels);
cv::imshow("Original Image", image);
cv::imshow("蓝色 Channel", channels[0]);
cv::imshow("绿色 Channel", channels[1]);
cv::imshow("红色 Channel", channels[2]);
cv::waitKey(0);
return 0;
}
再将拆开后的单通道进行对比度处理后进行合并
#include <opencv2/opencv.hpp>
int main() {
cv::Mat image = cv::imread("1.jpg");
if (image.empty()) {
std::cerr << "Error: Could not read the image." << std::endl;
return -1;
}
std::vector<cv::Mat> channels;
cv::split(image, channels);
cv::imshow("Original Image", image);
cv::imshow("蓝色 Channel", channels[0]);
cv::imshow("绿色 Channel", channels[1]);
cv::imshow("红色 Channel", channels[2]);
for (cv::Mat& channel : channels) {
cv::equalizeHist(channel, channel);
}
cv::imshow("Original Image", image);
cv::imshow("蓝色对比度增加 Channel", channels[0]);
cv::imshow("绿色对比度增加 Channel", channels[1]);
cv::imshow("红色对比度增加 Channel", channels[2]);
cv::Mat processedImage;
cv::merge(channels, processedImage);
cv::imshow("合并 Image", processedImage);
cv::waitKey(0);
return 0;
}
在分离的通道中的ROI感兴趣区域里添加logo图片
#include <opencv2/opencv.hpp>
int main() {
cv::Mat image = cv::imread("1.jpg");
cv::Mat logo = cv::imread("4.jpg",0);
if (image.empty()) {
std::cerr << "Error: Could not read the image." << std::endl;
return -1;
}
std::vector<cv::Mat> channels;
cv::split(image, channels);
cv::imshow("Original Image", image);
cv::imshow("蓝色 Channel", channels[0]);
cv::imshow("绿色 Channel", channels[1]);
cv::imshow("红色 Channel", channels[2]);
for (cv::Mat& channel : channels) {
cv::equalizeHist(channel, channel);
}
cv::imshow("Original Image", image);
cv::imshow("蓝色对比度增加 Channel", channels[0]);
cv::imshow("绿色对比度增加 Channel", channels[1]);
cv::imshow("红色对比度增加 Channel", channels[2]);
cv::Rect roiRect(100, 250, logo.cols, logo.rows);
cv::Mat imageROI = channels[0](roiRect);
logo.copyTo(imageROI);
cv::imshow("蓝色对比度增加logo区域 Channel", channels[0]);
cv::Mat processedImage;
cv::merge(channels, processedImage);
cv::imshow("合并 Image", processedImage);
cv::waitKey(0);
return 0;
}