Menghuadong
Menghuadong

Reputation: 35

OpenCV:split HSV image and scan through H channel

I meet a problem when I want to scan through H channel and print its pixel values after splitting a HSV image.The problem is that the outputs are not numbers but messy codes.

Following is my code(using Opencv):

Mat hsv;
cvtColor(saveImage,hsv,CV_BGR2HSV);// convert BRG to HSV
vector<cv::Mat> v_channel;
split(hsv,v_channel);          //split into three channels
if (v_channel[0].data==0)      //channel[0] is Hue
{
    cout<<"Error getting the Hue***********"<<endl;
}

for (int i=0;i<hue.rows;i++)     //scan through Hue 
{
    for (int j=0;j<hue.cols;j++)
    {
        cout<<v_channel[0].at<uchar>(i,j)<<endl;
    }
}

Hope anyone could help . Thanks very much!

Upvotes: 0

Views: 5855

Answers (1)

Martin Beckett
Martin Beckett

Reputation: 96109

The data is stored as bytes, ie chars, the output is interpreting the chars as, well, chars and trying to print the symbol. Simply tell it they are integers

cout<< (int) v_channel[0].at<uchar>(i,j)<<endl;

Upvotes: 4

Related Questions