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:
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)
Whiting out or trimming rows that have less than % of pixel color
Whiting out or trimming rows that have less than % of pixel color
Last edited by Masejoer on 2016-07-14T14:14:27-07:00, edited 1 time in total.
-
- 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
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
Re: Whiting out or trimming rows that have less than % of pixel color
Tried this. Need to find a more efficient way to handle it than I currently am.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.
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.
- 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
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.
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.
Re: Whiting out or trimming rows that have less than % of pixel color
Did this already. It sped it up quite a bit.fmw42 wrote:Write your scaled image to txt: format.
See h
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.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.
-
- 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
Seconds? Hardly. 0.08 seconds on my computer. Windows BAT syntax:
I expect V7 could do it in a single command.
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
snibgo's IM pages: im.snibgo.com
Re: Whiting out or trimming rows that have less than % of pixel color
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.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