opencv Mat.at


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() { return *this;}会生成一个临时对象变量,并把这个临时变量给你
这样就多了一步操作

当返回一个变量时,会产生拷贝。当返回一个引用时,不会发生拷贝,你可以将引用看作是一个变量的别名,就是其他的名字,引用和被引用的变量其实是一个东西,只是有了两个名字而已。

问题的关键是,当你想要返回一个引用而不是一个拷贝时,你要确保这个引用的有效性,比如:
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);

 

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM