opencv 图像像素 颜色空间缩减

#include <opencv2/core/core.hpp>  
#include <opencv2/highgui/highgui.hpp>  
#include <iostream>  
using namespace std;  
using namespace cv;  


void colorReduce(Mat& inputImage, Mat& outputImage, int div)  
{  
    outputImage = inputImage.clone();
    int rowNumber = outputImage.rows;
    int colNumber = outputImage.cols * outputImage.channels();


    for(int i = 0; i < rowNumber; i++)
    {  
        uchar* data = outputImage.ptr<uchar>(i);
        
        //printf("\n%02X\n", i);
        for(int j = 0; j < colNumber; j++)
        {      
            //printf("%02X/", data[j]);
            data[j] = data[j] / div * div + div/2;  
            //printf("%02X ", data[j]);
            
        }
    }  
}  


int main( )  
{  

    Mat srcImage = imread("1.jpg");  
    imshow("原始图像",srcImage);  

    Mat dstImage;
    dstImage.create(srcImage.rows, srcImage.cols, srcImage.type());

    double time0 = static_cast<double>(getTickCount());  

    colorReduce(srcImage, dstImage, 32);  

    time0 = ((double)getTickCount() - time0)/getTickFrequency();
    cout<<"\t此方法运行时间为: "<<time0<<""<<endl;

    imshow("效果图",dstImage);  
    waitKey(0);  
}  

 

 

posted @ 2019-02-28 22:39  hehe_2014  阅读(196)  评论(0编辑  收藏  举报