I assume ImageMagick can do this, but I have no leads in where to start.
I have this sample 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:
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).
Removing saturated blocks of rows under pixel height
-
- 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
Windows BAT syntax:
EDIT: As always, there are alternatives. Here's a faster and more obvious replacement for that middle step:
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
Code: Select all
%IM%convert x.png -morphology close rectangle:1x30 -scale "232x216^!" x2.png
snibgo's IM pages: im.snibgo.com
Re: Removing saturated blocks of rows under pixel height
Yeah, I tried this last night and found the following to work well across multiple samples: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
Code: Select all
convert input ^( +clone -scale "1x^!" -threshold 95%% -morphology close rectangle:1x55 -scale "232x216^!" ^) -compose Lighten -composite output
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!