Removing saturated blocks of rows under pixel height

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
Masejoer
Posts: 23
Joined: 2016-06-23T11:34:37-07:00
Authentication code: 1151

Removing saturated blocks of rows under pixel height

Post by Masejoer »

I assume ImageMagick can do this, but I have no leads in where to start.

I have this sample image:
Image

I want to keep only the digits in the center of the image. Is there a way in IM, without writing things out to file, to remove the black bars at the top and bottom of the image? If I take the average grayscale value of each row, if fewer then X number of rows have a dark color, convert them to white. In other words, if a contiguous number of rows less than 30-pixels high have a darker average color, make the white. If there are more than 30 contiguous rows that have a darker average color, leave them intact.

Here is the output I'm going for:
Image

I know I can, as someone recommended in another thread for a different question I had, resize down to 1 column wide to get average grayscale values, and then use those values in a script to perform the logic externally, but processing that way was very slow due to only processing one line at a time with IM.

Tips or ideas? Thanks!

I'm running ImageMagick-7.0.1-6 Q16-x64 on Windows (.bat) and 7.0.2-5 Q16-x64 on Ubuntu (.sh).
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Removing saturated blocks of rows under pixel height

Post by snibgo »

Windows BAT syntax:

Code: Select all

%IM%convert image_step9.jpg -scale "1x^!" -threshold 90%% x.png

%IM%convert x.png -scale "1x7^!" -scale "1x216^!" -threshold 5%% -morphology erode rectangle:1x30 -scale "232x216^!" x2.png

%IM%convert image_step9.jpg x2.png -compose Lighten -composite x3.png
EDIT: As always, there are alternatives. Here's a faster and more obvious replacement for that middle step:

Code: Select all

%IM%convert x.png -morphology close rectangle:1x30 -scale "232x216^!" x2.png
snibgo's IM pages: im.snibgo.com
Masejoer
Posts: 23
Joined: 2016-06-23T11:34:37-07:00
Authentication code: 1151

Re: Removing saturated blocks of rows under pixel height

Post by Masejoer »

snibgo wrote: EDIT: As always, there are alternatives. Here's a faster and more obvious replacement for that middle step:

Code: Select all

%IM%convert x.png -morphology close rectangle:1x30 -scale "232x216^!" x2.png
Yeah, I tried this last night and found the following to work well across multiple samples:

Code: Select all

convert input ^( +clone -scale "1x^!" -threshold 95%% -morphology close rectangle:1x55 -scale "232x216^!" ^) -compose Lighten -composite output
with width and height variables set previously:

Code: Select all

convert input ^( +clone -scale "1x^!" -threshold 95%% -morphology close rectangle:1x55 -scale "!WW!x!HH!^!" ^) -compose Lighten -composite output

As always, thanks snibgo. I think I'm better understanding things in IM now!
Post Reply