OpenCV 像素访问指针与 at() - 不同的值
OpenCV pixel access pointr vs. at() - Different values
我在尝试访问像素时遇到了奇怪的行为,如下所示:
void Dbscan::regionQuery(int i, int j, std::vector<Point>* res) const {
// check rect. grid around center point
const size_t row_min = std::max(0, i-eps_);
const size_t row_max = std::min(n_rows_, i+eps_+1);
const size_t col_min = std::max(0, j-eps_);
const size_t col_max = std::min(n_cols_, j+eps_+1);
assert(masked_img_.depth() == CV_8UC1);
for (int m = row_min; m<row_max; ++m) {
const uchar* mask_ptr = masked_img_.ptr(m);
for (int n = col_min; n<col_max; ++n) {
assert(*mask_ptr == masked_img_.at<uchar>(m, n));
if (masked_img_.at<uchar>(m, n) == 255) {
res->emplace_back(Point(m,n));
}
mask_ptr++;
}
}
基本上,如图所示的第二个断言失败了,我对发生了什么一无所知。有谁知道如何最好地解决上述问题?
此致问候
菲利克斯
cv::Mat::ptr
returns a pointer to the beginning of the row from the argument, which is an address of an element in the first column of this row. cv::Mat::at
returns 对参数中行和列中元素的引用。在您的代码中,行匹配,但列不匹配(除非您的 col_min
评估为 0),因此您需要将指针从 cv::Mat::ptr
n
元素移动以匹配您的列嗯:
for (int m = row_min; m<row_max; ++m) {
const uchar* mask_ptr = masked_img_.ptr(m);
for (int n = col_min; n<col_max; ++n) {
assert(*(mask_ptr + n) == masked_img_.at<uchar>(m, n));
if (masked_img_.at<uchar>(m, n) == 255) {
res->emplace_back(Point(m,n));
}
}
}
我在尝试访问像素时遇到了奇怪的行为,如下所示:
void Dbscan::regionQuery(int i, int j, std::vector<Point>* res) const {
// check rect. grid around center point
const size_t row_min = std::max(0, i-eps_);
const size_t row_max = std::min(n_rows_, i+eps_+1);
const size_t col_min = std::max(0, j-eps_);
const size_t col_max = std::min(n_cols_, j+eps_+1);
assert(masked_img_.depth() == CV_8UC1);
for (int m = row_min; m<row_max; ++m) {
const uchar* mask_ptr = masked_img_.ptr(m);
for (int n = col_min; n<col_max; ++n) {
assert(*mask_ptr == masked_img_.at<uchar>(m, n));
if (masked_img_.at<uchar>(m, n) == 255) {
res->emplace_back(Point(m,n));
}
mask_ptr++;
}
}
基本上,如图所示的第二个断言失败了,我对发生了什么一无所知。有谁知道如何最好地解决上述问题?
此致问候
菲利克斯
cv::Mat::ptr
returns a pointer to the beginning of the row from the argument, which is an address of an element in the first column of this row. cv::Mat::at
returns 对参数中行和列中元素的引用。在您的代码中,行匹配,但列不匹配(除非您的 col_min
评估为 0),因此您需要将指针从 cv::Mat::ptr
n
元素移动以匹配您的列嗯:
for (int m = row_min; m<row_max; ++m) {
const uchar* mask_ptr = masked_img_.ptr(m);
for (int n = col_min; n<col_max; ++n) {
assert(*(mask_ptr + n) == masked_img_.at<uchar>(m, n));
if (masked_img_.at<uchar>(m, n) == 255) {
res->emplace_back(Point(m,n));
}
}
}