Ask Your Question
0

Assigning uchar pixel going wrong

asked 2016-02-05 07:01:10 -0600

goldroses gravatar image

updated 2016-02-05 08:10:14 -0600

I want to set a pixel which meets certain criteria to 4096 (2^12), but what I end up with is 14; Its an cv::Mat dImg in the format 16UC1

        for(int j=0;j<dImg.rows;j++) 
        {
        uchar* pixrow = dImg.ptr<uchar>(j); 

        for (int i=0;i<dImg.cols;i++)
        {
            if(j == 240 && pixrow[i] ==0)  dImg.at<uchar>(j,i) = 2^12;

also this results in cout=14:

                if(j == 240 && pixrow[i] ==0)  pixrow[i]  = 2^12; //= 4096

I check the value with

 cout<<"\nmodified: "<<endl;
for(int i = 0; i<dImg.cols;i++){
  cout<<" "<<unsigned(dImg.at<uchar>(240,i));
}

SOLUTION (thank your for answering)

 for(int j=0;j<dImg.rows;j++) 
    {
        ushort* pixrow = dImg.ptr<ushort>(j);  //not uchar

        for (int i=0;i<dImg.cols;i++)
        {
            if(j == 239 && pixrow[i] ==0)  pixrow[i]  = 4096;
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-02-05 07:39:20 -0600

uchar is an 8 bit step but you have a 16 bit image ... so you would need at dImg.at<ushort>(240,i) to get the correct value. Also you are not considering that we start counting at index 0, so substract 1 position of that index!

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-02-05 07:01:10 -0600

Seen: 163 times

Last updated: Feb 05 '16