Forum to discuss AForge.NET Framework, its features, API, how-tos, etc.
by Requan » Sat Mar 03, 2012 5:23 pm
Dear All, I would like write program in c# to measure distance between two dark squares:  I read that i have to count pixels, so could You tell me: 1. How recognize end of dark area to start (stop) count pixels? 2. How to show arrow with distance? Best Regards, Martin
-
Requan
-
- Posts: 5
- Joined: Sat Mar 03, 2012 5:01 pm
by andrew.kirillov » Sun Mar 04, 2012 3:25 pm
Hello, Requan wrote:1. How recognize end of dark area to start (stop) count pixels?
In grayscale images, black pixels usually have value 0, but white pixels have value 255. So you may need write some code which searches for a white gap between black areas (255 values between 0 values). To simplify things, you may invert your image and then use BlobCounter from AForge.NET framework. Requan wrote:2. How to show arrow with distance?
Draw it. Use some drawing API.
-

andrew.kirillov
- Site Admin, AForge.NET Developer
-
- Posts: 2567
- Joined: Fri Jan 23, 2009 9:12 am
- Location: UK
by Requan » Sun Mar 04, 2012 6:50 pm
Hi Andrew; Thanks for help and sorry for my basic questions. My camera has 640x480 res, i converted on grayscale and counted blobs - blobs lenght showed = 90 - it is correct? One blob = one pixel? - Code: Select all
Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721); Bitmap grayImage = filter.Apply(image);
//pictureBox1.Image = grayImage; // create and configure blob counter BlobCounter blobCounter = new BlobCounter();
blobCounter.MinHeight = 1; blobCounter.MinWidth = 1; blobCounter.FilterBlobs = true; blobCounter.ObjectsOrder = ObjectsOrder.Size;
blobCounter.ProcessImage(grayImage); Blob[] blobs = blobCounter.GetObjectsInformation();
for (int i = 0, n = blobs.Length; i < n; i++) { }
To check value of blobs i will have to use: blobs.getvalue in FOR loop? Best Regards, Martin
-
Requan
-
- Posts: 5
- Joined: Sat Mar 03, 2012 5:01 pm
by andrew.kirillov » Sun Mar 04, 2012 8:35 pm
Where do you use invert filter? As you may find in documentation, BlobCounter can find white objects (none black) on black background. Background must be black. Everything else is treated as object (blob). Requan wrote:One blob = one pixel?
No. One blob = one object = collection of pixels which have some connection (8 directions are used for connection).
-

andrew.kirillov
- Site Admin, AForge.NET Developer
-
- Posts: 2567
- Joined: Fri Jan 23, 2009 9:12 am
- Location: UK
by Requan » Mon Mar 05, 2012 1:09 pm
Thanks for Your comments and patience. I appreciate it. Sorry i thought that grayscale is the same as invert. I corrected the code: - Code: Select all
Invert filter = new Invert(); // apply the filter filter.ApplyInPlace(image); BlobCounter blobCounter = new BlobCounter();
blobCounter.MinHeight = 32; blobCounter.MinWidth = 32; blobCounter.FilterBlobs = true; blobCounter.ObjectsOrder = ObjectsOrder.Size;
blobCounter.ProcessImage(image); Blob[] blobs = blobCounter.GetObjectsInformation();
for (int i = 0, n = blobs.Length; i < n; i++) {
}
1) Now background is black. On picture i have two squares and program detected 1 blobs - it is correct? I read documentation but i have no idea how put value of pixels to the list. In blobcounter we have methods: GetBlobsTopAndBottomEdges, GetBlobsLeftAndRightEdges and GetBlobsEdgePoints. 2)Which i should to use it? 3) How i use it? like this: List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i])? and check value in list? Best Regards, Martin
-
Requan
-
- Posts: 5
- Joined: Sat Mar 03, 2012 5:01 pm
by andrew.kirillov » Mon Mar 05, 2012 1:19 pm
Requan wrote:On picture i have two squares and program detected 1 blobs - it is correct?
No, it is not correct. You should get 2 blobs - one per each white square. If you got only one, then you may need to try using Threshold filter to make sure you have only black and white pixels. When you get 2 blobs, just check their coordinates (see Rectangle property of Blob). Using some trivial math you will get distance between squares from those coordinates.
-

andrew.kirillov
- Site Admin, AForge.NET Developer
-
- Posts: 2567
- Joined: Fri Jan 23, 2009 9:12 am
- Location: UK
by Requan » Tue Mar 06, 2012 11:51 am
Thanks Andrew. Now i detect object:  I also detect corners, but when i used rectangle properties and drow a line (please look at picture) program showed line in strange position. Where i made mistake? - Code: Select all
g.DrawLine(pen, blobs[0].Rectangle.X, blobs[0].Rectangle.Y, blobs[1].Rectangle.X, blobs[1].Rectangle.Y);
Could You advice how i should to do? maybe use GetBlobsLeftAndRightEdges, or measure distance between corners? Best Regards, Martin
-
Requan
-
- Posts: 5
- Joined: Sat Mar 03, 2012 5:01 pm
by Requan » Wed Mar 07, 2012 12:41 pm
Andrew, I know You don't have time to answer on my silly question, but please answer me only wy when i us higher resolution then 800x600 (eg full HD - my camera can take it) and convert image to bitmap grayscale etc i get image with offset position (it shifted form orginal) - Code: Select all
VideoCaptureDevice videoSource = form.VideoDevice; Size sz = new Size(); sz.Height = 1080; sz.Width = 1920; videoSource.DesiredFrameSize = sz;
-
Requan
-
- Posts: 5
- Joined: Sat Mar 03, 2012 5:01 pm
by andrew.kirillov » Wed Mar 07, 2012 1:37 pm
I don't know why your image is shifted. Grayscale filter is not supposed to shift anything. You can actually check it by examining initial color image and result grayscale image. Maybe your camera shifts something, when it is configured to capture in high resolution. Maybe there is something wrong in the way you draw the image. Do you draw it on your own? If so check all coordinates carefully.
-

andrew.kirillov
- Site Admin, AForge.NET Developer
-
- Posts: 2567
- Joined: Fri Jan 23, 2009 9:12 am
- Location: UK
Return to AForge.NET Framework
|

|