I'm trying to index a 3 channel image in opencv.
When I read in image files this code works
int Blue = LeftCol.at<cv::Vec3b>(v,u)[0];
int Green = LeftCol.at<cv::Vec3b>(v,u)[1];
int Red = LeftCol.at<cv::Vec3b>(v,u)[2];
But it crashes when I use a webcam input. The webcam has 3 channels and u,v
starts at 0,0
.
I have no idea why it won't work.
I've tried all variations of Vec3b
, Vec3i
, Vec3s
, Vec3f
, Vec3d
I'm lost.... why can't I index this webcam image?
EDIT
Right so after many hours this is where I've got to...here's an outline of the program. I was having the problem I mentioned above inside a function. So I've gone back to basic, trying to look at the matrix before the function...
void main (int argc, char** argv) {
Mat LeftCol;
while (1==1) {
if (ProgramMode == "Files") {
//read in the colour images
LeftCol = imread(ColImLeft.c_str(),1);
RightCol = imread(ColImRight.c_str(),1);
} else if (ProgramMode == "Camera") {
VideoCapture CapLeft, CapRight;
CapLeft.open(1);
CapRight.open(2);
CapLeft >> LeftCol;
CapRight >> RightCol;
//THIS WORKS, THIS PIXEL VALUES ARE DISPLAYED
cout << "uchar" << endl;
for (int x=0;x<10;x++) {
for (int y=0;y<10;y++) {
int pixel = LeftCol.at<cv::Vec3b>(x,y)[0];
cout << pixel;
}
cout << endl;
}
} //end if
///////ADDED THIS BIT ////////
cout << "channels = " << LeftCol.channels() << endl;
//^^This bit works, output shows "channels = 3"
//vv This bit doesn't work.... so there's a problem with LeftCol.
//I wonder if reading the data like CapLeft >> LeftCol; is changing something
imshow("Test",LeftCol);
///////ADDED THIS BIT ////////
//THIS DOES NOT WORK WHEN USING THE CAMERA INPUT, PROGRAM CRASHES
cout << "uchar" << endl;
for (int x=0;x<10;x++) {
for (int y=0;y<10;y++) {
int pixel = LeftCol.at<cv::Vec3b>(x,y)[0];
cout << pixel;
} //end for
cout << endl;
} //end for
} //end while
} //end main
Right I have got it working but it's not ideal. I'm creating a temp Mat
to read the files into it then cloning them.
Mat TempLeft;
Mat TempRight;
VideoCapture CapLeft, CapRight;
CapLeft.open(1);
CapRight.open(2);
CapLeft >> TempLeft;
CapRight >> TempRight;
LeftCol = TempLeft.clone();
RightCol = TempRight.clone();