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=6;
			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.

时间: 2024-10-07 17:16:58

OpenCV.2.Computer.Vision.Application.Programming.Cookbook--Scanning an image with pointers的相关文章

OpenCV.2.Computer.Vision.Application.Programming.Cookbook--Efficient scanning of continuous images

#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 if (image.isContinuous()) { // then no padded pixels nc

Computer vision:algorithm and application 计算机视觉算法与应用

最近在看Computer vision:algorithm and application计算机视觉算法与应用这本书,感觉对机器视觉学习很好的一本书,下面对书本知识和网上的资源作个总结. 先从后面的附录开始看,其中包括数据集站点,相关机器视觉软件和参考文献,对今后算法学习与实现有用. 下面是学长整理的一些资料,现在搬过来学习学习: 以下是computer vision:algorithm and application计算机视觉算法与应用这本书中附录里的关于计算机视觉的一些测试数据集和源码站点,

Graph Cut and Its Application in Computer Vision

Graph Cut and Its Application in Computer Vision 原文出处: http://lincccc.blogspot.tw/2011/04/graph-cut-and-its-application-in.html 网络流算法最初用于解决流网络的优化问题,比如水管网络.通信传输和城市的车流等.Graph cut作为其中一类最常见的算法,用于求解流网络的最小割,即寻找一个总容量最小的边集合,去掉这个集合中的所有边将阻断这个网络.图像和视频也能被视作网络(或者

[新书推荐]A Practical Introduction to Computer Vision with OpenCV

一本opencv好书,  在我上传的资源里 http://download.csdn.net/detail/qq_21970857/8504829 Computer Vision is a rapidly expanding area and it is becomingprogressively easier for developers to make use of this field dueto the ready availability of high quality librari

Computer Vision: OpenCV, Feature Tracking, and Beyond--From &lt;&lt;Make Things See&gt;&gt; by Greg

In the 1960s, the legendary Stanford artificial intelligence pioneer, John McCarthy, famously gave a graduate student the job of “solving” computer vision as a summer project. It has occupied an entire community of academic researchers for the past 4

翻译:Mastering OpenCV with Practical Computer Vision Projects(第8章)(二)

/******************************************************************************************************************************************************************************************翻译:Mastering 请接着上一篇:OpenCV with Practical Computer Vision Proje

Computer Vision Algorithm Implementations

Participate in Reproducible Research General Image Processing OpenCV (C/C++ code, BSD lic) Image manipulation, matrix manipulation, transforms Torch3Vision (C/C++ code, BSD lic) Basic image processing, matrix manipulation and feature extraction algor

[iOS翻译]《iOS 7 Programming Cookbook》:iOS文件与文件夹管理(上)

简介: iOS基于OS X,而OSX本身基于Unix操作系统.在iOS里面,操作系统的完全路径结构是不可见的,因为每个APP的数据都存储自身的沙盒里面.沙盒环境实际上听起来像这样:一个只允许当前APP访问的文件夹目录.每个APP都有自身的沙盒文件夹,并且沙盒文件夹下的子文件夹只有当前APP能够访问. 当一个iOS APP在设备上安装后,系统为其创建的文件夹结构如下: XXX.app 即Main Bundle Documents/ 存储用户创建的内容 Library/ 存储缓存文件.偏好设置等等

Computer Vision 学习 -- 图像存储格式

本文把自己理解的图像存储格式总结一下. 计算机中的数据,都是二进制的,所以图片也不例外. 这是opencv文档的描述,具体在代码里面,使用矩阵来进行存储. 类似下图是(BGR格式): 图片的最小单位是像素,这里是BGR(通常我们说的blud.green.red的表示法)表示每个像素对应的值(这里BGR的混合,可以得到我们可见光的所有值). 如果是单通道(例如:灰度化之后的图像,这里就只有一列) 参考可见光光谱: 因为物体都是原子组成,原子都在运动,运动会产生光波,不同的物体生成的光波不一样,人类