opencv 访问像素不正确的坑

用opencv中三种访问像素的方法要,使用at访问单通道的图像,注意其顺序

img.at(x,y),访问的其实是图像中(y,x)点;

img.at(Point(x,y)),访问的才是我们希望的(x,y)点像素;

原因的oencv Mat中存储的像素点下标(0,1)分别是指的(y,x);

使用时一定要注意,按照常理就使用mg.at(Point(x,y))吧,用错了会出现像素点xy反掉了,浪费了一天时间,才从官网上查到这个,大家引以为鉴。

opencv官网原文(https://docs.opencv.org/2.4.13/doc/user_guide/ug_mat.html):

In order to get pixel intensity value, you have to know the type of an image and the number of channels. Here is an example for a single channel grey scale image (type 8UC1) and pixel coordinates x and y:

Scalar intensity = img.at<uchar>(y, x);

intensity.val[0] contains a value from 0 to 255. Note the ordering of x and y. Since in OpenCV images are represented by the same structure as matrices, we use the same convention for both cases - the 0-based row index (or y-coordinate) goes first and the 0-based column index (or x-coordinate) follows it. Alternatively, you can use the following notation:

Scalar intensity = img.at<uchar>(Point(x, y));

你可能感兴趣的:(opencv)