Reputation: 1665
I am attempting to use OpenCV to grab frames from a webcam and convert it to aHSV(Hue,Saturation,Value) Mat object and threshold it.
When I print threshold image pixel values its giving me [0,0,0] for all the pixels, even black pixel values are also [0,0,0]. And I need to do some calculations if the selected pixel is black; how can I access the pixel values?.
imgOriginal=frame from camera
Mat imgHSV;
cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV
Mat imgThresholded;
inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded); //Threshold the image
//morphological opening (remove small objects from the foreground)
erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
dilate( imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
//morphological closing (fill small holes in the foreground)
dilate( imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
//************************************
std::vector<cv::Vec3b> pixels(imgThresholded.rows * imgThresholded.cols);
cv::Mat m(imgThresholded.rows, imgThresholded.cols, CV_8UC3, &pixels[0]);
imgThresholded.copyTo(m);
for(int i =0;i<1000;i++)
cout<<pixels[0];
if(pixels[0][0]==black)
// do some calculations!
Upvotes: 2
Views: 3842
Reputation: 11941
for(int i =0;i<1000;i++)
cout<<pixels[0];
will just print the first pixel 1000 times. I think you meant:
Vec3b black(0, 0, 0);
for(int i =0;i<1000;i++)
{
cout << pixels[i];
if pixels[i] == black)
{
/* ... */
}
}
But why bother copying the pixels to a std::vector? You could do this
Vec3b black(0, 0, 0);
Mat img(imgThresholded); // just to make a short name
for(int y = 0; y < img.rows; ++y)
{
Vec3b* row = img.ptr<Vec3b>(y);
for(int x = 0; x < img.cols; ++x)
{
Vec3b& pixel = row[x];
if(pixel == black)
{
/* ... */
}
}
}
Upvotes: 2