当前位置:网站首页>Basic operation of OpenCV actual combat image: this effect amazed everyone (with code analysis)

Basic operation of OpenCV actual combat image: this effect amazed everyone (with code analysis)

2022-06-10 23:58:00 Programmer pear

Preface

author :“ Programmer pear ”

** The article brief introduction **: This article focuses on Opencv Basic operation binarization 、 Image noise processing .

** Article source code access **: In order to thank everyone who pays attention to me, the project source code of each article is distributed free of charge

Enjoy drops

Click here for the blue line font , If you need any source code, remember to say the title and name ! I can also !

Welcome friends give the thumbs-up 、 Collection 、 Leaving a message.

Text

One 、 Two valued

1.1  The basic principle of binarization

Image binarization is to set the gray value of pixels on the image to 0 or 255, That is to present the whole image in obvious black and white

The process of effect . In digital image processing , Binary image plays a very important role , The binarization of image makes the amount of data in the image large

In order to reduce , So as to highlight the outline of the target .

1.2  Basic operation of image binarization

We use OpenCV To perform image binarization , Today's lecture OpenCV Image binary is mainly used in this

threshold Function to implement .

1.3  The attached code

image = cv2.imread('nv.png')
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)# Grayscale the input image 
cv2.threshold(image, 140, 255, 0, image)

cv2.namedWindow("Image")
cv2.imshow("Image", image)
cv2.waitKey(0)

In this case we are using threshold function , The function prototype is

threshold(src, thresh, maxval, type[, dst])
  • src Parameter means input image ( Multichannel ,8 Bit or 32 Bit floating point ).
  • thresh The parameter represents the threshold .
  • maxval Parameter representation and THRESH_BINARY and THRESH_BINARY_INV The threshold type is used together with the maximum value set .
  • type The parameter indicates the threshold type .
  • retval Parameter indicates the threshold value returned . If it is a global fixed threshold algorithm , Then return to thresh Parameter values . If it is a global adaptive threshold algorithm , Then the appropriate threshold calculated adaptively is returned .
  • dst The parameter represents the output and src Images of the same size and type and the same number of channels .

1.4  Effect display

I actually think the binary graph is very nice ~

Two 、 Image denoising

1.1 Introduction of the principle

Add noise -> The picture is not clear .

principle : Randomly replace the prime points with other values .

1.2  The attached code

img = cv2.imread('nv.png')
#  Number of noise points 
coutn = 100000
for k in range(0,coutn):
    #  Obtain the random position of image noise points 
    xi = int(np.random.uniform(0,img.shape[1]))
    xj = int(np.random.uniform(0,img.shape[0]))
    # Add noise 
    if img.ndim == 2:
        #  Grayscale image 
        img[xj,xi] = 255
    elif img.ndim == 3:
        #  Non grayscale image , Image denoising 
        img[xj,xi,0] = 25
        img[xj,xi,1] = 20
        img[xj,xi,2] = 20
cv2.namedWindow('img')
cv2.imshow('img',img)
cv2.waitKey()
cv2.destroyAllWindows()

1.2  Effect display

Original picture ——

design sketch ——

​ summary

Is the result of binarization and noise adding also quite satisfactory Ha ha ha It may be that my buyer shows a good picture ~ Rely on your face

value .jpg Well ~ Then you can ask me to try the code by yourself !

  Follow Xiaobian for more wonderful content ! Remember to click on the portal

Remember Sanlian ! If you need packaged source code + Free sharing of materials ! Portal

 

原网站

版权声明
本文为[Programmer pear]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206102242248200.html