Page 1 of 1
Whiting out or trimming rows that have less than % of pixel color
Posted: 2016-07-14T13:50:17-07:00
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:
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)
Re: Whiting out or trimming rows that have less than % of pixel color
Posted: 2016-07-14T14:01:14-07:00
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.
Re: Whiting out or trimming rows that have less than % of pixel color
Posted: 2016-07-14T17:05:43-07:00
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.
Re: Whiting out or trimming rows that have less than % of pixel color
Posted: 2016-07-14T17:17:22-07:00
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.
Re: Whiting out or trimming rows that have less than % of pixel color
Posted: 2016-07-14T17:31:37-07:00
by Masejoer
fmw42 wrote:Write your scaled image to txt: format.
Did this already. It sped it up quite a bit.
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.
Re: Whiting out or trimming rows that have less than % of pixel color
Posted: 2016-07-14T17:56:16-07:00
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.
Re: Whiting out or trimming rows that have less than % of pixel color
Posted: 2016-07-26T14:14:53-07:00
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.