Reputation: 4669
I've seen similar questions asked but the answers were for the old C API.
I have a colour image loaded with cv::Mat img = cv::imread("C:/some_image.jpg");
Normally, I access elements in a grayscale image with img.at<float>(row, col)
, but this would clearly just return a float. How do I get a value (perhaps a float? integer?) for each component R, G, B at each pixel location?
Upvotes: 0
Views: 296
Reputation: 1
You can do like this:
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
}
}
Upvotes: 0
Reputation: 3062
One way to do it is just as you have, but the three channels will be stored in a three-dimensional vector rather than the one channel float.
cv::Mat img = ...;
cv::Vec3f pixel = img.at<cv::Vec3f>(row, col);
// pixel contains [red, green, blue] values
Upvotes: 1