简体   繁体   中英

How to fill a binary matrix in opencv using C++

I want to fill a matrix dest from another matrix which named fgMask,so I write this program:

Mat dest;
dest.create(fgMask.size(),fgMask.type());
dest=cv::Scalar(0.0f);


for(int i=4;i<fgMask.rows-4;++i){
    for(int j=4;j<fgMask.cols-4;++j){
        bool white = true;
        for(int k=c.x;i<c.width;++k){
            for(int l=c.y;l<c.height;++l){
                if(fgMask.at<uchar>(k,l)>0){

                    white=false;
                }
                if(white){  
                    dest.at<uchar>(i,j)=255;
                }
                else
                {

                    dest.at<uchar>(i,j)=0;
                }                               
            }               
        }

    }       
}

return dest;

But when I debug it I got that :

 Mat dest;
(gdb) n
56      dest.create(fgMask.size(),fgMask.type());
(gdb) n
57      dest=cv::Scalar(0.0f);
(gdb) 
60      for(int i=4;i<fgMask.rows-4;++i){
(gdb) 
61          for(int j=4;j<fgMask.cols-4;++j){
(gdb) 
62              bool white = true;
(gdb) 
63              for(int k=c.x;i<c.width;++k){
(gdb) 
64                  for(int l=c.y;l<c.height;++l){
(gdb) 
63              for(int k=c.x;i<c.width;++k){
(gdb) 
64                  for(int l=c.y;l<c.height;++l){
(gdb) 
63              for(int k=c.x;i<c.width;++k){
(gdb) 
64                  for(int l=c.y;l<c.height;++l){
(gdb) 
63              for(int k=c.x;i<c.width;++k){
(gdb) 
64                  for(int l=c.y;l<c.height;++l){
(gdb) 
63              for(int k=c.x;i<c.width;++k){
(gdb) 
64                  for(int l=c.y;l<c.height;++l){
(gdb) 
63              for(int k=c.x;i<c.width;++k){
(gdb) 
64                  for(int l=c.y;l<c.height;++l){
(gdb) 
63              for(int k=c.x;i<c.width;++k){
(gdb) 
64                  for(int l=c.y;l<c.height;++l){
(gdb) 
63              for(int k=c.x;i<c.width;++k){
(gdb) 
64                  for(int l=c.y;l<c.height;++l){
(gdb) 
63              for(int k=c.x;i<c.width;++k){
(gdb) 
64                  for(int l=c.y;l<c.height;++l){
(gdb) 

it can't enter to the loop to fill the matrix.Can anyone know what is my mistakes?

I see 2 errors in your code:

There is one here ;i<c.width; . it should be ;k<c.width;

and another error here fgMask.at<uchar>(k,l)

k refers to the width, so in a way of speaking is the columns, and l refers to the rows, so it should be:

fgMask.at<uchar>(l,k)

also, you should give some more info, like how is cv::Rect c intialized ? it could be anything...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM