Any easier way to do this?

asked 2018-04-01 07:53:21 -0600

open_ranger gravatar image

I want to compare my labeled image to a vector of background labels.if they matches change that label to 255.Here is my code:

void markBackground(Mat input,vector backgroundLabel)
     for(int x=0;x<input.cols;x++)
        for(int y=0;y<input.rows;y++)
            for(int i=0;i<backgroundLabel.size();i++){
                if(input.at<uchar>(y,x)==backgroundLabel[i])input.at<int>(y,x)=255;            
        }

It involes 3 loops not very neat any other way to do this?

edit retag flag offensive close merge delete

Comments

how many labels/background labels are there ? are those ordered ?

if you could make it so (like: anything below 5 is bg), it would be an easy:

input.setTo(255, (input<5));

also:

input.at<int>(y,x)=255; // wrong type: int
berak gravatar imageberak ( 2018-04-01 08:21:47 -0600 )edit

about 70-50 labels 10-20 of them will be background the label number are pretty much scattered they can be sort in order but I dont see any neccessity.

open_ranger gravatar imageopen_ranger ( 2018-04-01 09:06:32 -0600 )edit

well, IF you can order the labels, the whole operation boils down to a simple thresholding.

else you could at least reorder it, like

        for(int i=0;i<backgroundLabel.size();i++){
            input.setTo(255, (input==backgroundLabel[i]));
        }
berak gravatar imageberak ( 2018-04-01 09:13:21 -0600 )edit

that looks good to me.gona give it a try.thank you.

open_ranger gravatar imageopen_ranger ( 2018-04-01 09:30:20 -0600 )edit

Also note that what you were doing with the .at<uchar> and .at<int> is not safe and could not work. Make sure you understand what those mean and why they wouldn't work before you use them elsewhere.

Tetragramm gravatar imageTetragramm ( 2018-04-02 16:48:19 -0600 )edit