reduce color

在看到Scanning an image with pointers中的减少图像中的色彩这个task时,对其中的原理不是很明白,现在想想主要是对被除数N的理解,当一维的像素点/N时,其实是代表取整,假设N为64,像素点为0到64,则64/N为0,表示的是在0到63之间都是0,同理在64到127之间为1,依此类推。。。

至于书上的像素点/N之后乘以N,其结果在加上N/2。表示的是取每段的中间值,即4个。原来表示的色彩为256*256*256,现在则为4*4*4.

时钟周期:如果cpu的时钟频率为8MHz,则时钟周期为:1/8MHz = 125 ns。

我的电脑为2.3GHz,则我的时钟周期为:1/2.3 = 0.43 ns

代码如下:

// a2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <OpenCV245.h>

using namespace std;
using namespace cv;

//void reduceColor(Mat &image, int N)
//{
//	int ni = image.cols;
//	int nj = image.rows;
//	int np = image.cols * image.channels();
//	for (int j = 0; j < nj; j++)
//	{
//		uchar* data = image.ptr<uchar>(j);
//
//		for (int i = 0; i < np; i++)
//		{
//			data[i] = data[i]/N*N + N/2;
//		}
//	}
//}

void reduceColor(Mat &image, int div)
{
	int ni = image.cols;
	int nj = image.rows;
	for (int j = 0; j < nj; j++)
	{
		for (int i = 0; i < ni; i++)
		{
			image.at<Vec3b>(j, i)[0] = image.at<Vec3b>(j , i)[0]/div*div  +  div/2;
			image.at<Vec3b>(j, i)[1] = image.at<Vec3b>(j , i)[1]/div*div  +  div/2;
			image.at<Vec3b>(j, i)[2] = image.at<Vec3b>(j , i)[2]/div*div  +  div/2;
		}
	}
}




int _tmain(int argc, _TCHAR* argv[])
{
	Mat img = imread("C:\\Users\\sony\\Desktop\\pic\\Lena.jpg");

	double duration = static_cast<double>(getTickCount());
	reduceColor(img, 64);
    duration = static_cast<double>(getTickCount()) - duration;
	cout<<"reduce time: "<<duration<<endl;

	imshow("dst", img);
	waitKey(0);
	return 0;
}



你可能感兴趣的:(reduce color)