Remove irregular rounded corners from scanned images

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
michelgalibert
Posts: 1
Joined: 2017-10-05T02:58:17-07:00
Authentication code: 1151

Remove irregular rounded corners from scanned images

Post by michelgalibert »

Hello,

I'm quite new to ImageMagick. I have scanned thousands of old slides. On some of them, I have rounded corners I'd like to automatically remove.
The position of the corners is not always the same so applying exactly the same inner cropping would not work.

I would like to detect automatically and crop only the red area, while respecting the ratio of the picture.
https://cl.ly/222C1b3w042R
Image

Here are some original pictures if needed for testing:
https://cl.ly/2u2F2Q3L3P2l
https://cl.ly/3b3Z3s3w3442
https://cl.ly/3C3R0n1s0D0O
https://cl.ly/0p1B401l112k

Do you know how to do this?

Many thanks for your help,

Michel
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Remove irregular rounded corners from scanned images

Post by snibgo »

Please say what version of IM you use, on what platform (eg Windows, Unix, etc) and your preferred script language (Windows BAT, bash, etc) if any.

The first image has a "CloudApp" logo bottom-left, obscuring that corner. Has that been added by your hoster? If so, I suggest you upload them to somewhere that doesn't alter your images. For example, put them in zips that you upload to dropbox.com or somewhere.

Doing the crop is easy, with "-crop" or "-shave". Detecting whether it is needed, and how much, is more difficult. The photographed subject may be very dark in the corners, leading to false readings.

A possible approach: from each corner, examine the line at 45 degrees, towards the centre of the image. As we examine pixels from the corner inwards, if there is a jump from almost-black to something lighter, that marks a point for a crop. If all four corners have a jump at a similar distance, use the inner-most as a crop for all four.

(This could be done by rotating the image by 45 degrees, and taking horizontal or vertical crops.)

If two adjacent corners have a jump but the other two don't, do you care what happens? We could assume the other two have a jump in the same places.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove irregular rounded corners from scanned images

Post by fmw42 »

I have just add a new unix bash shell script, trim corners, to my collection that should do what you want above.

It uses a similar approach as suggested by snibgo to search along the diagonals of the image.
Post Reply