gpt4 book ai didi

c++ - 在 openCV 中循环使用带有掩码的图像

转载 作者:太空宇宙 更新时间:2023-11-03 22:34:50 24 4
gpt4 key购买 nike

我需要从图像中收集一些数据。循环应该用掩码完成。

例如,我有一个简单的十字掩码:

  1  
1 1 1
1

并且我需要知道图像每个点的灰度值之和。

我可以使用简单的循环,像这样:

// looping except first and last
int nr = image.rows-1;
int nc = image.cols-1;

for (int j=1; j<nr; j++) { // for all rows

const uchar* previous = image.ptr<const uchar>(j-1); // previous row
const uchar* current = image.ptr<const uchar>(j); // current row
const uchar* next = image.ptr<const uchar>(j+1); // next row

for (int i=1; i<nc; i++) {
sum = previos[i] + current[i] + current[i-1]
+ current[i+1] + next[i];

}
}

但我认为我做错了。也许我应该使用像 cv::Mat kernel() 这样的东西?

我需要掩码作为参数,所以我可以使用任何类型的掩码。

是否有现成的函数可以使用蒙版循环播放图像? (有 filter2D 功能,但我不需要对图像进行更改,只需对像素进行一些计算)。

最佳答案

如果您想要每个像素的总和,这不正是 filter2d() 所做的吗?您计算每个像素的总和,然后使用这些总和继续处理 SUSAN:(未经测试的代码)

cv::Mat img;
// TODO: load img
cv::Mat kernel = cv::Mat::ones(3,3,CV_8U);
// TODO: set some elements to zero you don't like
cv::Mat sums = img.clone();
cv::filter2d(img, sums, -1, kernel);
// TODO: use sums for further computation

图像边缘发生的情况取决于您为 filter2d 指定的边界外推类型。 From the docs :

Another common feature of the functions and classes described in this section is that, unlike simple arithmetic functions, they need to extrapolate values of some non-existing pixels. For example, if you want to smooth an image using a Gaussian 3 \times 3 filter, then, when processing the left-most pixels in each row, you need pixels to the left of them, that is, outside of the image. You can let these pixels be the same as the left-most image pixels (“replicated border” extrapolation method), or assume that all the non-existing pixels are zeros (“constant border” extrapolation method), and so on. OpenCV enables you to specify the extrapolation method. For details, see the function borderInterpolate() and discussion of the borderType parameter in the section and various functions below.

/*
Various border types, image boundaries are denoted with '|'

* BORDER_REPLICATE: aaaaaa|abcdefgh|hhhhhhh
* BORDER_REFLECT: fedcba|abcdefgh|hgfedcb
* BORDER_REFLECT_101: gfedcb|abcdefgh|gfedcba
* BORDER_WRAP: cdefgh|abcdefgh|abcdefg
* BORDER_CONSTANT: iiiiii|abcdefgh|iiiiiii with some specified 'i'
*/

关于c++ - 在 openCV 中循环使用带有掩码的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10033243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com