0

Here's an image from the sample images of openCV: enter image description here

I would like to automatically draw rectangles around the black boxes.

My initial idea was to find contours and find those with the largest area, or see if they actually match a rectangle.

I have tried several filters (different combinations of canny, threshold with varying parameters) in conjunction with the findContours function. It seems the boxes are actually entirely missed by these methods.

Heres a pyplot of one of my attempts. As you can see, the boxes aren't even recognized as contours.

enter image description here

Is there some smarter way of doing this? I would think there should could be a function like matchShape or something where it would match a some preset shape.

I am researching possibilities of automatically aligning probes for a university project. The long term goal, after this example, is to make markings on the chip that I could recognize for alignment.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
iHnR
  • 125
  • 7
  • do you want to find the chips, and you assume they're black/dark gray and rectangular and not rotated... -- your picture's illumination isn't even. it's brighter in the top left. you need to address that. -- to find the chips, you should apply a median filter (suppress noise), then threshold the picture, and find contours, then filter those by area and number of corners when approxPolyDP'ed – Christoph Rackwitz Jun 22 '22 at 16:41
  • and you should review [ask]. your task is a type of automated optical inspection. there is tons of literature on all kinds of approaches to do that. you should look for some formal education on the topic. – Christoph Rackwitz Jun 22 '22 at 16:43
  • if you have a top-down picture of the PCB's top side, rendered from gerber files and/or the ECAD program, you could try feature matching to align the camera to the model picture. and you can extract points of interest from your model data, and match that up with the camera picture – Christoph Rackwitz Jun 22 '22 at 16:45

1 Answers1

0

Strongly recommend YOLOv5 based neural network approach. https://github.com/ultralytics/yolov5

Assuming you can procure about 50 - 100 training images with annotations, you can choose a lightweight model from the ones available in YOLOv5 and draw bounding boxes automatically with each new pictures that you pass into it.

The output from the neural network would be in form det = [[cls_id, x1,y1,x2,y2],[cls_id, x1,y1,x2,y2]]. You simply cycle through each of the elements in det list (which is another list), extract the (x1,y1) and (x2,y2) which are coordinates two points on one of the diagonal enclosing the region of interest (black boxes in your case). Then using cv2.drawRectangle() function, you should be able to achieve your desired result. Go over this post for more details How to draw a rectangle around a region of interest in python

Good luck.