opencv通过三种方式访问Mat类型的数据
作者:互联网
#include<opencv2/opencv.hpp>
#include<iostream>
#include<vector>
using namespace std;
using namespace cv;
void colorReduce(Mat&srcImage, Mat&dstImage, int div);
int main(int argc, char**argv)
{
Mat srcImage = imread("D:/pic/pic_fgo.jpeg");
Mat dstImage;
double time0 = static_cast<double>(getTickCount());
colorReduce(srcImage, dstImage, 32);
time0 = ((double)getTickCount() - time0) / getTickFrequency();
imshow("效果图", dstImage);
waitKey(0);
}
void colorReduce(Mat&srcImage, Mat&dstImage, int div)
{
dstImage = srcImage.clone();
int rowNumber = dstImage.rows;
int colNumber = dstImage.cols*dstImage.channels();
for (int i = 0; i < rowNumber; i++)
{
uchar*data = dstImage.ptr<uchar>(i);
for (int j = 0; j < colNumber; j++)
{
data[j] = data[j] / div * div + div / 2;
}
}
}
void colorReduce(Mat&srcImage, Mat&dstImage, int div)
{
dstImage = srcImage.clone();
Mat_<Vec3b>::iterator it = dstImage.begin<Vec3b>();
Mat_<Vec3b>::iterator itend = dstImage.end<Vec3b>();
for (; it != itend; it++)
{
(*it)[0] = (*it)[0] / div * div + div / 2;
(*it)[1] = (*it)[1] / div * div + div / 2;
(*it)[2] = (*it)[2] / div * div + div / 2;
}
}
void colorReduce(Mat&srcImage, Mat&dstImage, int div)
{
dstImage = srcImage.clone();
int rowNumber = srcImage.rows;
int colNumber = srcImage.cols;
for(int i=0;i<rowNumber;i++)
for (int j = 0; j < colNumber; j++)
{
dstImage.at<Vec3b>(i, j)[0] = srcImage.at<Vec3b>(i, j)[0] / div * div + div / 2;
dstImage.at<Vec3b>(i, j)[1] = srcImage.at<Vec3b>(i, j)[1]/ div * div + div / 2;
dstImage.at<Vec3b>(i, j)[2] = dstImage.at<Vec3b>(i, j)[2] / div * div + div / 2;
}
}
标签:srcImage,Mat,dstImage,colorReduce,opencv,int,三种,div 来源: https://blog.csdn.net/qq_43036419/article/details/112685577