彩色图像处理

mac2025-10-30  15

1.对“lena.jpg”的彩色图像分离出R、G、B三个通道图像,分别求其补色,然后再合并为一张图像保存并显示 源代码:

#include "opencv.hpp" #include <opencv2/core/core.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/imgproc/types_c.h> #include <opencv2/highgui/highgui.hpp> #include <opencv2/highgui/highgui_c.h> #include <iostream> using namespace cv; using namespace std; int main() { Mat img = imread("C:\\Users\\15518\\Desktop\\opencv\\lena.jpg"); imshow("原图像", img); Mat dst1, newImage; img.copyTo(dst1); int row = img.rows; int col = img.cols; for (int i = 0; i < row; ++i) for (int j = 0; j < col; ++j) { int b = img.at<Vec3b>(i, j)[0];//blue原色 int g = img.at<Vec3b>(i, j)[1];//green原色 int r = img.at<Vec3b>(i, j)[2];//red原色 int maxrgb = max(max(r, g), b); int minrgb = min(min(r, g), b); dst1.at<Vec3b>(i, j)[0] = maxrgb + minrgb - b; dst1.at<Vec3b>(i, j)[1] = maxrgb + minrgb - g; dst1.at<Vec3b>(i, j)[2] = maxrgb + minrgb - r; } Mat Image[3]; split(dst1, Image); imshow("B通道补色图", Image[0]); imshow("G通道补色图", Image[1]); imshow("R通道补色图", Image[2]); merge(Image, 3, newImage); imshow("补色合并后的图像", newImage); imwrite("C:\\Users\\15518\\Desktop\\opencv\\weiImage.jpg", newImage); waitKey(); return 0; }

结果图像: 2.对“lena.jpg” 的彩色图像分离为H、S、V三个通道图像,分别采用平滑滤波处理,分别显示,且合并为一张图像保存 源代码:

#include "opencv.hpp" #include <opencv2/core/core.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/imgproc/types_c.h> #include <opencv2/highgui/highgui.hpp> #include <opencv2/highgui/highgui_c.h> #include <iostream> using namespace cv; using namespace std; int main() { Mat srcImage = imread("C:\\Users\\15518\\Desktop\\opencv\\lena.jpg"); if (!srcImage.data) { cout << "图像加载失败!" << endl; return false; } else cout << "图像加载成功!" << endl << endl; imshow("原图像", srcImage); //将RGB图像转换为HSV图 Mat HSVImage, newImage; Mat Image[3]; cvtColor(srcImage, HSVImage, COLOR_BGR2HSV); split(HSVImage, Image); Mat HImage; Mat SImage; Mat VImage; blur(Image[0], HImage, Size(3, 3)); blur(Image[1], SImage, Size(3, 3)); blur(Image[2], VImage, Size(3, 3)); imshow("H", HImage); imshow("S", SImage); imshow("V", VImage); //将3个单通道重新合成一个三通道图像 merge(Image, 3, newImage); imshow("将H S V通道合并后的图像", newImage); imwrite("C:\\Users\\15518\\Desktop\\opencv\\weiImage1.jpg", newImage); waitKey(); return 0; }

结果图像: 、 3.对灰度图像转成伪彩色图,并显示和保存 源代码:

#include<opencv2/opencv.hpp> using namespace cv; using namespace std; Mat scaleGray(const Mat& inputGray) { Mat outputGray(inputGray.size(), CV_8U); unsigned char grayValue, maxValue = 1; for (int y = 0; y < inputGray.rows; y++) { for (int x = 0; x < inputGray.cols; x++) { grayValue = inputGray.at<uchar>(y, x); maxValue = max(maxValue, grayValue); } } float scale = 255.0 / maxValue; for (int y = 0; y < inputGray.rows; y++) { for (int x = 0; x < inputGray.cols; x++) { outputGray.at<uchar>(y, x) = static_cast<unsigned char>(inputGray.at<uchar>(y, x)*scale + 0.5); } } return outputGray; } Mat gray2pseudocolor(const Mat& scaledGray) { Mat outputPseudocolor(scaledGray.size(), CV_8UC3); unsigned char grayValue; for (int y = 0; y < scaledGray.rows; y++) { for (int x = 0; x < scaledGray.cols; x++) { grayValue = scaledGray.at<uchar>(y, x); Vec3b & pixel = outputPseudocolor.at<Vec3b>(y, x); pixel[0] = abs(255 - grayValue); pixel[1] = abs(127 - grayValue); pixel[2] = abs(0 - grayValue); } } return outputPseudocolor; } int main() { //输入灰度图像 Mat inputGray = imread("C:\\Users\\15518\\Desktop\\opencv\\gf3.jpg",0); imshow("原图像", inputGray); Mat scaledGray = scaleGray(inputGray); Mat pseudocolor = gray2pseudocolor(scaledGray); imshow("伪彩色图像", pseudocolor); imwrite("C:\\Users\\15518\\Desktop\\opencv\\pseudocolor.jpg", pseudocolor); waitKey(); return 0; }

结果图象:

最新回复(0)