# Thread: Image processing (math) question

1. ## Image processing (math) question

Given a binary image such as the attached figure, how can I split the white pixels into 2 subsets, the inner and the outer ones? In other words, how do I determine if a white pixel is entirely surrounded by red pixels?

Rather than code what I need is the guidelines, a method or algorithm.

2. ## Re: Image processing (math) question

Well, I think I could examine the pixels in a row to the right and left.

But now a related question that seems to be more difficult.

The image could be like the one above or like this one:

How can I find out (by code) if the red area closes on itself forming a central white "lake"?

3. ## Re: Image processing (math) question

Hmm, the only thing that comes to mind is using the
ExtFloodFill call set a color different from White or Red
and then testing, say, the upper left corner and the
center of the circle for the flooded color.

4. ## Re: Image processing (math) question

Have a look at "morphological image processing"

http://www.dspguide.com/ch25/4.htm

5. ## Re: Image processing (math) question

Pratically identical to my question
LooK at post 21 and 23

6. ## Re: Image processing (math) question

Originally Posted by namrekka
Have a look at "morphological image processing"

http://www.dspguide.com/ch25/4.htm
Thanks for this link. The book is incredible. I came across it quite a while ago but had forgotten about it. I may have done the same this time around, but the chapter section you linked to gave me some great ideas for something I am working on.

Now complex algorithms take a lot of time to write, debug, maintain and so forth. This would not be what I would do in the case of the OP requirements. Instead I favor the idea in post #3, which is exactly what I suggested in the thread reexre referred to in post #5.

7. ## Re: Image processing (math) question

Originally Posted by namrekka
Have a look at "morphological image processing"

http://www.dspguide.com/ch25/4.htm
Interesting book but I don't see how this chapter is going to help me.

The question is, given an image like either of the above, how can I detect whether it has an inner group of white pixels? It could be a lake or a bay.

8. ## Re: Image processing (math) question

Originally Posted by krtxmrtz
Interesting book but I don't see how this chapter is going to help me.

The question is, given an image like either of the above, how can I detect whether it has an inner group of white pixels? It could be a lake or a bay.
So, you don't need any Pixel-Manipulation on the Input-Image - right?

Still I'm wondering what you're really after ...
- is it just a function, which only returns a Boolean, when there's "some outer thing which encloses some inner thing"
- or is it a function, which determines whether the Image contains something that either encloses "a Lake" OR "a Bay" (e.g. returning an Enum-Value).

In other words:
- do you need to determine only, whether something is "quite curved"
- or do you need to determine between "something is quite curved and open" (like a C) OR "curved and closed" (like an O)

Olaf

9. ## Re: Image processing (math) question

"Morphological Image Processing" will be not a single layer solution for you.

Suppose you make a mask of 3*3 pixels:

123
456
789

You put this some where on your image on a point where a pixel exists with 5 as center of the mask.
Suppose the mask is filled with "1" on all positions. You compare (AND) the positions (except 5) with the underlaying image and count the hits.
As the counter returns 0 there are no connecting pixels. This can be used for removing single unwanted pixels. If the counter returns 1 its a end of a line.
In this case you can grow your image2 till its closed. Also if you change the mask you can detect directions and grow in certain directions.

Erosion, dilation, opening and closing are common algo's depending the mask.

https://en.wikipedia.org/wiki/Mathematical_morphology

In your case you need some layers after each other.

10. ## Re: Image processing (math) question

Let's make it so we have on a form an array of 4 vertically stacked command buttons and a picturebox on the right side of those buttons.

Let's say also we have those same picture samples as posted above, but saved as BMP instead of PNG.

Finally, let's assume the center of the picture box is a white pixel. This condition is included here for code simplicity, the alternative being that you would have to scan the pixels of the whole picture to find a white one for the test to be positive.

Code:
```Private Declare Function ExtFloodFill Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal bcolor As Long, ByVal bstyle As Long) As Long

Picture1.Appearance = 0
Picture1.BorderStyle = 0
Picture1.AutoSize = True
Picture1.ScaleMode = vbPixels
Picture1.AutoRedraw = True
Picture1.FillStyle = vbFSSolid
Picture1.FillColor = vbYellow

Command1(2).Caption = "Floodfill"
Command1(3).Caption = "Test"
End Sub

Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 0: Picture1.Picture = LoadPicture(App.Path & "\lake.bmp")
Case 1: Picture1.Picture = LoadPicture(App.Path & "\bay.bmp")
Case 2: ExtFloodFill Picture1.hdc, 3, 3, vbWhite, 1: Picture1.Refresh
Case 3: pcolor = Picture1.Point(Picture1.ScaleWidth / 2, Picture1.ScaleHeight / 2)
MsgBox IIf(pcolor = vbWhite, "Lake", "Bay")
End Select

End Sub```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured