-1

I have matlab code for some image registration algorithm. It is using imfill() for a binary image. I have to write c++ code for this algorithm without using any external libraries like openCV.

Please tell me how to implement imfill() function in c++

Below are input and output images respectively.

enter image description here

enter image description here

Gaurav
  • 75
  • 9
  • What have you tried? What are you having trouble with? Where have you looked? Do you already know some C++? How much of the function do you have to implement; the user mouse-input part, or just the image processing part? – Isaac Apr 01 '14 at 15:13
  • @Issac I want to fill the holes as explained in this [link] (http://stackoverflow.com/questions/1716274/fill-the-holes-in-opencv). Yes I know c++ – Gaurav Apr 01 '14 at 18:04
  • I also referred this [link](http://stackoverflow.com/questions/5941471/c-algorithm-for-flood-filling-a-binary-image) but I am not sure about includePoint() and setPoint() functions. – Gaurav Apr 01 '14 at 18:10
  • `includePoint()` is a criterion you would define to determine whether an adjacent pixel should be filled, and `setPoint()` is the operation that fills that pixel. – Isaac Apr 01 '14 at 22:27
  • @Isaac what is the criterion to determine whether an adjacent pixel should be filled or not? – Gaurav Apr 02 '14 at 02:40
  • 1
    That depends on what aspect of `imfill` you are trying to replicate. Please put more effort into making this question specific and constructive if you want to get a good answer. – Isaac Apr 02 '14 at 03:31
  • If you need more details please let me know. I am just using `imfill()` function of matlab. I have added images that I am getting without using `imfill` and with `imfill`. – Gaurav Apr 02 '14 at 04:00
  • Thanks for putting the effort of uploading some images to give us some sense of what you are trying to do. A good question will indeed get you a better answer. I suggest looking in to the answer to this question: http://stackoverflow.com/questions/5941471/c-algorithm-for-flood-filling-a-binary-image – pangyuteng Apr 02 '14 at 04:03
  • @teng Thanks for your reply. I have already checked this link. I want to implement this in C++ without using any external library like openCV. And I have some doubts regarding C++ code given. Please check previous comments of this post for details. – Gaurav Apr 02 '14 at 04:24

1 Answers1

0

So I started digging into Matlab's imfill code, but quickly found that the code it provides is just a wrapper for some compiled c or c++ code. For now I'll try to outline an algorithm that should get you what you want. The following algorithm assumes that the regions to be filled contain zero values in the original image.

  1. Identify contiguous regions of zeros, for instance by:
    1. creating a new (zero) mask image
    2. looping over the mask image, and when a position is found that is zero both in the original image and in the mask image, recursively assigning a value to this location in the mask, and to all neighbors who are zero in both the mask and the original. This results in a mask where each contiguous region of zeros in the original image has a unique ID.
  2. For each contiguous region of zeros, identify all other values that border this region in the original image, taking care to record when a region of zeros borders the edge of the image.
  3. For each contiguous region of zeros that borders only one value in the original image, and does not border the edge of the image, replace all of those zeros in the original image with the value that they border.

This algorithm is not necessarily efficient. In particular step 1 could probably see some speedup with intelligent use of queues to keep you from looking at the same position multiple times in the recursive calls.

Isaac
  • 3,586
  • 1
  • 18
  • 20