Search code examples
pythonimage-processingbounding-boxopencv

OpenCV Python: retrieve rectangle main area from image then crop image


I'm trying to isolate the main area art from Pokemon cards and crop them. For example,

Pokemon card

I want to extract the main area, as seen in the bounding box below.

Card with the main area highlighted

I want to use the OpenCV in Python for the task. I've tried out shape detection and corner detection, but I can't seem to make them work as intended, and they seem to pick up anything but the main area that I want to extract.

I can't hard-code the bounding box because I want to process many cards and the position of the main area is different per card.

What are the steps needed to extract the main area and save a png file of just the main area?


Solution

  • If the area of interest is always contained inside a somewhat contrasted quasi-rectangular frame you can try your luck with a Hough transform, keep the long horizontal and vertical edges (obtained separately) and try to reconstitute that frame.

    To begin, process a small number of cards and observe the results of Hough and figure out which systematic rules you could use to select the right segments by length/position/alignment/embedding in a larger frame...