原图像的值是0-255,设置新图像的值为0~3,注意三个通道都要量化。程序简单但踩了不少坑,Mat的 at函数很麻烦,要注意一旦出现赋值操作,必须与定义时的数据类型一致。另外,在调试过程中出现了height变量触发异常的情况,修改了属性中的堆栈大小,将图片像素改小都无果,发现单步调试下只有将height变小才能运行,最后换了一张图片竟然好了,难道boy.jpg携带bug吗。
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <math.h>
cv::Mat decrease_color(cv::Mat img) {
int height = img.cols;
int width = img.rows;
int channel