opencv c++ mat获取像素及像素赋值
For accessing pixel's channel value :
for (int i = 0; i < image.cols; i++) {
for (int j = 0; j < image.rows; j++) {
Vec3b intensity = image.at<Vec3b>(j, i);
for(int k = 0; k < image.channels(); k++) {
uchar col = intensity.val[k];
}
}
}
*For changing a pixel value of a channel :*
uchar pixValue;
for (int i = 0; i < image.cols; i++) {
for (int j = 0; j < image.rows; j++) {
Vec3b &intensity = image.at<Vec3b>(j, i);
for(int k = 0; k < image.channels(); k++) {
// calculate pixValue
intensity.val[k] = pixValue;
}
}
}
附录:引用
http://www.cppblog.com/gtwdaizi/articles/38521.html
C++返回引用类型 指针的引用
C++返回引用类型
A& a(){ return this;} 就生成了一个固定地址的指针,并把指针带给你
但A a() { returnthis;}会生成一个临时对象变量,并把这个临时变量给你
这样就多了一步操作
当返回一个变量时,会产生拷贝。当返回一个引用时,不会发生拷贝,你可以将引用看作是一个变量的别名,就是其他的名字,引用和被引用的变量其实是一个东西,只是有了两个名字而已。
问题的关键是,当你想要返回一个引用而不是一个拷贝时,你要确保这个引用的有效性,比如:
int & fun() { int a; a=10; return a; }
这样是不行的,因为a会在fun退出时被销毁,这时返回的a的引用是无效的。
这种情况下,如果fun的返回类型不是int & 而是int就没有问题了。
指针的引用
GetNearestFontInTwips(CFont &aFont, const TFontSpec &aFontSpec);
第一个参数aFont是一个指针, 前面加上&表示指针的引用, 其实可以如下看待这个方式(CFont*) &aFont, 这就一目了然了.
// image.at返回的是引用,将引用赋值到intensity
Vec3b intensity = image.at<Vec3b>(j, i);
//intensity是image.at返回的引用的另一个别名
Vec3b &intensity = image.at<Vec3b>(j, i);
原文链接: https://www.cnblogs.com/guoqiaojin/p/3204958.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/96819
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!