Extracr B&W ratio in order to "target" images

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
SteelMassimo
Posts: 16
Joined: 2019-02-06T11:29:18-07:00
Authentication code: 1152

Extracr B&W ratio in order to "target" images

Post by SteelMassimo »

Hello everyone!

I need to "target" some images in order to batch convert them with the following code (made with fmw42's amazing guidance):

Code: Select all

convert 0001.jpg -type Grayscale -threshold 35% -define connected-components:area-threshold=3 -define connected-components:mean-color=true -connected-components 8 0001.jpg
If I apply it on some images on the folder, it will eliminate a lot of information. Fortunately, I'm fairly confident the images I'm targeting for this command line actually have a lot more black than the rest.

I though of thresholding the whole folder, extract the ratio, filter the ratio interval that corresponds to the images I'm targeting, and them applying the code only for those.

I'm struggling with the "info:" output. Any tips?


IM version: 7.0.8-27-Q16-x64
OS: Windows 7 Pro 64-bits.
Version date: 2019-01-27.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Extracr B&W ratio in order to "target" images

Post by fmw42 »

You are going to have to show some of these images that do not work so we can see what is happening. Show both your input and output. It is likely that you will have to adjust the threshold and perhaps the area-threshold.
SteelMassimo
Posts: 16
Joined: 2019-02-06T11:29:18-07:00
Authentication code: 1152

Re: Extracr B&W ratio in order to "target" images

Post by SteelMassimo »

Here's the target image:

https://drive.google.com/open?id=1wQN8Q ... e36w3RdaVO

And these are the ones I'm trying to preserve:

https://drive.google.com/open?id=1xlyE5 ... Si-3Jok393
https://drive.google.com/open?id=1vmJvP ... 6wEVAu5IPg

Here's the histogram data:

Code: Select all

Range		                target.jpg	  preserve1.jpg	   preserve2.jpg
0-240	non-White px	            367029	         306392           285048
241-255	White px	           1867195	        1931716	         1952908
	% of n-White	            16,43%	         13,69%	          12,74%

If I can determine the ratio of white and non-white pixels, I think I'll be able to make a pretty good "target" on the whole folder with a batch file. Since there are thousands of images like those to separate, batch processing seems the way to go.
SteelMassimo
Posts: 16
Joined: 2019-02-06T11:29:18-07:00
Authentication code: 1152

Re: Extracr B&W ratio in order to "target" images

Post by SteelMassimo »

@fmw42 The input and output are on this folder

https://drive.google.com/drive/folders/ ... sp=sharing:

And here is the code applied:

Code: Select all

convert 0108_example.jpg -type Grayscale -threshold 35% -define connected-components:area-threshold=3 -define connected-components:mean-color=true -connected-components 8 0108_example_b.jpg
convert 0131_example.jpg -type Grayscale -threshold 35% -define connected-components:area-threshold=3 -define connected-components:mean-color=true -connected-components 8 0131_example_b.jpg
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Extracr B&W ratio in order to "target" images

Post by snibgo »

IM can calculate those numbers, eg:

Code: Select all

magick target_bw.jpg -threshold %[fx:240/255*100]% -format %[fx:(1-mean)*100] info:

16.4276
This is the percentage of pixels that are non-white, where 240/255 is the cut-off for "white".
snibgo's IM pages: im.snibgo.com
SteelMassimo
Posts: 16
Joined: 2019-02-06T11:29:18-07:00
Authentication code: 1152

Re: Extracr B&W ratio in order to "target" images

Post by SteelMassimo »

Hey @snibgo!

This is exactly what I need to filter the images.
I applied like this:

Code: Select all

magick *.jpg  -threshold %[fx:240/255*100]% -format %[fx:(1-mean)*100] info: >list.txt
So that all the information was transfered to a .txt file, but the spacing was all wrong:

Code: Select all

19.308218.20919.348718.994419.587417.671919.83718.648818.839418.72919.037618.445219.019418.887719.434417.96818.982119.142514.594913.510313.943212.279412.677812.379712.5871...
When ideally it would be more like this:

Code: Select all

19.3082
18.209
19.3487
18.9944
19.5874
17.6719
19.837
18.6488
18.8394
18.729
19.0376
18.4452
19.0194
18.8877
19.4344
17.968
18.9821
19.1425
14.5949
13.5103
13.9432
12.2794
12.6778
12.3797
12.5871
The cut-off percentages for the images I'm targeting is anything above 17.5%.

Is there a way for the output information on the .txt file to be formated like that?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Extracr B&W ratio in order to "target" images

Post by snibgo »

Insert \n at the end of the format:

Code: Select all

-format %[fx:(1-mean)*100]\n
You might need quotes:

Code: Select all

-format "%[fx:(1-mean)*100]\n"
You can make the fx expression more complex, eg to output "1" if the percentage is at least 17.5, otherwise "0":

Code: Select all

-format "%[fx:(1-mean)*100]>=17.5?1:0\n"
snibgo's IM pages: im.snibgo.com
SteelMassimo
Posts: 16
Joined: 2019-02-06T11:29:18-07:00
Authentication code: 1152

Re: Extracr B&W ratio in order to "target" images

Post by SteelMassimo »

@snibgo

Worked like a charm. Thanks for the help guys!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Extracr B&W ratio in order to "target" images

Post by fmw42 »

I believe snibgo has a typo and that this

Code: Select all

-format "%[fx:(1-mean)*100]>=17.5?1:0\n"
should actually be

Code: Select all

-format "%[fx:(1-mean)*100>=17.5?1:0]\n"
I believe that quotes are needed in Unix-like environments.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Extracr B&W ratio in order to "target" images

Post by snibgo »

Thanks for the correction, Fred.
snibgo's IM pages: im.snibgo.com
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Extracr B&W ratio in order to "target" images

Post by anthony »

For UNIX I would use single quotes.
Otherwise '\' in '\n' may get removed, though as the shell did not understand '\n' it probably didn't remove it.

In shell (and more (non-DOS) languages), single quotes means 'literal' or don't do anything... While double quotes means handle '$' variable substitutions and then that needs backslash escapes (for '\$' handling).

See IM examples...
Escaping Escapes
http://www.imagemagick.org/Usage/text/#escaping_escapes
To Quote or backslash
http://www.imagemagick.org/Usage/draw/#text
Though this is more about quoting within the -draw operator argument.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Extracr B&W ratio in order to "target" images

Post by fmw42 »

I have generally use double quotes on my Mac scripts with no issues with \n and when I need $variable in an expression.
Post Reply