Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello,
here i am working on C# WinForms desktop application where i have to do some image processing on the images. there is the lines between an object that showing in the image.
here i am adding some reference images in links.
image1
image2
image3
image4
image5
image6

What I have tried:

i am doing some image processing using EmguCV library for C#. i have not getting the output i want and it supposed to be.
here are some images of output i am getting.

see all the output images. there are in many images are not detecting the lines on the object and some images are get line but half. i want to detect whole black line on the object are. if there's black line of 40px then the draw line at least 90% on the detected black line.

output1
output2
output3
output4
output5


the code i have tried is given below.


if (img_path != null)
           {

               try
               {
                   Image<Bgr, byte> imgInput = new Image<Bgr, byte>(img_path);
                   // Convert the image to grayscale
                   Image<Gray, byte> gray = imgInput.Convert<Gray, byte>();

                   // Apply a threshold
                   gray = gray.ThresholdBinary(new Gray(100), new Gray(255));

                   // Detect edges
                   Image<Gray, byte> edges = gray.Canny(100, 50);

                   // Find contours
                   VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
                   Mat hierarchy = new Mat();
                   CvInvoke.FindContours(edges, contours, hierarchy, RetrType.List, ChainApproxMethod.ChainApproxSimple);

                   // Filter the contours
                   List<VectorOfPoint> filteredContours = new List<VectorOfPoint>();
                   for (int i = 0; i < contours.Size; i++)
                   {
                       VectorOfPoint contour = contours[i];
                       double area = CvInvoke.ContourArea(contour);
                       if (area > 500 && area < 10000) // adjust these values based on your image
                       {
                           filteredContours.Add(contour);
                       }
                   }

                   // Detect lines
                   LineSegment2D[] lines = CvInvoke.HoughLinesP(
                       edges, // input image
                       1, // rho
                       Math.PI / 180, // theta
                       100, // threshold
                       3, // minLineLength
                       10 // maxLineGap
                   );

                   // Draw the lines
                   foreach (LineSegment2D line in lines)
                   {
                       imgInput.Draw(line, new Bgr(Color.Red), 2);
                   }



                   // Display the result
                   pictureBox2.Image = imgInput.ToBitmap();

               }
               catch (Exception ex)
               {
                   MessageBox.Show(ex.Message);
               }
           }
Posted
Updated 26-Apr-23 23:48pm
v2
Comments
Richard MacCutchan 27-Apr-23 3:38am    
"i have not getting the output i want and it supposed to be."
Maybe you should explain what it should be.
sahil ajmeri 2022 27-Apr-23 5:47am    
see all the output images. there are in many images are not detecting the lines on the object and some images are get line but half. i want to detect whole black line on the object are. if there's black line of 40px then the draw line at least 90% on the detected black line.
Hardik Baraiya 30-May-23 8:27am    
ha ho!!!
Tim the Gamer 8-Jul-23 8:17am    
This needs you to check if a contour is a horizontal straight line. Hire me for this, I can help you beat this but it's gonna cost you. Do you want to draw a rectangle over the detected horizontal line? Or you want to detecf both horizontal and vertical lines?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900