Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize threshold Segmentation by Python+OpenCV

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to achieve threshold segmentation in Python+OpenCV". In daily operation, I believe many people have doubts about how to achieve threshold segmentation in Python+OpenCV. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubt of "how to achieve threshold segmentation in Python+OpenCV". Next, please follow the editor to study!

I. Global threshold

Original picture:

The whole picture uses a threshold, which is compared with the grayscale of each pixel of the picture and re-assigned.

1. Effect picture

two。 Source code import cv2import matplotlib.pyplot as plt# sets threshold thresh=130# to load the original image And converted to grayscale image img_original=cv2.imread (ringing E:\ py\ python3.7\ test2\ test14yuzhi\ cell.png',0) img_original=cv2.resize (img_original, (0Let0), fx=0.3,fy=0.3) # Segmentation of image retval1,img_binary=cv2.threshold (img_original,thresh,255,cv2.THRESH_BINARY) retval2,img_binary_invertion=cv2.threshold (img_original,thresh,255,cv2.THRESH_BINARY_INV) retval3 using five threshold types (thresholding type) Img_trunc=cv2.threshold (img_original,thresh,255,cv2.THRESH_TRUNC) retval4,img_tozero=cv2.threshold (img_original,thresh,255,cv2.THRESH_TOZERO) retval5,img_tozero_inversion=cv2.threshold (img_original,thresh,255,cv2.THRESH_TOZERO_INV) # uses plt.imshow () to display the image imgs= [IMG _ original,img_binary,img_binary_invertion,img_trunc,img_tozero,img_tozero_inversion] titles= ['original','binary','binary_inv','trunc' 'tozero','tozero_inv'] for i in range (6): plt.subplot (2jin3) plt.imshow (IMGS [I],' gray') plt.title (titles1) plt.xticks ([]) plt.yticks ([]) plt.show () 2, sliding change threshold (slider) 1. Effect picture

two。 Source code

The code is as follows (example):

Import cv2import numpy as npimport matplotlib.pyplot as plt# loads the original image, converts it to grayscale image, and adjusts the image size by cv2.resize (cv2.resize:\ py\ python3.7\ test2\ test14yuzhi\ cell.png',0) img_original=cv2.resize (img_original, (0L0), fx=0.3,fy=0.3) # initialization threshold, defining the global variable imgsthresh=130imgs=0# to create a slider callback function Parameter thresh is the numerical value def threshold_segmentation (thresh) of the corresponding position of the slider bar: # Segmentation image retval1,img_binary=cv2.threshold (img_original,thresh,255,cv2.THRESH_BINARY) retval2,img_binary_invertion=cv2.threshold (img_original,thresh,255,cv2.THRESH_BINARY_INV) retval3,img_trunc=cv2.threshold (img_original,thresh,255,cv2.THRESH_TRUNC) retval4,img_tozero=cv2.threshold (img_original) using five threshold types (thresholding type) Thresh,255,cv2.THRESH_TOZERO) retval5,img_tozero_inversion=cv2.threshold (img_original,thresh,255,cv2.THRESH_TOZERO_INV) # because cv2.imshow () shows multidimensional arrays (ndarray), we splice arrays through np.hstack (horizontal splicing of arrays) # and np.vstack (vertical splicing) Achieve the goal of displaying multiple images at the same time img1=np.hstack ([img_original,img_binary,img_binary_invertion]) img2=np.hstack ([img_trunc,img_tozero,img_tozero_inversion]) global imgs imgs=np.vstack ([img1,img2]) # New window cv2.namedWindow ('Images') # New Slider The initial position is 130cv2.createTrackbar ('threshold value','Images',130,255,threshold_segmentation) # first call function threshold_segmentation (thresh) # display image while (1): cv2.imshow (' Images',imgs) if cv2.waitKey (1) = = ord ('q'): breakcv2.destroyAllWindows () 3, adaptive threshold segmentation 1. Effect picture

two。 Source code

The code is as follows (example):

Import cv2import matplotlib.pyplot as plt# loads the original image img_original=cv2.imread (ringing E:\ py\ python3.7\ test2\ test14yuzhi\ cell.png',0) # Global threshold segmentation retval,img_global=cv2.threshold (img_original,130,255,cv2.THRESH_BINARY) # Adaptive threshold segmentation img_ada_mean=cv2.adaptiveThreshold (img_original,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,15,3) img_ada_gaussian=cv2.adaptiveThreshold (img_original,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C Cv2.THRESH_BINARY,15,3) imgs= [IMG _ original,img_global,img_ada_mean,img_ada_gaussian] titles= ['Original Image','Global Thresholding (130,130)', 'Adaptive Mean','Adaptive Guassian',] # display picture for i in range (4): plt.subplot (2meme 2meme 1) plt.imshow (IMGS [I] 'gray') plt.title (titles [I]) plt.xticks ([]) plt.yticks ([]) plt.show () 3.GaussianBlur () function denoising

The code is as follows (example):

Import cv2import matplotlib.pyplot as plt# loads the original image img_original=cv2.imread (ringing E:\ py\ python3.7\ test2\ test14yuzhi\ cell.png',0) # Gaussian filter IMG _ blur=cv2.GaussianBlur (img_original, (1313), 13) # modify parameters # adaptive threshold segmentation img_thresh=cv2.adaptiveThreshold (img_original,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,15,3) img_thresh_blur=cv2.adaptiveThreshold (img_blur,255) Cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,15,3) # display image imgs= [IMG _ thresh,img_thresh_blur] titles= ['img_thresh','img_thresh_blur'] for i in range (2): plt.subplot (1meme2) plt.imshow (imgs [I],' gray') plt.title (titles [I]) plt.xticks ([]) plt.yticks ([]) plt.show () IV.

1.cv2.threshold (src, thresh, maxval, type)

Parameters:

Src: imported ima

Thresh: threshold used for image segmentation (threshold value)

Maxval: the new value assigned to the pixel when the threshold type (thresholding type) is cv2.THRESH_BINARY and cv2.THRESH_BINARY_INV

Type: introduces 6 types:

Cv2.THRESH_BINARY (when the pixel value of some point in the image is greater than thresh (threshold), maxval is assigned, and vice versa. Note: most commonly used)

Cv2.THRESH_BINARY_INV (maxval is given when the pixel value of a certain point in the image is less than thresh, and vice versa)

Cv2.THRESH_TRUNC (thresh is given when the pixel value of a certain point in the image is greater than thresh, and vice versa. Note: although maxval is useless, the calling function cannot be omitted)

Cv2.THRESH_TOZERO (0 is given when the pixel value of a certain point in the image is less than thresh, and vice versa. Note: ditto)

Cv2.THRESH_TOZERO_INV (0 is given when the pixel value of a certain point in the image is greater than thresh, and vice versa. Note: ditto)

Cv2.THRESH_OTSU (this method automatically finds the optimal threshold and returns it to retval, see below)

Return value:

Retval: the threshold value set, or the optimal threshold calculated by cv2.THRESH_OTSU

Dst: image after threshold segmentation

At this point, the study on "how to achieve threshold segmentation in Python+OpenCV" 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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report