1 Attachment(s)
[RESOLVED] Add border around a single-color region in a bitmap
Hello everyone ! :wave:
I'm looking for a code/algorithm that will help me to draw border around different color regions inside a bitmap.
I have a bitmap like the "before" image.
I want to add border around the colored regions like the "after" image.
Any idea ?
VS Version: 2005-2010 (Doesn't matter)
Language: VB.Net / C# / C++ (Doesn't matter as long as it can be called from a managed exe)
Re: Add border around a single-color region in a bitmap
Is the border drawn on the color, or outside of the color? If it is drawn on the color, one routine would be this:
1) Find a pixel of the color
2) Check all pixels adjacent to that pixel to see if they are also of the color.
3) If any adjacent pixel is a different color other than the border color, then change the color of the current pixel to the border color.
A very similar routine would also work for the case where you would draw the border not on the pixels:
1) Find a pixel of the color
2) Check all pixels adjacent to that pixel to see if they are also of the color.
3) If any adjacent pixel is a different color other than the border color, then change the color of that pixel to the border color.
Technically, either one should work for the entire bitmap, but there will be a whole lot of worthless and costly checks to make along the way. After all, it will check all pixels in the interior of the image. Therefore, you could probably improve on the algorithm by doing something like this:
1) Scan the image pixel by pixel, row by row, looking for the first pixel of the target color.
2) Perform the algorithm on that pixel.
3) Look for a pixel of the target color adjacent to the current pixel which has an adjacent pixel of a different color (other than the border color).
4) Goto 2
That won't cover all situations, but would be faster as long as the region is contiguous and has no doughnut holes. Far fewer total pixels would be examined using that variation.
Re: Add border around a single-color region in a bitmap
I would simply scan the image scanline by scanline testing every pixel that has an adjacent pixel of a different color from itself and where this is true, I would set that pixel to black. There are other minor issues with this but this is the general idea I'd use.
Re: Add border around a single-color region in a bitmap
Thanks for your reply.
My idea was similar. I was searching for a "edge finding" algorithm. But the real picture is huge and takes too much time.
This is a Map projection software. Before generating the map, I have calculated complete list of x,y and color value of pixels and loaded into memory (and saved into a database for future use).
I'll try your idea and let you know how it goes.
Thanks. :)
Re: Add border around a single-color region in a bitmap
Quote:
Originally Posted by
iPrank
Thanks for your reply.
My idea was similar. I was searching for a "edge finding" algorithm. But the real picture is huge and takes too much time.
This is a Map projection software. Before generating the map, I have calculated complete list of x,y and color value of pixels and loaded into memory (and saved into a database for future use).
I'll try your idea and let you know how it goes.
Thanks. :)
Hello iPrank.. sorry for my late post..
I have a problem similiarly as you.. i want to draw a border into the region of map.. but still not have a good solution..
have you solved this.. ? can you share this to me.. ?
Thanx before..
Re: [RESOLVED] Add border around a single-color region in a bitmap
I used Aforge Image Processing lab for that.
http://www.aforgenet.com/projects/iplab/
Here is how I did this:
1. First I applied a "Edge" filter and got a new image.
2. Then I make that image's background transparent.
3. Finally draw this new bitmap over original bitmap.
Following code segment may give you some idea.
Code:
Public Sub Render(ByVal SourceImage As System.Drawing.Bitmap, ByVal EdgeDetectionFilter As EnumEdgeDetectionFilter)
Dim bmp As Bitmap = Nothing
Select Case EdgeDetectionFilter
Case EnumEdgeDetectionFilter.Homogenity
bmp = EdgeFilterHomogenity(SourceImage)
Case EnumEdgeDetectionFilter.Difference
bmp = EdgeFilterDifference(SourceImage)
Case EnumEdgeDetectionFilter.Sobel
bmp = EdgeFilterSobel(SourceImage)
End Select
bmp.MakeTransparent(Color.Black)
DrawOverOriginalBitmap(SourceImage, bmp)
End Sub
Re: [RESOLVED] Add border around a single-color region in a bitmap
Quote:
Originally Posted by
iPrank
I used Aforge Image Processing lab for that.
http://www.aforgenet.com/projects/iplab/
Here is how I did this:
1. First I applied a "Edge" filter and got a new image.
2. Then I make that image's background transparent.
3. Finally draw this new bitmap over original bitmap.
Following code segment may give you some idea.
Code:
Public Sub Render(ByVal SourceImage As System.Drawing.Bitmap, ByVal EdgeDetectionFilter As EnumEdgeDetectionFilter)
Dim bmp As Bitmap = Nothing
Select Case EdgeDetectionFilter
Case EnumEdgeDetectionFilter.Homogenity
bmp = EdgeFilterHomogenity(SourceImage)
Case EnumEdgeDetectionFilter.Difference
bmp = EdgeFilterDifference(SourceImage)
Case EnumEdgeDetectionFilter.Sobel
bmp = EdgeFilterSobel(SourceImage)
End Select
bmp.MakeTransparent(Color.Black)
DrawOverOriginalBitmap(SourceImage, bmp)
End Sub
Hey... Thanx..
It's work for me..
I'd try to code to detect the edge of region by finding the different of color..
but i think your solution is better..
Great.. thx alot :) !
Re: [RESOLVED] Add border around a single-color region in a bitmap
Hi, you have a pure vb6 code of edges or vertices detection?
Re: [RESOLVED] Add border around a single-color region in a bitmap
Quote:
Originally Posted by
xman2000
Hi, you have a pure vb6 code of edges or vertices detection?
Try asking in the VB6 section. For the most part, the VB6 guys don't veer into the .Net section and vice versa. There are guys that post there like LaVolpe and Diletantte for example, that have done a lot of amazing image processing stuff in pure VB6 code.