作者:Steven
著作權宣告:著作權歸作者所有,商業轉載請聯系作者獲得授權,非商業轉載請注明出處
實作原理
懷舊色可以通過調整RGB三通道數值實作,具體公式:
功能函式代碼
// 懷舊色
cv::Mat Nostalgic(cv::Mat src)
{
CV_Assert(src.channels() == 3);
int row = src.rows;
int col = src.cols;
cv::Mat temp = src.clone();
for (int i = 0; i < row; ++i)
{
uchar *s = src.ptr<uchar>(i);
uchar *t = temp.ptr<uchar>(i);
for (int j = 0; j < col; ++j)
{
int B = s[3 * j];
int G = s[3 * j + 1];
int R = s[3 * j + 2];
// 懷舊調色
float newB = 0.272f*R + 0.534f*G + 0.131f*B;
float newG = 0.349f*R + 0.686f*G + 0.168f*B;
float newR = 0.393f*R + 0.769f*G + 0.189f*B;
if (newB < 0)
newB = 0;
if (newB > 255)
newB = 255;
if (newG < 0)
newG = 0;
if (newG > 255)
newG = 255;
if (newR < 0)
newR = 0;
if (newR > 255)
newR = 255;
t[3 * j] = (uchar)newB;
t[3 * j + 1] = (uchar)newG;
t[3 * j + 2] = (uchar)newR;
}
}
return temp;
}
C++測驗代碼
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
cv::Mat Nostalgic(cv::Mat src);
int main()
{
cv::Mat src = imread("test2.jpg");
cv::Mat result1 = Nostalgic(src);
cv::Mat gray;
cvtColor(src, gray, COLOR_BGR2GRAY);
cv::imshow("original", src);
cv::imshow("gray", gray);
cv::imshow("result", result);
waitKey(0);
return 0;
}
// 懷舊色
cv::Mat Nostalgic(cv::Mat src)
{
CV_Assert(src.channels() == 3);
int row = src.rows;
int col = src.cols;
cv::Mat temp = src.clone();
for (int i = 0; i < row; ++i)
{
uchar *s = src.ptr<uchar>(i);
uchar *t = temp.ptr<uchar>(i);
for (int j = 0; j < col; ++j)
{
int B = s[3 * j];
int G = s[3 * j + 1];
int R = s[3 * j + 2];
// 懷舊調色
float newB = 0.272f*R + 0.534f*G + 0.131f*B;
float newG = 0.349f*R + 0.686f*G + 0.168f*B;
float newR = 0.393f*R + 0.769f*G + 0.189f*B;
if (newB < 0)
newB = 0;
if (newB > 255)
newB = 255;
if (newG < 0)
newG = 0;
if (newG > 255)
newG = 255;
if (newR < 0)
newR = 0;
if (newR > 255)
newR = 255;
t[3 * j] = (uchar)newB;
t[3 * j + 1] = (uchar)newG;
t[3 * j + 2] = (uchar)newR;
}
}
return temp;
}
測驗效果
懷舊色濾鏡完成,與灰度圖相比,有種古典蕭瑟的感覺~
如果函式有什么可以改進完善的地方,非常歡迎大家指出,一同進步何樂而不為呢~
如果文章幫助到你了,可以點個贊讓我知道,我會很快樂~加油!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/296785.html
標籤:其他
