Using Python OpenCV-based image of painting effects

Algorithm steps :

1, to obtain an image of the gray-scale pictures

2, the design of a small box (4x4 / 8x8 / 10x10, etc.), the statistical value of each pixel in the small block

3, the 0-255 gray value is divided into several levels, and the results are mapped to the respective second-step treatment level set and counted

4, to find all the pixels in each block up to the gray scale, and the mean of these pixels is obtained

5, the statistical average out to replace the original pixel value

CV2 Import 
Import numpy AS NP 

IMG = cv2.imread ( ' ./Nature.jpg ' , . 1 ) 
imgInfo = img.shape 
height = imgInfo [ 0 ] 
width = imgInfo [ . 1 ] 
Gray = cv2.cvtColor (IMG, cv2.COLOR_BGR2GRAY ) 
DST = np.zeros ((height, width, . 3 ), np.uint8) 
pixel_class = . 4 
sectionTop = int ( 256 / pixel_class) 
# use two for each loop to iterate the picture data 
for Iin Range ( . 3 , height - . 3 ):
     for J in Range ( . 3 , width - . 3 ): 
        # current gray level is defined in the program 4 
        # define an array to load the number of pixels in four levels 
        array1 = np.zeros (pixel_class, np.uint8) 
        # current small block is defined in the program's 6x6 
        for m in Range (- . 3 , . 3 ):
             for n- in Range (- . 3 , . 3 ): 
                # P1 is the pixel point division level segment, represented by the subscript 0 - . 3 
                P1 =int (Gray [I + m, n-J +] / sectionTop) 
                # next level of the pixel counts, the subscripts represent array1 pixel level, 
                the number # of the representative value of the pixel level of the pixel in the small block 
                array1 [P1] = array1 [P1] + . 1 
        # next determine which of the pixels within a pixel segment of this small box up 
        CurrentMax = array1 [ 0 ] 
        l = 0   up to a # l is provided herein for the recording pixel array segment count subscript
         for K in Range ( 0 , pixel_class):
             IF CurrentMax < array1 [K]: 
                CurrentMax = array1 [K] 
                L = K  
        # mean treatment
        U= v = w = 0
        for m in range(-3, 3):
            for n in range(-3, 3):
                if gray[i + m, j + n] >= (l * section) and gray[i + m, j + n] <= ((l + 1) * section):
                    (b, g, r) = img[i + m, j + n]
                    u += b
                    v += g
                    w += r
        u = int(u / array1[l])
        v = int(v / array1[l])
        w = int(w / array1[l])
        dst[i, j] = [u, v, w]
cv2.imshow('dst', dst)
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('RoseOilPainting.png', dst)

 

Guess you like

Origin www.cnblogs.com/5211314jackrose/p/11306556.html