设为首页 加入收藏

TOP

OpenCV2 访问各个像素点的方法
2014-11-24 11:22:42 来源: 作者: 【 】 浏览:0
Tags:OpenCV2 访问 各个 像素 方法

内容来自《OpenCV 2 Computer Vision Application Programming Cookbook》


OpenCV2 访问图像的各个像素有各种方法


我们来用各种方法来实现减少图像的颜色数量


color = color/div*div +div/2;


若div为8,则原来RGB每个通道的256种颜色减少为32种。


若div为64,则原来RGB每个通道的256种颜色减少为4种,此时三通道所有能表示的颜色有4×4×4 = 64 种



首先,我们来看一个函数


void colorReduce1(cv::Mat& image, cv::Mat& result, int div=64){
int nrow = image.rows;
int ncol = image.cols * image.channels();
for(int i=0; i<nrow; i++){
uchar
* data = image.ptr<uchar>(i);
uchar
* data_out = result.ptr<uchar>(i);
for(int j=0; j<ncol; j++){
data_out[j]
= data[j]/div*div +div/2;
}
}
}


第二种方案:


先来看如下函数:


C++: bool Mat::isContinuous() const


C++: Mat Mat::reshape(int cn, int rows=0) const


出于性能方面的考虑,在图像每一行的最后可能会填充一些像素,这样图像的数据就不是连续的了


我们可以用函数isContinuous()来判断图像的数据是否连续


reshape函数的作用如下:


Changes the shape and/or the number of channels of a 2D matrix without copying the data.



这样,我们就提出了对第一种方法的改进



】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇OpenCV2 图像叠加 给照片添加水印 下一篇OpenCV2 计算直方图

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·如何理解c语言指针和 (2025-12-27 01:19:11)
·为什么C标准库没有链 (2025-12-27 01:19:08)
·玩转C语言和数据结构 (2025-12-27 01:19:05)
·MySQL 基础入门视频 (2025-12-26 23:20:22)
·小白入门:MySQL超详 (2025-12-26 23:20:19)