OpenCV.2.Computer.Vision.Application.Programming.Cookbook--Scanning an image with pointers

#include<opencv2\opencv.hpp>

void colorReduce(cv::Mat &image, int div=64) 
{  
	int nr= image.rows; // number of rows  
	int nc= image.cols * image.channels(); // total number of elements per line  
	for (int j=0; j<nr; j++)
	{  
		// get the address of row j
		//ptr:It is a template method that returns the address of row number j:
		uchar* data= image.ptr<uchar>(j);  
		for (int i=0; i<nc; i++) 
		{  
			//we could have equivalently used pointer arithmetic to move from column to column

			// process each pixel ---------------------

			//data[i]= data[i]/div*div + div/2;  

			//data[i]= data[i]-data[i]%div + div/2;

			// mask used to round the pixel value
			int n= static_cast<int>(log(static_cast<double>(div))/log(2.0));
			uchar mask= 0xFF<<n;
			data[i]=(data[i]&mask) + div/2;
			// e.g. for div=16, mask= 0xF0

			// end of pixel processing ----------------
		}                    
	}  
}  

int main(int argc,char* argv[])
{
	cv::Mat pImg;

	pImg=cv::imread("lena.jpg");
	cv::namedWindow("Image");
	cv::imshow("Image",pImg);

	colorReduce(pImg);

	
	cv::namedWindow("pImg");
	cv::imshow("pImg",pImg);

	cv::waitKey(0);

	cv::destroyWindow("Image");
	return 0;
}



color reduction is achieved by taking advantage of an integer division that floors the division result to the nearest lower integer:       

data[i]= data[i]/div*div + div/2;


The reduced color could have also been computed using the modulo operator which brings us to the nearest multiple of div (the 1D reduction factor):    

data[i]= data[i] – data[i]%div + div/2;
But this computation is a bit slower because it requires reading each pixel value twice.


Another option would be to use bitwise operators. Indeed, if we restrict the reduction factor to a power of 2, that is, div=pow(2,n), then masking the first n bits of the pixel value would give us the nearest lower multiple of div. This mask would be computed by a simple bit shift:
// mask used to round the pixel value
uchar mask= 0xFF<<n;

 // e.g. for div=16, mask= 0xF0

The color reduction would be given by:    

data[i]= (data[i]&mask) + div/2;
In general, bitwise operations lead to very efficient code, so they could constitute a powerful alternative when efficiency is a requirement.


In our color reduction example, the transformation is directly applied to the input image,which is called an in-place transformation.However, in some applications, the user wants to keep the original image intact. The user would then be forced to create a copy of the image before calling the function. Note that the easiest way to create an identical deep copy of an image is to call the clone method

cv::Mat pImgClone=pImg.clone();
	colorReduce0(pImgClone);

This extra overload can be avoided by defining a function that gives the option to the user to either use or not use the in-place processing.

void colorReduce12(const cv::Mat &image, // input image   
                 cv::Mat &result,      // output image  
                 int div=64) 
{  
	int nr= image.rows; // number of rows  
	int nc= image.cols ; // number of columns  
	// allocate output image if necessary  
	result.create(image.rows,image.cols,image.type());  
	// created images have no padded pixels  
	nc= nc*nr;   
	nr= 1;  // it is now a 1D array  
	int n= static_cast<int>(log(static_cast<double>(div))/log(2.0));  
	// mask used to round the pixel value  
	uchar mask= 0xFF<<n; // e.g. for div=16, mask= 0xF0  
	for (int j=0; j<nr; j++) 
	{  
		uchar* data= result.ptr<uchar>(j);  
		const uchar* idata= image.ptr<uchar>(j);  
		for (int i=0; i<nc; i++) 
		{  
			*data++= (*idata++)&mask + div/2;  
			*data++= (*idata++)&mask + div/2;  
			*data++= (*idata++)&mask + div/2;  
		} // end of row                   
	}  
}  

int main(int argc,char* argv[])
{
	cv::Mat pImg,pImg1;

	pImg=cv::imread("lena.jpg");
	//cv::namedWindow("Image");
	cv::imshow("Image",pImg);


	//salt(pImg,300);
	
	//cv::Mat pImgClone=pImg.clone();
	//colorReduce0(pImgClone);

	colorReduce12(pImg,pImg1);

	//cv::namedWindow("pImg1");
	cv::imshow("pImg1",pImg1);

	cv::waitKey(0);

	//cv::destroyWindow("Image");
	//cv::destroyWindow("pImgClone");
	//cv::destroyAllWindows();
	return 0;
}

实验结果:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值