0

I Have a specific image: enter image description here

In this picture I try to detect the following lines reliable: enter image description here

I tired to detect them with the following code:

def find_and_draw_lines(img):
    # convert to grayscale
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Apply Canny edge detection to find edges
    edges = cv2.Canny(img, 50, 150, apertureSize=3)

    
    # Apply Hough Transform to find lines
    lines = cv2.HoughLinesP(edges, rho=1, theta=np.pi/180, threshold=50, minLineLength=30, maxLineGap=10)
    
    img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
    print(f"Number of lines found in image: {len(lines)}\n")
    # Draw red lines over the original image
    for line in lines:
        x1, y1, x2, y2 = line[0]
        cv2.line(img, (x1,y1), (x2,y2), (0,0,255), 2)
    
    # Display the image
    cv2.imshow("Image with lines", img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

But this code just does not detect the right lines. Most of the lines detected are lines of the ovals(output of the line detection): enter image description here

Is there a way to just detect the marked lines?

More Information on the core Problem onhand:

So I have a Part that should get picked up by a roboter arm. I already managed to locate the part on a workbench, but I still need to know if the Part is orientet face up or face down, to know if I need to turn the part around or not. For this I want to use a webcam and a script of some sort. The script should determine if the part is face up or not.

The big problem is, that the part looks really similar from the top and bottom, so I tried playing around with the angel of the camera to see the differences. With my own eyes I can say confident if the part is face up or not, but with a script it is a bit harder...

I thought this could be done with analyzing the amount of black pixels or with the amount of lines in the image. But so far non have worked.

I could also try to train a custom object detection with yolo or tensorflow, but I think this is a bit overkill...

Here is a image I can provide from the original parts. One is face up and one face down: enter image description here

  • source data please, no filters. -- mentally prepare to discard the entire approach. – Christoph Rackwitz Mar 04 '23 at 13:38
  • we need to discuss the camera angle. that angle may not be best. present a broad view of the problem. your problem isn't finding lines. it's telling the orientation of some "object", of which (and the entire situation) I'd like to know more about. -- related: https://stackoverflow.com/questions/75602036/find-custom-changing-image-in-other-image – Christoph Rackwitz Mar 04 '23 at 13:43
  • ok, yes indeed I want to determine the orientation of the object. How can I provide you with more information? Open a new question or expand this one? – Moritz Pfennig Mar 04 '23 at 16:21
  • I believe I asked for unfiltered source data (pics). that would help. you could also be more specific than saying "object". it's not any object, it's something specific. I could guess at what I'm seeing but you should be volunteering those "details" (crucial information), and not just those details I'm specifically asking about, but _other_ details nobody asked about – Christoph Rackwitz Mar 04 '23 at 22:21
  • Ok, I tried to give some more context to the problem – Moritz Pfennig Mar 05 '23 at 12:00
  • ok so lighting is an issue. the table is reflective, lighting is uneven, I see inconsistent shadows. I'd suggest a matte translucent surface lit from the bottom. let's make that a strong "recommendation", and anything else will simply *not work* or be way too much of a hassle to _make_ work. when you have that, pictures will have the object look dark and the background look bright. that is easy to threshold. now you can throw contours at the problem. consider the interior contour's "convexity defects". those gaps between the fingers show more prominently depending on orientation. – Christoph Rackwitz Mar 05 '23 at 12:20
  • or you could train a neural network. classification is good enough. you don't need detection/localization. – Christoph Rackwitz Mar 05 '23 at 12:21
  • Ok, I will try to train a image classification model! I will get back here if I can show progress – Moritz Pfennig Mar 05 '23 at 15:44
  • to collect training data, I'd recommend a simple little program that shows the video feed, and upon pressing one of two keys, stores the current picture with the right label for the pressed key. think about expected variations. also consider augmentation in those ways that the scene/picture can vary. – Christoph Rackwitz Mar 05 '23 at 19:22

0 Answers0