How to threshold an image to get about same amount of black and white?

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?".
konstantin
Posts: 50
Joined: 2013-08-07T13:50:31-07:00
Authentication code: 6789

Re: How to threshold an image to get about same amount of black and white?

Post by konstantin »

Yes, in fact I need a cumulative histogram, but I had no clue that ImageMagick can provide it.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to threshold an image to get about same amount of black and white?

Post by fmw42 »

konstantin wrote:Yes, in fact I need a cumulative histogram, but I had no clue that ImageMagick can provide it.
It does not have the ability to output a cumulative histogram directly. You can script that. But both -contrast-stretch and -linear-stretch use it when expressing arguments in percent.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to threshold an image to get about same amount of black and white?

Post by snibgo »

Cumulative histograms, and related processes, are available with my process modules:

Code: Select all

f:\web\im>"%IMDEV%convert" 7cXqWHe.jpg -colorspace gray -process 'mkhisto cumulnorm' -process invclut -crop 2x1@ -delete 0 +repage -crop 1x1+0+0 +repage txt:

# ImageMagick pixel enumeration: 1,1,255,gray
0,0: (79,79,79)  #4F4F4F  gray(79)
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to threshold an image to get about same amount of black and white?

Post by fmw42 »

I have a script to do that also for Unix systems. See http://www.fmwconcepts.com/imagemagick/ ... /index.php, though it is 8 bit (256 bins) limited, now.
konstantin
Posts: 50
Joined: 2013-08-07T13:50:31-07:00
Authentication code: 6789

Re: How to threshold an image to get about same amount of black and white?

Post by konstantin »

JB in facebook colours:
ImageImage

Image
konstantin
Posts: 50
Joined: 2013-08-07T13:50:31-07:00
Authentication code: 6789

Re: How to threshold an image to get about same amount of black and white?

Post by konstantin »

Maybe such a bisection / binary search method should be implemented in ImageMagick (similarly to Excel solver) to help maximize certain image properties. For example to find a gamma value against maximizing the black-white pixel transitions (edges) on the thresholded image, or simply find the thresholding graylevel value for maximizing a term defined by the ImageMagick built-in fx-language: http://www.imagemagick.org/script/fx.php
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to threshold an image to get about same amount of black and white?

Post by snibgo »

It's a technique that I use frequently, for example "Find the gamma that maximises standard deviation", or "Find the sigmoidal-contrast setting that results in a desired standard deviation".

Including a generic solver in IM would be useful, and faster than doing it in a script (because it wouldn't need to write and re-read). The software engineering would be non-trivial. But don't let that put you off if you have a spare month or three.
snibgo's IM pages: im.snibgo.com
konstantin
Posts: 50
Joined: 2013-08-07T13:50:31-07:00
Authentication code: 6789

Re: How to threshold an image to get about same amount of black and white?

Post by konstantin »

I cant' help, I can't code in C. Only Ruby, as I am a self-taught hobby programmer.
konstantin
Posts: 50
Joined: 2013-08-07T13:50:31-07:00
Authentication code: 6789

Re: How to threshold an image to get about same amount of black and white?

Post by konstantin »

Sometimes the histogram data is provided in a different way, which doesn't show the usual graylevels, I have no clue why:

Code: Select all

Histogram:
         1: ( 19, 19, 19) #131313 srgb(19,19,19)
         2: ( 20, 20, 20) #141414 grey8
         4: ( 22, 22, 22) #161616 srgb(22,22,22)
         4: ( 23, 23, 23) #171717 grey9
         8: ( 24, 24, 24) #181818 srgb(24,24,24)
         5: ( 25, 25, 25) #191919 srgb(25,25,25)
         9: ( 26, 26, 26) #1A1A1A grey10
        16: ( 27, 27, 27) #1B1B1B srgb(27,27,27)
        13: ( 28, 28, 28) #1C1C1C grey11
        13: ( 29, 29, 29) #1D1D1D srgb(29,29,29)
        15: ( 30, 30, 30) #1E1E1E srgb(30,30,30)
        17: ( 31, 31, 31) #1F1F1F grey12
        16: ( 32, 32, 32) #202020 srgb(32,32,32)
        24: ( 33, 33, 33) #212121 grey13
        20: ( 34, 34, 34) #222222 srgb(34,34,34)
        31: ( 35, 35, 35) #232323 srgb(35,35,35)
        28: ( 36, 36, 36) #242424 grey14
        30: ( 37, 37, 37) #252525 srgb(37,37,37)
        36: ( 38, 38, 38) #262626 grey15
        29: ( 39, 39, 39) #272727 srgb(39,39,39)
        28: ( 40, 40, 40) #282828 srgb(40,40,40)
        45: ( 41, 41, 41) #292929 grey16
        42: ( 42, 42, 42) #2A2A2A srgb(42,42,42)
        36: ( 43, 43, 43) #2B2B2B grey17
        33: ( 44, 44, 44) #2C2C2C srgb(44,44,44)
        36: ( 45, 45, 45) #2D2D2D srgb(45,45,45)
        40: ( 46, 46, 46) #2E2E2E grey18
        43: ( 47, 47, 47) #2F2F2F srgb(47,47,47)
        36: ( 48, 48, 48) #303030 grey19
        50: ( 49, 49, 49) #313131 srgb(49,49,49)
        42: ( 50, 50, 50) #323232 srgb(50,50,50)
        37: ( 51, 51, 51) #333333 grey20
        30: ( 52, 52, 52) #343434 srgb(52,52,52)
        40: ( 53, 53, 53) #353535 srgb(53,53,53)
        45: ( 54, 54, 54) #363636 grey21
        33: ( 55, 55, 55) #373737 srgb(55,55,55)
        38: ( 56, 56, 56) #383838 grey22
        31: ( 57, 57, 57) #393939 srgb(57,57,57)
        30: ( 58, 58, 58) #3A3A3A srgb(58,58,58)
        49: ( 59, 59, 59) #3B3B3B grey23
        41: ( 60, 60, 60) #3C3C3C srgb(60,60,60)
        35: ( 61, 61, 61) #3D3D3D grey24
        39: ( 62, 62, 62) #3E3E3E srgb(62,62,62)
        43: ( 63, 63, 63) #3F3F3F srgb(63,63,63)
        25: ( 64, 64, 64) #404040 grey25
        26: ( 65, 65, 65) #414141 srgb(65,65,65)
        30: ( 66, 66, 66) #424242 grey26
        22: ( 67, 67, 67) #434343 srgb(67,67,67)
        25: ( 68, 68, 68) #444444 srgb(68,68,68)
        26: ( 69, 69, 69) #454545 grey27
        21: ( 70, 70, 70) #464646 srgb(70,70,70)
        24: ( 71, 71, 71) #474747 grey28
        15: ( 72, 72, 72) #484848 srgb(72,72,72)
        25: ( 73, 73, 73) #494949 srgb(73,73,73)
        22: ( 74, 74, 74) #4A4A4A grey29
        18: ( 75, 75, 75) #4B4B4B srgb(75,75,75)
        30: ( 76, 76, 76) #4C4C4C srgb(76,76,76)
        19: ( 77, 77, 77) #4D4D4D grey30
        15: ( 78, 78, 78) #4E4E4E srgb(78,78,78)
        21: ( 79, 79, 79) #4F4F4F grey31
        31: ( 80, 80, 80) #505050 srgb(80,80,80)
        24: ( 81, 81, 81) #515151 srgb(81,81,81)
        24: ( 82, 82, 82) #525252 grey32
        26: ( 83, 83, 83) #535353 srgb(83,83,83)
        25: ( 84, 84, 84) #545454 grey33
        31: ( 85, 85, 85) #555555 srgb(85,85,85)
        31: ( 86, 86, 86) #565656 srgb(86,86,86)
        32: ( 87, 87, 87) #575757 grey34
        43: ( 88, 88, 88) #585858 srgb(88,88,88)
        29: ( 89, 89, 89) #595959 grey35
        40: ( 90, 90, 90) #5A5A5A srgb(90,90,90)
        33: ( 91, 91, 91) #5B5B5B srgb(91,91,91)
        42: ( 92, 92, 92) #5C5C5C grey36
        37: ( 93, 93, 93) #5D5D5D srgb(93,93,93)
        45: ( 94, 94, 94) #5E5E5E grey37
        55: ( 95, 95, 95) #5F5F5F srgb(95,95,95)
        34: ( 96, 96, 96) #606060 srgb(96,96,96)
        34: ( 97, 97, 97) #616161 grey38
        39: ( 98, 98, 98) #626262 srgb(98,98,98)
        45: ( 99, 99, 99) #636363 grey39
        44: (100,100,100) #646464 srgb(100,100,100)
        48: (101,101,101) #656565 srgb(101,101,101)
        39: (102,102,102) #666666 grey40
        47: (103,103,103) #676767 srgb(103,103,103)
        41: (104,104,104) #686868 srgb(104,104,104)
        57: (105,105,105) #696969 DimGray
        48: (106,106,106) #6A6A6A srgb(106,106,106)
        58: (107,107,107) #6B6B6B grey42
        63: (108,108,108) #6C6C6C srgb(108,108,108)
        61: (109,109,109) #6D6D6D srgb(109,109,109)
        58: (110,110,110) #6E6E6E grey43
        73: (111,111,111) #6F6F6F srgb(111,111,111)
        80: (112,112,112) #707070 grey44
        66: (113,113,113) #717171 srgb(113,113,113)
        58: (114,114,114) #727272 srgb(114,114,114)
        74: (115,115,115) #737373 grey45
        70: (116,116,116) #747474 srgb(116,116,116)
        76: (117,117,117) #757575 grey46
        68: (118,118,118) #767676 srgb(118,118,118)
        62: (119,119,119) #777777 srgb(119,119,119)
        59: (120,120,120) #787878 grey47
        70: (121,121,121) #797979 srgb(121,121,121)
        71: (122,122,122) #7A7A7A grey48
        57: (123,123,123) #7B7B7B srgb(123,123,123)
        54: (124,124,124) #7C7C7C srgb(124,124,124)
        65: (125,125,125) #7D7D7D grey49
        68: (126,126,126) #7E7E7E gray
        66: (127,127,127) #7F7F7F grey50
        64: (128,128,128) #808080 fractal
        67: (129,129,129) #818181 srgb(129,129,129)
        72: (130,130,130) #828282 grey51
        64: (131,131,131) #838383 srgb(131,131,131)
        72: (132,132,132) #848484 srgb(132,132,132)
        83: (133,133,133) #858585 grey52
        57: (134,134,134) #868686 srgb(134,134,134)
        69: (135,135,135) #878787 grey53
        58: (136,136,136) #888888 srgb(136,136,136)
        74: (137,137,137) #898989 srgb(137,137,137)
        69: (138,138,138) #8A8A8A grey54
        64: (139,139,139) #8B8B8B srgb(139,139,139)
        68: (140,140,140) #8C8C8C grey55
        72: (141,141,141) #8D8D8D srgb(141,141,141)
        93: (142,142,142) #8E8E8E srgb(142,142,142)
        79: (143,143,143) #8F8F8F grey56
        74: (144,144,144) #909090 srgb(144,144,144)
        80: (145,145,145) #919191 grey57
       106: (146,146,146) #929292 srgb(146,146,146)
        93: (147,147,147) #939393 srgb(147,147,147)
        70: (148,148,148) #949494 grey58
        87: (149,149,149) #959595 srgb(149,149,149)
        83: (150,150,150) #969696 grey59
       101: (151,151,151) #979797 srgb(151,151,151)
        81: (152,152,152) #989898 srgb(152,152,152)
        84: (153,153,153) #999999 grey60
       109: (154,154,154) #9A9A9A srgb(154,154,154)
       101: (155,155,155) #9B9B9B srgb(155,155,155)
        93: (156,156,156) #9C9C9C grey61
        98: (157,157,157) #9D9D9D srgb(157,157,157)
        84: (158,158,158) #9E9E9E grey62
        96: (159,159,159) #9F9F9F srgb(159,159,159)
        85: (160,160,160) #A0A0A0 srgb(160,160,160)
        80: (161,161,161) #A1A1A1 grey63
        79: (162,162,162) #A2A2A2 srgb(162,162,162)
       107: (163,163,163) #A3A3A3 grey64
        68: (164,164,164) #A4A4A4 srgb(164,164,164)
        73: (165,165,165) #A5A5A5 srgb(165,165,165)
        73: (166,166,166) #A6A6A6 grey65
        71: (167,167,167) #A7A7A7 srgb(167,167,167)
        96: (168,168,168) #A8A8A8 grey66
        69: (169,169,169) #A9A9A9 DarkGray
        84: (170,170,170) #AAAAAA srgb(170,170,170)
        88: (171,171,171) #ABABAB grey67
        82: (172,172,172) #ACACAC srgb(172,172,172)
       101: (173,173,173) #ADADAD grey68
        79: (174,174,174) #AEAEAE srgb(174,174,174)
        54: (175,175,175) #AFAFAF srgb(175,175,175)
        86: (176,176,176) #B0B0B0 grey69
        61: (177,177,177) #B1B1B1 srgb(177,177,177)
        70: (178,178,178) #B2B2B2 srgb(178,178,178)
        52: (179,179,179) #B3B3B3 grey70
        52: (180,180,180) #B4B4B4 srgb(180,180,180)
        63: (181,181,181) #B5B5B5 grey71
        58: (182,182,182) #B6B6B6 srgb(182,182,182)
        70: (183,183,183) #B7B7B7 srgb(183,183,183)
        54: (184,184,184) #B8B8B8 grey72
        52: (185,185,185) #B9B9B9 srgb(185,185,185)
        48: (186,186,186) #BABABA grey73
        56: (187,187,187) #BBBBBB srgb(187,187,187)
        48: (188,188,188) #BCBCBC srgb(188,188,188)
        45: (189,189,189) #BDBDBD grey74
        48: (190,190,190) #BEBEBE grey
        54: (191,191,191) #BFBFBF grey75
        65: (192,192,192) #C0C0C0 silver
        61: (193,193,193) #C1C1C1 srgb(193,193,193)
        42: (194,194,194) #C2C2C2 grey76
        52: (195,195,195) #C3C3C3 srgb(195,195,195)
        50: (196,196,196) #C4C4C4 grey77
        49: (197,197,197) #C5C5C5 srgb(197,197,197)
        39: (198,198,198) #C6C6C6 srgb(198,198,198)
        54: (199,199,199) #C7C7C7 grey78
        48: (200,200,200) #C8C8C8 srgb(200,200,200)
        46: (201,201,201) #C9C9C9 grey79
        36: (202,202,202) #CACACA srgb(202,202,202)
        27: (203,203,203) #CBCBCB srgb(203,203,203)
        38: (204,204,204) #CCCCCC grey80
        43: (205,205,205) #CDCDCD srgb(205,205,205)
        35: (206,206,206) #CECECE srgb(206,206,206)
        56: (207,207,207) #CFCFCF grey81
        41: (208,208,208) #D0D0D0 srgb(208,208,208)
        67: (209,209,209) #D1D1D1 grey82
        42: (210,210,210) #D2D2D2 srgb(210,210,210)
        69: (211,211,211) #D3D3D3 LightGray
        59: (212,212,212) #D4D4D4 grey83
        76: (213,213,213) #D5D5D5 srgb(213,213,213)
        69: (214,214,214) #D6D6D6 grey84
        74: (215,215,215) #D7D7D7 srgb(215,215,215)
        68: (216,216,216) #D8D8D8 srgb(216,216,216)
        83: (217,217,217) #D9D9D9 grey85
        67: (218,218,218) #DADADA srgb(218,218,218)
        83: (219,219,219) #DBDBDB grey86
        62: (220,220,220) #DCDCDC gainsboro
        63: (221,221,221) #DDDDDD srgb(221,221,221)
        60: (222,222,222) #DEDEDE grey87
        63: (223,223,223) #DFDFDF srgb(223,223,223)
        74: (224,224,224) #E0E0E0 grey88
        65: (225,225,225) #E1E1E1 srgb(225,225,225)
        58: (226,226,226) #E2E2E2 srgb(226,226,226)
        72: (227,227,227) #E3E3E3 grey89
        83: (228,228,228) #E4E4E4 srgb(228,228,228)
       135: (229,229,229) #E5E5E5 grey90
       204: (230,230,230) #E6E6E6 srgb(230,230,230)
       130: (231,231,231) #E7E7E7 srgb(231,231,231)
        69: (232,232,232) #E8E8E8 grey91
        94: (233,233,233) #E9E9E9 srgb(233,233,233)
        50: (234,234,234) #EAEAEA srgb(234,234,234)
        43: (235,235,235) #EBEBEB grey92
        30: (236,236,236) #ECECEC srgb(236,236,236)
        26: (237,237,237) #EDEDED grey93
        20: (238,238,238) #EEEEEE srgb(238,238,238)
        26: (239,239,239) #EFEFEF srgb(239,239,239)
        20: (240,240,240) #F0F0F0 grey94
        23: (241,241,241) #F1F1F1 srgb(241,241,241)
        31: (242,242,242) #F2F2F2 grey95
        33: (243,243,243) #F3F3F3 srgb(243,243,243)
        34: (244,244,244) #F4F4F4 srgb(244,244,244)
        32: (245,245,245) #F5F5F5 grey96
        61: (246,246,246) #F6F6F6 srgb(246,246,246)
       375: (247,247,247) #F7F7F7 grey97
       353: (248,248,248) #F8F8F8 srgb(248,248,248)
       158: (249,249,249) #F9F9F9 srgb(249,249,249)
       145: (250,250,250) #FAFAFA grey98
       135: (251,251,251) #FBFBFB srgb(251,251,251)
       203: (252,252,252) #FCFCFC grey99
       340: (253,253,253) #FDFDFD srgb(253,253,253)
       621: (254,254,254) #FEFEFE srgb(254,254,254)
         1: (255,255,250) #FFFFFA srgb(255,255,250)
       410: (255,255,255) #FFFFFF white

snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to threshold an image to get about same amount of black and white?

Post by snibgo »

If you mean the names at the end like "grey9" or "srgb(24,24,24)", I would ignore them. For each colour, IM looks to see if it has a name. If it has, like "grey9", it writes the name.
snibgo's IM pages: im.snibgo.com
konstantin
Posts: 50
Joined: 2013-08-07T13:50:31-07:00
Authentication code: 6789

Re: How to threshold an image to get about same amount of black and white?

Post by konstantin »

Problem solved, I had to apply options:

Code: Select all

 -colorspace gray -type palette 
Spectre in 2 colours:
https://www.youtube.com/watch?v=ZCtWucorr7g
konstantin
Posts: 50
Joined: 2013-08-07T13:50:31-07:00
Authentication code: 6789

Re: How to threshold an image to get about same amount of black and white?

Post by konstantin »

Another animation by animating the threshold:
Image
Post Reply