#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
//指针读取像素
void guidRead_PhotoNumber (Mat &inputImage, Mat &outputImage, int div);
// 动态地址计算 读取像素
void DAC_PhotoNumber (Mat &inputImage, Mat &outputImage, int div);
int main(int argc, char** argv)
{
Mat src_Image = imread ("4.jpg");
imshow ("原图像", src_Image);
Mat dst_Image;
//指针读取像素方法 进行颜色空间缩减
DAC_PhotoNumber (src_Image ,dst_Image,16);
// 动态地址计算 读取像素 进行颜色空间缩减
//guidRead_PhotoNumber (src_Image ,dst_Image,16);
imshow ("效果图",dst_Image);
waitKey (0);
return 0;
}
void guidRead_PhotoNumber (Mat &inputImage, Mat &outputImage, int div)
{
outputImage = inputImage.clone ();
int rowNumber = outputImage.rows ;
int colNumber = outputImage.cols * outputImage.channels (); //列数* 通道数 = 每一行所有元素个数
int allNumber = colNumber * outputImage .cols ;
for (int i = 0; i<rowNumber ; i++)
{
uchar *data = outputImage.ptr<uchar> (i); //用 data 指向i 行的首个元素
for (int j = 0; j<colNumber ; j++)
{
// 颜色空间缩减 其中数据类型为 uchar 无符号字符类型
data[j] = data[j]/div*div + div/2;
}
}
}
void DAC_PhotoNumber (Mat &inputImage, Mat &outputImage, int div)
{
outputImage = inputImage.clone ();
int rowNumber = outputImage.rows ;
int colNumber = outputImage.cols;
for (int i = 0; i< rowNumber ; i++)
{
for (int j = 0; j< colNumber ; j++)
{
// 颜色空间缩减 其中数据类型为 uchar 无符号字符类型
outputImage .at<Vec3b>(i,j)[0] = outputImage .at<Vec3b>(i,j)[0] / div*div + div/2;
outputImage .at<Vec3b>(i,j)[1] = outputImage .at<Vec3b>(i,j)[1] / div*div + div/2;
outputImage .at<Vec3b>(i,j)[1] = outputImage .at<Vec3b>(i,j)[1] / div*div + div/2;
//cout <<"["<<outputImage .at<Vec3b>(i,j)[0] <<","<<outputImage .at<Vec3b>(i,j)[1] <<","<<outputImage .at<Vec3b>(i,j)[2] <<"]";
}
}
}
访问图像内像素的两种常用方法
最新推荐文章于 2023-04-11 11:32:48 发布