rockinfresh
rockinfresh

Reputation: 2106

Taking R,G,B individually from a <cv::vec3b> vector

To make my question clearer, please see the codes.

vector<cv::Vec3b> v_char; // the vector that I am storing my r,g,b values, or b,g,r in this case since it's opencv.

I know to print out the value of use the values, I can just simply take from image directly, something like this.

b = image.at<cv::Vec3b>(i,j)[0];
g = image.at<cv::Vec3b>(i,j)[1];
r = image.at<cv::Vec3b>(i,j)[2];

However, without the use of the image(imagine image is gone), and I only want the values from the vector, v_char where all the image values have been stored, let's say for instance, I only want the r value, how do I retrieve it? Tried searching it up on the internet and playing around with the codes, but to no avail :(

EDIT: MORE DETAILS The vector, v_char is just storing the value of one row of the image. So for example: I want to find and print out all the r values of the particular row which can be found in the v_char vector. I can use a for loop, but how do I declare out the r values only?

Upvotes: 1

Views: 8148

Answers (2)

skm
skm

Reputation: 5679

Vec3b v_char itself is a vector of 3 values which can be accessed as first values = v_char.val[0], second values = v_char.val[1], third values = v_char.val[2]. As you need only the red values so, just access all the values in v_char.val[0] for the whole vector.

so through a for loop try to get all the values of:

v_char[i].val[0]; // "i" represents the index of "for loop" for a given row

Upvotes: 5

Cory Nelson
Cory Nelson

Reputation: 30031

It's difficult to know without knowing your data structure, but likely something like this.

Assuming j is the row index, i is the column index, and the stride of your rows is neatly packed.

auto &px = v_char[j * width + i];

Upvotes: 1

Related Questions