偷偷拿来记录一下萌新的cs路——day 41
R = 0.393r + 0.769g + 0.189b
G = 0.349r + 0.686g + 0.168b
B = 0.272r + 0.534g + 0.131b
#include#include #include using namespace cv; int main() { auto frame = imread("cat.jpg"); // 读取图像 int width = frame.cols; int heigh = frame.rows; RNG rng; Mat img(frame.size(), CV_8UC3); for (int y = 0; y < heigh; y++) { uchar* P0 = frame.ptr (y); uchar* P1 = img.ptr (y); for (int x = 0; x < width; x++) { float B = P0[3 * x]; float G = P0[3 * x + 1]; float R = P0[3 * x + 2]; float newB = 0.272 * R + 0.534 * G + 0.131 * B; float newG = 0.349 * R + 0.686 * G + 0.168 * B; float newR = 0.393 * R + 0.769 * G + 0.189 * 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; P1[3 * x] = (uchar)newB; P1[3 * x + 1] = (uchar)newG; P1[3 * x + 2] = (uchar)newR; } } imshow("origin", frame); imshow("filter", img); waitKey(); imwrite("E:/filter.jpg", img); // 将img输出到图像文件 destroyAllWindows(); return 0; }
运行结果
学了更多知识还会回来更新的!有误之处请大佬指正,感谢!