How to circle/highlight/rectangle certain regions

2019-09-01 03:21发布

问题:

I have a grayscale image with various regions that need to be circled in a WPF C# program. An example of such an image would be this:

The regions I'm looking to circle are the white ones, in this manner:

So far what I'm trying to do is make an array of bytes, consisting of the intensity of each pixel (0 being black and white being 4000 and upwards.) I then pass each byte through a threshold test to see if its a pixel with part of the region in it or not (for example, if bytes have lengths beyond 3500, consider it as part of the region, else it's not.)

I am stumped as what to do after this. So far one of my ideas is to find the first pixel of a region within a row, then find the last pixel of a region of row on the bottom of a region (so top left and bottom right.) Then I'd find the distance in between both pixels and use that to get the center point (from which I could use to get the coordinates needed to draw circle.) Issues with this method I think would be noise that would either cause many circles to appear on the image or misinformed circles. I guess I could use thresholds to eliminate small circles though.

These images are actually frames of a video (information is obtained as a byte array and then written to a WriteableBitmap) , so I don't know if too many manipulations with the data would lag the footage. I'm also a newbie with C#, bytes and CS in general, but this project is something I took up during my winter break to learn how to program. Any answers or resources would be much appreciated, especially in pseudocode or C#.

回答1:

From what you said it sounds like a Computer Vision problem to me. I used to develop couple of CV problem applications based on C#. Basic idea is using GDI+ to marshal Bitmap.Lockbits() object into Byte[] and do the manipulation, or simply use Bitmap.SetPixel() Bitmap.GetPixel(), but they are tolerably slower.

A great choice is to use EmguCV. A OpenCV wrapper for .NET 2.0 and up. Thus making writing C# to solve CV problem much eaiser.