How to integrate Opencv mosaics and ground glass effects with pictures
This article mainly introduces "how to achieve Opencv mosaic and ground glass effect and picture fusion". In daily operation, I believe many people have doubts about how to achieve Opencv mosaic and ground glass effect and picture fusion. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "how to achieve Opencv mosaic and ground glass effect and picture fusion". Next, please follow the editor to study!
1. Mosaic effect
The basic principle of mosaics is to replace all pixels in a region with a pixel in a region, resulting in a blurred effect on the image, as follows:
Import cv2# mosaic effect img = cv2.imread (filename='../anqila21.jpg',flags=1) imgInfo = img.shapeheight = imgInfo [0] width = imgInfo [1] flag = coefficient, the smaller the coefficient The less the effect of mosaics is, the less obvious it is: for m in range (26) height): for n in range (140880): if m%flag==0 and n%flag==0: for i in range (0 flag): for j in range (0 flag): (brecory gpentry r) = img [mmagne n] img [isimmjimn] = (benerg g) R) cv2.imshow ('dst',img) cv2.waitKey (0)
two。 Ground glass effect
Import cv2import numpy as npimport random# ground glass effect img = cv2.imread (filename='../anqila21.jpg',flags=1) imgInfo = img.shapeheight = imgInfo [0] width = imgInfo [1] dst = np.zeros ((height,width,3), np.uint8) for i in range (height-8): for j in range (width-8): index = int (random.random () * 8) # generate random numbers between 0 and 8 (excluding 8) dst [I J] = img cv2.imshow ('dst',dst) cv2.waitKey (0)
3. The fusion of pictures
Is to merge two pictures into one picture, you can see the effect of the first picture and the effect of the second picture.
Import cv2img_1 = cv2.imread (filename='../anqila21.jpg',flags=1) img_2 = cv2.imread (filename='../huanghe.jpg',flags=1) imgInfo = img_1.shapeheight = imgInfo [0] width = imgInfo [1] # roiroi = 0.5dst = cv2.addWeighted (img_1,roi,img_2, (1-roi), 0) cv2.imshow ('dst',dst) cv2.waitKey (0)
Algorithm realization
Import cv2import numpy as npimg_1 = cv2.imread (filename='../anqila21.jpg',flags=1) img_2 = cv2.imread (filename='../huanghe.jpg',flags=1) imgInfo = img_1.shapeheight = imgInfo [0] width = imgInfo [1] # roiroi = 0.5dst = np.zeros ((height,width,3), np.uint8) for i in range (height): for j in range (width): R2) = img_2 [iMagnej] dst [iMagnej] = (int (b1roomroitransib2* (1-roi)), int (g1furoiimperg2* (1-roi)), int (r1roitransir2* (1-roi)) cv2.imshow ('dst',dst) cv2.waitKey (0)
At this point, the study on "how to achieve Opencv mosaic and ground glass effect and picture fusion" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!