Mat中不管是以at访问还是ptr访问,都是行优先 ,先Y轴后X轴(即先行后列)
1、使用at访问
/*
*OpenCV2中Mat的at操作访问矩阵元素
*
*/
#include <highgui.h>
using namespace std ;
using namespace cv ;
int main()
{
Mat image = imread("forest.jpg") ;
imshow("image" , image) ;
//三通道图像,at(y , x)索引是先行(y轴) , 后列(x轴)
//第一种方法
for(int h = 0 ; h < image.rows ; ++ h)
{
for(int w = 0 ; w < image.cols / 2 ; ++ w)
{
image.at<Vec3b>(h , w)[0] = 255 ;
image.at<Vec3b>(h , w)[1] = 0 ;
image.at<Vec3b>(h , w)[2] = 0 ;
}
}
imshow("color1" , image) ;
//方法二
for(int h = 0 ; h < image.rows ; ++ h)
{
for(int w = 0 ; w < image.cols / 2 ; ++ w)
{
Vec3b &bgr = image.at<Vec3b&g