如何访问加载的映像的各个 R/G/B 组件(较新的 C++ API)

How to access the individual R/G/B components of a loaded in image (newer C++ API)

本文关键字:组件 C++ API 访问 何访问 加载 映像      更新时间:2023-10-16

我见过类似的问题,但答案是针对旧的C API。

我有一个加载了cv::Mat img = cv::imread("C:/some_image.jpg");的彩色图像 通常,我使用 img.at<float>(row, col) 访问灰度图像中的元素,但这显然只会返回浮点数。 如何为每个像素位置的每个分量 R、G、B 获取一个值(也许是浮点数?整数?)?

一种方法就像你一样,但三个通道将存储在三维向量中,而不是一个通道浮点数。

cv::Mat img = ...;
cv::Vec3f pixel = img.at<cv::Vec3f>(row, col);
// pixel contains [red, green, blue] values

你可以这样做:

img_channel_=image.channels();
img_rows_=image.rows;
img_cols_=image.cols;
if (image.isContinuous() && fg_img_.isContinuous() && moving_img_.isContinuous() && abandon_img_.isContinuous()) {
    img_cols_ *=img_rows_;
    img_rows_=1;
}
img_cols_*=img_channel_;
for (int i=0; i<img_rows_; ++i) {
    const int iindex=i*img_cols_;
    const uchar *piex=image.ptr<uchar>(i);//three channel
    for (int j=0,truej=0; j<img_cols_; j+=img_channel_, ++truej) {
       piex[0]=pixe[1]+piex[2];//B=G+R
    }
}