Question

I have a binary image. and I want to fill some of its section completely black and remaining white. what i want can be best explained here-http://www.roborealm.com/tutorial/Obstacle_Avoidance/slide020.php explanation-I start from bottom right and go reach to top left. If i find any non-black pixel,all pixels having Y Coordinate less then it and having same X coordinate will become black.This works by starting at the bottom of the image and proceeding vertically pixel by pixel filling each empty black pixel until a non-black pixel is seen. The filling then stops that vertical column and proceeds with the next.

I wrote a code in opencv for it. I am mentioning code only for that particular section.

for (int j =dst.cols; j>=0; j--) {
    for (int i =dst.rows; i>=0; i--) {
        if (dst.at<char>(i,j) == 0){
            dst.at<char>(i,j)=255;
        }  
        if (dst.at<char>(i,j)>0){
            for (int k =i; k>=0; k--) {
               dst.at<char>(k,j)=0;
            }
        }   
    }
}

but its not working.but it showed segmentation fault(core dumped).

Was it helpful?

Solution 2

try this (not tested): (used different test and unsigned char instead of char

for (int j =dst.cols-1; j>=0; j--) 
{
    bool white = true;
    for (int i =dst.rows-1; i>=0; i--) 
    {
        if (dst.at<unsigned char>(i,j) > 0)
        {
            white = false;
        }  
        if(white)
            dst.at<unsigned char>(i,j)=255;
        else
            dst.at<unsigned char>(i,j)=0;

    }
}

edited: added a > 0 instead of == 255 to check whether the binary condition holds.

OTHER TIPS

Should be:

for (int j =dst.cols-1; j>=0; j--) {
for (int i =dst.rows-1; i>=0; i--) {

UPD: I think this should be faster:

#pragma once
#include <string>
#include <iostream>
#include <vector>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
//----------------------------------------------------------
// MAIN
//----------------------------------------------------------
int main(int argc, char* argv[])
{
    // src image
    Mat src;
    // dst image
    Mat dst;
    // Image loading
    namedWindow("result");
    namedWindow("src");
    src=imread("d:\\ImagesForTest\\obstacle_scene_1_edge.jpg",0);   
    dst=Mat::zeros(src.size(),CV_8UC1);
    for (int i=0;i<src.cols;++i)
    {
        int j=src.rows-1;
        for (j=src.rows-1;j>0;--j)
        {
            if(src.at<uchar>(j,i)>0)
            {           
                break;
            }
        }
        dst(Range(j,dst.rows-1),Range(i,i+1))=255;
    }
    imshow("src",src);
    imshow("result",dst);
    //----------------------------------------------------------
    // Wait key press
    //----------------------------------------------------------
    waitKey(0);
    destroyAllWindows();
    return 0;
}

i am referring the same paper/website. But I am using MATLAB.

%Loop function to fill image from bottom to top [rows, columns]=size(Binary Image);

for j=1:150; %number of columns for i=1:100; %Number of rows

%Find white pixels starting from (lastRow,firstColumns)

if Find_white_pixel=find(Binary Image==0,1,'first'); new_pixel=imfill (Binary Image,i,j)==0; %fill the pixels until non-lack pixels is seen

   else
       [rows, columns-1]=0;
          new_pixel=imfill (Binary Image,i++,j)==1;

end end end

But its error referring my coding for declare the row and column

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top