Whiting out or trimming rows that have less than % of pixel color

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

Whiting out or trimming rows that have less than % of pixel color

Post by Masejoer »

I am continuing work on OCR pre-processing. I was wondering if there is a way for ImageMagick to white out all pixels in a row that do not have at least x% of a color. Does anyone know of such a capability?

Example image:
Image

I would like rows with less than roughly-5% of black pixels to be whited-out. My end goal is to trim to the first row that has a greater percentage of black pixels. Ideas? Thanks much!

Windows version - ImageMagick-7.0.1-6-portable-Q16-x64. Currently doing all my testing through a batch file.

(also curious about the same thing for columns of pixels - may help separate characters with handwriting on top)
Last edited by Masejoer on 2016-07-14T14:14:27-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Whiting out or trimming rows that have less than % of pixel color

Post by snibgo »

Scale it to a single column. The values in the resulting pixels are the percentage of pixels that are white. You can ready those values and do the decision-making in a script. Or, with some effort, you can use image-processing techniques to find the boundaries, eg the first or last row that has at least 5% black pixels (ie less that 95% white.
snibgo's IM pages: im.snibgo.com
Masejoer
Posts: 23
Joined: 2016-06-23T11:34:37-07:00
Authentication code: 1151

Re: Whiting out or trimming rows that have less than % of pixel color

Post by Masejoer »

snibgo wrote:Scale it to a single column. The values in the resulting pixels are the percentage of pixels that are white. You can ready those values and do the decision-making in a script.
Tried this. Need to find a more efficient way to handle it than I currently am.

I scaled to a 1x161 pixel high image. It takes quite a while to tell imagemagick to open the file and read the value of pixel 0x1, close, open file and read the value of pixel 0x2, close, ...and after each, opening the output file and drawing a 1-pixel-high white line.

Trying to find a way to have imagemagick do it all in one command. For each row, if grayscale value is over "230", draw white line from 0x(row) to (width)x(row). Haven't had success in finding such an all-in-one command yet.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Whiting out or trimming rows that have less than % of pixel color

Post by fmw42 »

Write your scaled image to txt: format. See http://www.imagemagick.org/Usage/files/#txt. Then you can write a loop to find all rows (or columns depending upon your scaling) that pass your test by parsing for the color/grayscale. Then from the list of lines, you can have imagemagick draw lines using -draw by converting the list of lines to the needed "line x,y x,y" arguments as a new list that can be called by -draw "listargument" where listgargument is a variable containing all the draw arguments for the data that passes your test. You could also just overlay a white 1D image over each row/column that passes your test (again in a script loop).

Depends upon your OS for the scripting. I am on a Mac (unix) so the syntax is different for Windows and you would need some help from a Windows user.
Masejoer
Posts: 23
Joined: 2016-06-23T11:34:37-07:00
Authentication code: 1151

Re: Whiting out or trimming rows that have less than % of pixel color

Post by Masejoer »

fmw42 wrote:Write your scaled image to txt: format.
Did this already. It sped it up quite a bit. :D

See h
fmw42 wrote:ttp://www.imagemagick.org/Usage/files/#txt. Then you can write a loop to find all rows (or columns depending upon your scaling) that pass your test by parsing for the color/grayscale. Then from the list of lines, you can have imagemagick draw lines using -draw by converting the list of lines to the needed "line x,y x,y" arguments as a new list that can be called by -draw "listargument" where listgargument is a variable containing all the draw arguments for the data that passes your test. You could also just overlay a white 1D image over each row/column that passes your test (again in a script loop).

Depends upon your OS for the scripting. I am on a Mac (unix) so the syntax is different for Windows and you would need some help from a Windows user.
I can convert something from unix - that isn't a problem. I'll look into and try a list- such a feature would be awesome! I imaging it will take seconds to process that way.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Whiting out or trimming rows that have less than % of pixel color

Post by snibgo »

Seconds? Hardly. 0.08 seconds on my computer. Windows BAT syntax:

Code: Select all

for /F "usebackq" %%L in (`%IM%identify ^
  -format "WW=%%w\nHH=%%h" \n
  check_bw.jpg`) do set %%L


%IM%convert ^
  check_bw.jpg ^
  ( +clone ^
    -scale "1x^!" ^
    -fuzz 15%% ^
    +transparent White ^
    -fill White -colorize 100 ^
    -scale "%WW%x^!" ^
  ) ^
  -compose Over -composite ^
  check_out.png
I expect V7 could do it in a single command.

Image
snibgo's IM pages: im.snibgo.com
Masejoer
Posts: 23
Joined: 2016-06-23T11:34:37-07:00
Authentication code: 1151

Re: Whiting out or trimming rows that have less than % of pixel color

Post by Masejoer »

snibgo wrote:

Code: Select all

for /F "usebackq" %%L in (`%IM%identify ^
  -format "WW=%%w\nHH=%%h" \n
  check_bw.jpg`) do set %%L


%IM%convert ^
  check_bw.jpg ^
  ( +clone ^
    -scale "1x^!" ^
    -fuzz 15%% ^
    +transparent White ^
    -fill White -colorize 100 ^
    -scale "%WW%x^!" ^
  ) ^
  -compose Over -composite ^
  check_out.png
Much better than what I was trying before. Works perfect. Moving on to the next, and hopefully last issue in a new thread. Hoping IM has a way to tackle it rather than the resize-to-1-column way I was trying here earlier.
Post Reply