1 __author__="WSX"2 ImportCv2 as CV3 ImportNumPy as NP4 5 defLocal_threshold (IMG):#Local threshold value6Gray = Cv.cvtcolor (IMG, CV. Color_bgr2gray)#change to Grayscale first7binary = Cv.adaptivethreshold (gray, 255, CV. Adaptive_thresh_gaussian_c, CV. Thresh_binary, 25, 10,)#255 Maximum Value8 #There are two ways to adaptive_thresh_gaussian_c (mean with weights) and adaptive_thresh_mean_c (and mean comparison)9 #blockSize must be an odd number, C is a constant (the average of each pixel block and the larger the mean than the extra C ... Less than C)Ten #ret threshold, binary two value image OneCv.imshow ("binary", binary) A returnbinary - - defJinzita (Level, IMG): thetemp =img.copy () -Level = Level -Pyr_img = [] - forIinchRange (level): +DST = Cv.pyrdown (temp)#Pyrup and Pyrdown opposite -temp =dst.copy () + returnTemp A at defresult (binary): -W, h = binary.shape[:2] - Print(binary) - Print(w,h) - #temp = Np.zeros ((w, h)) - #temp = List (temp) in #temp = []; tt = [] -With open ("Result.txt","r+") as F: to forIinchRange (W): + forJinchRange (h): - ifBINARY[I,J] = =0: thetemp ="0" * elifBINARY[I,J] = = 255: $temp ="1"Panax Notoginseng f.write (temp) -F.write ("\ r \ n") the f.close () + #print (Temp.shape) A defMain (): theimg = Cv.imread ("1.JPG") + #Cv.namedwindow ("Show", CV. Window_autosize) -Cv.imshow ("Show", IMG) $t = Jinzita (3, IMG) $binary=local_threshold (t) - result (binary) - cv.waitkey (0) the cv.destroyallwindows () - WuyiMain ()
18, OpenCV Python Simple implementation of a picture generation (similar to the vibrato generated letter portrait)