1.可以采用分块方法;2.先缩放处理就行二值化,然后还原大小。
一:分块处理超大图像的二值化问题
局部阈值二值化
def big_image_binary0(image):
print(image.shape)
cw = 256
ch = 256
h, w = image.shape[:2]
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
for row in range(0, h, ch):
for col in range(0, w, cw):
roi = gray[row:row + ch, col:cw + col]
dst = cv.adaptiveThreshold(roi,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,127,20)
gray[row:row + ch,col:cw + col] = dst
print(np.std(dst),np.mean(dst))
cv.imwrite("./hangmu1.png",gray)
结果:
每块图像的阈值
--------HEllow Python-------
(2465, 4096, 3)
31.640189034551284 251.01173400878906
77.2843304085478 228.90701293945312
41.04087384865292 248.214111328125
71.44629373431908 233.10150146484375
97.55906208431752 209.58824157714844
79.7773812697638 226.95762634277344
66.2764738310132 236.42051696777344
75.7966708802325 230.0237274169922
0.0 255.0
9.014367760456446 254.68093872070312
71.74444215254391 232.899169921875
96.11961061661722 211.26914978027344
98.25366370805146 208.7555694580078
98.28904427087875 208.7127685546875
94.8701665360566 212.68157958984375
88.4249797080433 219.35462951660156
21.72076029323974 253.13621520996094
25.422714675248614 252.43972778320312
43.370019943908815 247.39700317382812
112.84872862355478 186.8415069580078
81.8056142806548 225.29617309570312
58.502007027885355 240.7862091064453
92.40763309819559 215.34690856933594
87.0298287707177 220.6775665283203
17.636372107464823 253.7743377685547
18.130766117209664 253.7042999267578
84.39505042692974 223.07052612304688
83.42015314186085 223.9226531982422
89.79073484531753 218.02001953125
65.37935169396087 236.96136474609375
65.91060304656314 236.64230346679688
93.67264755640228 213.99673461914062
38.93220402161748 248.9105987548828
37.835275069569285 249.25689697265625
95.02360746847852 212.5103759765625
99.03746395583319 207.7983856201172
44.857728025955595 246.84837341308594
15.11260936238124 254.10118103027344
24.36733921530624 252.64984130859375
98.03748466148843 209.01626586914062
67.89262643273925 235.4205322265625
8.792025054881647 254.69650268554688
55.76196324333264 242.15972900390625
79.80647769590844 226.9342803955078
95.64556608473764 211.80999755859375
89.97472203154959 217.83714294433594
52.129496917293906 243.856201171875
39.66476898462764 248.67324829101562
30.462525957645273 251.3074493408203
46.16112955496589 246.35032653808594
109.88753697140889 192.16049194335938
83.32561266691481 224.00436401367188
40.71901759694094 248.32305908203125
18.951407324747496 253.58367919921875
78.26989086355316 228.1482696533203
92.0697321603415 215.70098876953125
94.70213990089749 212.86834716796875
5.174788649202658 254.8949432373047
19.054666652879185 253.568115234375
65.43143946380246 236.93023681640625
118.77244132225242 173.8611602783203
94.78623645195646 212.77496337890625
59.34569336992472 240.34652709960938
60.11615608636622 239.93797302246094
109.3046488784285 193.14102172851562
103.07197380642646 202.5494384765625
116.6116059823452 179.05563354492188
106.8640526539306 197.04368591308594
98.77791886560556 208.11744689941406
81.08389522368869 225.8953857421875
94.90159749050389 212.6465606689453
95.58721805053882 211.8761444091797
79.73855888661323 226.98875427246094
24.605934379388106 252.6031494140625
34.879757615172934 250.13626098632812
98.95219276495973 207.9034423828125
112.38192336587014 187.7208709716797
102.39705402185547 203.46771240234375
89.02329904646253 218.77487182617188
27.670844934312317 251.9611358642578
108.33063361887686 194.736328125
65.98785900204174 236.59561157226562
99.26708856612973 207.51434326171875
105.34245030078563 199.32769775390625
114.99592121907851 182.5653076171875
118.79369004507535 173.8066864013672
108.70263747685453 194.1332244873047
106.54339836212202 197.53395080566406
46.88588546483457 246.0662841796875
85.9253979806723 221.69700622558594
111.12343020370517 190.01266479492188
112.16414141885409 188.1255340576172
120.44492889627385 169.3242645263672
118.03910763398133 175.6977081298828
81.62386063083493 225.4479217529297
88.89145855520842 218.9032745361328
65.64570862567567 236.8018341064453
53.83284535286192 243.0780029296875
66.49340176661178 236.28822326660156
71.12895819489212 233.3155059814453
97.83639402661117 209.25750732421875
105.12151873453179 199.65065002441406
79.5926978772228 227.10548400878906
65.72985704161248 236.75125122070312
97.1014088069064 210.12908935546875
60.30498931810651 239.83680725097656
114.22411076056656 184.14894104003906
114.01473612398398 184.5691680908203
120.41381543231034 169.41375732421875
117.8441623353825 176.1724090576172
115.92170470812738 180.58868408203125
120.05828906581044 170.42152404785156
35.969442175163195 249.8210906982422
61.44352914255672 239.2181396484375
64.49176023829378 237.4866485595703
90.2822977094654 217.52975463867188
102.11596261021646 203.8451385498047
56.61789640654224 241.739501953125
66.82962753857255 236.08200073242188
95.45649832878 212.0240020751953
80.81875253373067 226.11328125
71.30807269281362 233.19488525390625
42.16801438742143 247.82501220703125
86.98398183522019 220.72036743164062
91.58247925503261 216.20681762695312
89.10699964014528 218.6931610107422
90.34433664263197 217.46749877929688
103.3822628298962 202.12142944335938
78.86169758297827 227.68524169921875
77.74167722341066 228.55682373046875
73.7877108075869 231.4789581298828
86.78757979578833 220.9032440185547
92.85659552144297 214.87220764160156
95.0444881452859 212.48703002929688
98.38858450440226 208.59214782714844
104.3653856146073 200.74012756347656
67.99148637218782 235.3582763671875
72.57198891256633 232.33108520507812
57.91815374451231 241.0858154296875
87.68340734017337 220.0627899169922
81.26769205728982 225.74363708496094
93.51066259539299 214.1718292236328
99.22943439176554 207.56103515625
113.57627548943026 185.43685913085938
91.70697939482875 216.07810073757764
79.54909619289221 227.14030958850933
72.59872303087597 232.31257278726707
68.40470170205734 235.0966857531056
61.89310802823305 238.96969623447205
86.51976819090247 221.1513732531056
100.35301045976775 206.14809782608697
71.83354841568097 232.83846079192546
71.47843218048165 233.07975058229815
88.37089676611131 219.40666246118013
89.3323320230577 218.47243788819875
81.25463073394236 225.75444002329192
15.406313334392701 254.06577542701862
57.51637685862726 241.2897903726708
50.49439720518448 244.57504367236024
35.883610113016545 249.8462975543478
每块的阈值:
--------HEllow Python-------
(2121, 3900, 3)
15.99890373610347 253.9922332763672
29.16832530952289 251.6187286376953
34.104343302770175 250.35415649414062
33.395597588112 250.5487060546875
9.33791197563113 254.6575927734375
4.671309562002848 254.91439819335938
3.4502540446253858 254.95330810546875
0.0 255.0
0.0 255.0
0.0 255.0
0.9960861503788008 254.99610900878906
0.9960861503788008 254.99610900878906
8.026781024360817 254.74708557128906
10.764779323497056 254.5447540283203
10.810602147263442 254.54086303710938
8.225801988826074 254.734375
8.792025054881647 254.69650268554688
57.09430318184392 241.5021514892578
61.1454515537087 239.38156127929688
4.563943610213882 254.9182891845703
2.4398097301799266 254.97665405273438
23.470954383863223 252.821044921875
66.32121504059718 236.39328002929688
62.844591798434905 238.43605041503906
24.025118299816388 252.7159881591797
24.186789148675878 252.6848602294922
26.250054789649884 252.26852416992188
20.987843204155105 253.26072692871094
32.002512855101244 250.91835021972656
35.67704626247899 249.9066925048828
27.723296813960474 251.949462890625
36.476315260372175 249.6708984375
25.037228085912485 252.51754760742188
24.900840505288958 252.54478454589844
23.615920075645622 252.79380798339844
25.594212314953253 252.4047088623047
35.636972283971794 249.91836547851562
24.327337136321006 252.65762329101562
53.032001029536374 243.44764709472656
68.47057399290217 235.05477905273438
65.53544644985986 236.86798095703125
51.53485880767861 244.12078857421875
43.61663051532372 247.30751037597656
40.788225096936166 248.29971313476562
34.79756926706634 250.15960693359375
41.35993857872541 248.10516357421875
33.194232793033684 250.60317993164062
29.472407481140305 251.546875
46.807091780616666 246.097412109375
93.88768482075767 213.76327514648438
62.562144065891474 238.5955810546875
23.882725287073242 252.74322509765625
23.304136143440232 252.8521728515625
100.44702898371864 206.02798461914062
46.836657370372876 246.0857391357422
58.12372503860947 240.9807586669922
57.8647071330082 241.11305236816406
44.992079028358 246.79779052734375
31.16553966082931 251.13235473632812
23.49172047165232 252.81715393066406
38.725318295712285 248.97674560546875
38.357225581418625 249.09347534179688
18.58537969836053 253.63815307617188
30.501507315364208 251.2978515625
37.785152602907885 249.2724609375
74.15370648889291 231.21826171875
74.12104180912792 231.24160766601562
89.85736985395364 217.95387268066406
96.50439499789253 210.82557678222656
123.3420784231338 159.79522705078125
83.14501603849146 224.16000366210938
65.42493157035861 236.9341278076172
35.90321558034742 249.84054565429688
19.08039218593911 253.56422424316406
39.72415336479274 248.65379333496094
7.316747999012764 254.78988647460938
2.2272483693181195 254.9805450439453
4.9795187103624095 254.90272521972656
13.159476662356331 254.31907653808594
14.379665480106384 254.1865234375
51.84194304317472 243.98460388183594
91.1925844243533 216.6075897216797
118.83458847798059 173.70162963867188
124.95375128696467 152.85369873046875
126.73164211401743 141.4764404296875
125.0172871783738 152.5385284423828
126.68282276917714 113.0877685546875
126.00395754016888 146.9744110107422
118.3694586705266 174.88059997558594
116.4544186319265 179.4097137451172
105.05468747950384 199.7479248046875
98.99958847977669 207.84507751464844
85.02968190216382 222.50633239746094
53.11699017010448 243.4087371826172
3.726643425359686 254.94552612304688
0.0 255.0
51.51724497597576 244.12857055664062
53.56470950194826 243.2025146484375
50.65418773684733 244.50599670410156
52.05996181908495 243.8873291015625
51.72813784629874 244.03518676757812
84.69810917940018 222.8020477294922
86.8628955582694 220.8332061767578
80.48526634446256 226.38565063476562
105.42459439966863 199.2070770263672
97.92084499340876 209.15634155273438
92.35581716400557 215.40138244628906
93.39510265005141 214.2963409423828
117.94191408918208 175.93505859375
118.87236206575048 173.60435485839844
54.057827402837084 242.9729461669922
0.0 255.0
83.83659361789707 223.560791015625
83.79198936413916 223.59970092773438
80.45179098020846 226.4128875732422
78.09947535003796 228.2805633544922
72.45381446935227 232.4127960205078
63.675272040593065 237.9613494873047
41.665024401906244 248.00010681152344
53.89962595171116 243.046875
58.72749729978811 240.6694793701172
70.15852100470792 233.96141052246094
77.7568486278109 228.54515075683594
66.82962753857255 236.08200073242188
73.88083413195355 231.41281127929688
11.475851113263904 254.4824981689453
0.0 255.0
0.0 255.0
44.51489009621129 246.97666952054794
55.761524841513875 242.1599422089041
51.98649558879377 243.92016267123287
50.39918256960412 244.61606378424656
63.91848238817763 237.82079409246575
43.96163490354864 247.18134631849315
41.0283296516517 248.2183754280822
17.55565976763271 253.78558433219177
8.337603707477314 254.72709760273972
0.0 255.0
0.0 255.0
0.0 255.0
2.6378526063737304 254.97270976027397
10.208702002976986 254.59064640410958
0.0 255.0
0.0 255.0
全局阈值二值化:
代码:
def big_image_binary(image):
print(image.shape)
cw = 256
ch = 256
h, w = image.shape[:2]
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
for row in range(0, h, ch):
for col in range(0, w, cw):
roi = gray[row:row+ch, col:cw+col]
print(np.std(roi), np.mean(roi))
dev = np.std(roi)
if dev < 15:
gray[row:row + ch, col:cw + col] = 255
else:
ret, dst = cv.threshold(roi, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
gray[row:row + ch, col:cw + col] = dst
cv.imwrite("./hangmu.png", gray)
结果:
--------HEllow Python-------
(2465, 4096, 3)
25.86957622141447 111.30171203613281
31.382828673286628 114.033935546875
18.794691719041154 72.61190795898438
45.71099666500313 45.574066162109375
42.00464197730303 123.75770568847656
47.41478993082643 68.48690795898438
40.77424476213935 49.926116943359375
43.45519345327646 61.77958679199219
3.4032966702911165 33.86564636230469
8.382388090839827 39.47068786621094
24.47956959185208 57.72669982910156
34.00380577324739 68.85270690917969
46.465353215898816 66.59837341308594
48.16396633859457 74.9775390625
67.50434306631341 119.37858581542969
65.8917452539662 95.1212158203125
17.70856314349886 107.72308349609375
17.720010588139026 81.14501953125
30.39271154974085 37.761871337890625
65.19811520684566 112.73823547363281
33.78441822148287 81.18305969238281
31.891101555545934 36.23930358886719
56.569791543745424 66.93789672851562
49.94965686991538 109.45111083984375
9.534844628394263 31.671005249023438
9.713675122178145 34.86079406738281
63.865339449819885 81.05567932128906
56.80116444344759 126.84890747070312
58.15880513327027 90.43226623535156
32.394741698417754 50.294952392578125
41.31417213751762 59.88240051269531
63.50411393209818 91.37907409667969
16.19158712579837 86.69029235839844
25.692256637315474 49.013153076171875
68.52360376438953 63.714263916015625
38.91270040953175 115.26780700683594
27.87185484027689 52.05735778808594
10.82673271452347 21.746307373046875
17.820812104616625 35.405517578125
53.02105818105844 128.31390380859375
36.639584766993906 49.652679443359375
10.925141246871648 35.720123291015625
28.766406633518276 41.50666809082031
67.1622674194079 112.62068176269531
43.33569986023948 122.03021240234375
61.64416819263747 69.86381530761719
19.506393974314093 41.16581726074219
33.130457185448215 63.5777587890625
17.480449571911983 58.16755676269531
34.46861386591078 34.80108642578125
51.21063932219081 132.92037963867188
23.24808600120754 78.55363464355469
26.88038741889326 29.307022094726562
16.370963009854044 26.1937255859375
50.11344681550987 61.64768981933594
39.146740929917634 91.7999267578125
38.905574106825775 62.61299133300781
9.446234365772423 33.161468505859375
9.557759040751534 30.208709716796875
45.714638766989744 53.79292297363281
38.94113201328044 159.86720275878906
40.15661362415421 91.61495971679688
32.4570396096992 38.641998291015625
29.389074524473443 60.82911682128906
59.038552497833635 86.16160583496094
73.8904241731871 109.00022888183594
46.02586509868568 109.85159301757812
41.8761075709802 88.46076965332031
46.36599018726894 53.29402160644531
37.104850061053455 50.32513427734375
65.49107228111487 87.39775085449219
54.50351327629022 112.86756896972656
34.159181560492286 67.27159118652344
15.4503156830706 39.87754821777344
18.55591734383479 34.29579162597656
44.3339934679254 52.53962707519531
65.16144721228687 123.37677001953125
51.65544980169618 117.57984924316406
49.827434316623545 85.50364685058594
23.549024686841715 40.12579345703125
63.432036382889315 156.87863159179688
34.35005145965972 197.97369384765625
59.103037711089776 130.50083923339844
56.09032951058154 86.47663879394531
53.91214640063123 93.02033996582031
61.70862920530124 98.84623718261719
76.54013342681013 171.080078125
55.069139615028064 140.0390625
22.958571869447578 82.75112915039062
29.255427699673756 65.70443725585938
44.096135930370586 71.38880920410156
48.56358247808952 79.61769104003906
57.19490898752096 91.68116760253906
41.29811623424682 167.57044982910156
36.98404132267833 91.7947998046875
45.7996942862685 63.94404602050781
25.227795088029836 179.44020080566406
26.13229368689855 174.0244140625
32.63632382547953 182.32337951660156
31.801173373661825 177.77777099609375
45.53178594265585 161.86941528320312
50.958155132605626 164.3929901123047
44.90345751024645 179.6699981689453
43.248610569184315 115.31953430175781
61.31866351676398 132.41465759277344
21.56069526021746 88.98150634765625
67.28464769709475 140.4995574951172
68.52485700634212 165.4739227294922
71.29665638356965 149.674560546875
58.291568839746816 176.81442260742188
62.02295947019059 171.74581909179688
66.94816874573164 144.9168701171875
24.958344563947094 178.732177734375
29.298070673889953 176.68898010253906
28.291807855320975 175.8605194091797
39.72742292073194 176.18638610839844
35.65039072343436 179.0218505859375
28.083002962447022 169.2093505859375
41.740962052770186 203.34898376464844
54.930166360355116 131.56080627441406
53.22816624351744 161.7673797607422
38.28722167237901 109.70500183105469
20.774093688939022 86.3531494140625
48.22005849742125 146.8409423828125
32.60521914361197 183.23158264160156
30.35300243575428 186.8504638671875
30.583959481491952 188.8498992919922
33.25357425472831 192.01834106445312
28.62623092137019 186.5830078125
29.862428757888118 185.88925170898438
29.598665696605554 179.78834533691406
32.24794614607234 181.70152282714844
35.213202526109214 184.00425720214844
36.34243939164004 185.95277404785156
40.762230950426066 191.8435821533203
52.44276114249714 144.88037109375
36.545639626934246 187.1304168701172
39.16202239586461 106.29905700683594
21.797968576023933 86.30198669433594
42.032553638234845 122.55793762207031
40.226226547742 113.08074951171875
56.35311535020708 129.46678161621094
64.45036212599906 150.64332580566406
67.09861987097179 166.4184112548828
35.940203468226905 202.9121700310559
35.47080273153073 204.2239906832298
36.19617639807345 200.6146884704969
34.22066147166294 196.19050854037266
32.71476676108444 188.3088363742236
37.038086209922184 177.7964382763975
43.82899400250007 163.40853066770185
34.66566643481319 67.36825504658385
30.716071240968294 196.7517711568323
40.111618448229486 167.7240634704969
52.01390837779714 160.58732045807454
51.53683095559306 127.35047069099379
12.834237017604623 89.48173039596273
16.910069073816672 88.26620729813665
21.292641398953435 70.19504561335404
17.69449154812516 71.84328901397515
--------HEllow Python-------
(2121, 3900, 3)
23.308661952192857 142.4287567138672
27.127106346908985 156.44630432128906
29.83189150943956 172.5263214111328
32.70846870499006 182.81182861328125
19.43069724412608 193.54595947265625
21.63181895276084 197.13992309570312
22.204257609103248 188.80186462402344
14.891261861572554 175.17315673828125
11.649643403196237 167.88230895996094
6.292920530490104 162.3617401123047
11.019449355227382 156.70982360839844
13.922337596840052 151.69491577148438
13.918089018973102 147.139404296875
13.90778826177988 143.5006103515625
13.850750927855879 139.53567504882812
13.180982704239938 138.29850260416666
15.350000337651753 179.17161560058594
32.39922774986735 171.09019470214844
32.786874776467634 193.25990295410156
22.763099001971106 184.86395263671875
15.636542111084214 173.6963653564453
14.424008789170767 162.935791015625
31.713323334453293 157.48292541503906
29.19055409923562 148.45913696289062
11.779315635376522 138.303955078125
10.842140988156125 127.91716003417969
10.15791105845445 123.7200927734375
10.636010598019594 122.42030334472656
12.421127166032186 120.66653442382812
12.719569425289537 119.2015380859375
11.36802251573822 120.274658203125
12.294992639595357 117.24088541666667
24.538386027390914 134.19143676757812
23.72606765060207 135.020263671875
25.146020326901958 131.9980926513672
21.3687511795476 129.1284637451172
19.692040927812606 123.64056396484375
17.47609225760323 122.77145385742188
15.151052889628213 127.02458190917969
15.111202579292998 124.99278259277344
14.51183111418764 116.74411010742188
11.855100980681753 111.56816101074219
11.696281458334319 109.04548645019531
11.1721016036824 105.84286499023438
12.582850231468823 106.76193237304688
12.193447945404634 107.23333740234375
11.068754078386908 107.96971130371094
11.575392787412515 104.3486328125
19.375609430988124 99.4571533203125
41.498528763414484 109.58670043945312
26.87294264880948 99.21965026855469
9.734302492114692 95.17410278320312
9.319358366307972 99.43539428710938
42.131293570671176 114.96754455566406
10.711507302715374 108.56001281738281
12.049375412090397 109.90986633300781
13.377958726813128 101.32208251953125
11.264947599703364 99.33106994628906
11.161241628209606 94.11296081542969
11.942871505326414 92.21253967285156
13.105156076674882 89.28440856933594
13.11679027811587 90.68458557128906
13.26730578703864 92.96009826660156
11.430210025571341 94.01907552083334
16.211788315535514 92.52072143554688
31.667430243460753 98.99822998046875
33.65482581475147 100.49003601074219
41.6873240402385 103.84954833984375
45.21610634449543 116.87528991699219
75.17111656465659 139.1637725830078
31.877886323920084 107.12637329101562
21.989567661927346 97.41943359375
17.191280936818007 90.57301330566406
11.787996470761508 88.04959106445312
12.416194382655965 87.34877014160156
9.57869019004465 87.52934265136719
8.77725874194532 85.38420104980469
8.841515501787411 80.70962524414062
10.71097301474484 77.72021484375
11.103527353859668 79.31399739583334
11.026225757635727 90.05738830566406
46.585174090769016 97.45098876953125
57.717547213382346 75.71076965332031
65.2487167127936 91.95826721191406
63.94903059905861 89.56077575683594
80.29986950283471 117.21893310546875
73.32897704355958 97.72584533691406
71.30317510011388 95.25218200683594
70.57879492523783 92.79071044921875
73.59523042292815 109.33392333984375
50.44096464189038 88.26045227050781
48.2840893533582 92.30938720703125
41.80799147970376 95.863037109375
19.821035815591173 84.07264709472656
7.858670126506928 77.17665100097656
7.111514004711584 76.248828125
12.145315935729942 90.28662109375
11.347050808563901 94.5047607421875
18.413407465463766 84.87174987792969
21.48234504210734 68.97348022460938
21.295782734842433 57.02467346191406
41.101491266655025 58.490631103515625
50.34399311632312 66.36146545410156
44.51600923806025 69.45719909667969
40.62796932661677 66.40130615234375
37.245126853065706 57.66734313964844
35.416227688756926 55.6204833984375
36.608948024737856 55.4884033203125
58.67588079858267 73.56193542480469
81.61390809334205 135.31663513183594
31.00864220525394 84.08982849121094
4.795661342529599 76.76725260416667
17.076537663587345 82.25733947753906
17.702929826480574 86.93751525878906
15.722689627736363 90.32183837890625
14.703618304650142 89.62409973144531
13.519411624507265 93.18446350097656
15.119306437390449 85.09002685546875
17.753942151388742 77.42350769042969
22.01713856022951 68.68807983398438
22.76648183961 60.014190673828125
24.22544286201802 55.4622802734375
24.137760639425412 51.05790710449219
20.42001821145402 47.507720947265625
33.21887728796675 63.28456115722656
7.397171434837302 83.4925537109375
4.1972791472723525 83.89385986328125
4.445866784801439 82.7259765625
13.459688086731179 73.64372859589041
12.822193956544574 77.75032106164383
13.339906869414621 78.35439854452055
11.711455200836765 76.9642551369863
12.057621610234223 73.33128210616438
11.427753044410562 77.32475385273973
10.794552862343002 76.89972174657534
10.982364181401758 74.41566780821918
10.210984802650705 73.67471104452055
6.836992883867151 67.82047303082192
5.594209266744364 60.1505779109589
3.418486063260023 54.19616866438356
12.333969630342244 59.33411815068493
8.81828040695235 83.41208261986301
5.752886087401385 83.6779216609589
3.6205934904022956 84.51438356164384
完整代码:
import cv2 as cv
import numpy as np
def big_image_binary0(image):
print(image.shape)
cw = 256
ch = 256
h, w = image.shape[:2]
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
for row in range(0, h, ch):
for col in range(0, w, cw):
roi = gray[row:row + ch, col:cw + col]
dst = cv.adaptiveThreshold(roi,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,127,20)
gray[row:row + ch,col:cw + col] = dst
print(np.std(dst),np.mean(dst))
cv.imwrite("./hangmu1.png",gray)
def big_image_binary(image):
print(image.shape)
cw = 256
ch = 256
h, w = image.shape[:2]
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
for row in range(0, h, ch):
for col in range(0, w, cw):
roi = gray[row:row+ch, col:cw+col]
print(np.std(roi), np.mean(roi))
dev = np.std(roi)
if dev < 15:
gray[row:row + ch, col:cw + col] = 255
else:
ret, dst = cv.threshold(roi, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
gray[row:row + ch, col:cw + col] = dst
cv.imwrite("./hangmu.png", gray)
print("--------HEllow Python-------")
src = cv.imread("E:/picture/33.jpg")
#cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
#cv.imshow("input image",src)
big_image_binary(src)
cv.waitKey(0)
cv.destroyAllWindows()