python + opencv histogram and binarized

Image histogram (Histogram) is a statistical feature of the image, commonly used to understand the basic features of the image for analysis. However, the image histogram does not have spatial characteristics.

Image histogram (Histogram), then the image is converted to gray scale image, statistical gray value of each pixel, plotted as histograms horizontal axis represents gradation values ​​(0, 255), the vertical axis the gray scale value corresponding to the number of pixels. To do histogram points = size of the image.

Histogram

1  def plt_hist (img)
 2      plt.hist (img.ravel (), 256, [0, 256 ])
 3      plt.show ()

 

Three-color line graph (three primary colors can be intuitively seen that the proportion of distribution)

1 def img_hist(img):
2     color = ("blue", "green", "red")
3     for i, color in enumerate(color):
4         hist = cv.calcHist([img], [i], None, [256], [0, 256])
5         plt.plot(hist, color=color)
6         plt.xlim([0, 25])
7     plt.show()

 

In general, the histogram is bimodal, there will be two peaks to distinguish between foreground and background, but not so for each picture. Analysis of the image changes, and to determine the optimal value of binarization. Minimum between two peaks, often we need to find the binary value. We can see that the trough between two peaks are two peaks superimposed area. With the histogram, we can do the binary to the picture. Determine which pixel is we need to focus on what is important. Binarization two primary forms: one is the global threshold binarization, the other one is the local threshold binarization.

Select the binary value (to find the lowest point in two peaks, but not every multimodal there may be more than the minimum, you can use local threshold)

Classical algorithm : isodata algorithm preset initial binary value, and then continue the iteration, the effect is not the best

     Otsu algorithm bimodal association study evaluates the value of the binarized

     Entropy algorithm entropy algorithm, foreground and background entropy entropy hb hw. 0-255 gray values ​​are seeking entropy, maximum value of h is what we are looking for the binary value.

Other algorithms : triangle algorithm the maximum value and the minimum value of the left most connector of the gradation values, and then do the vertical, when the maximum vertical distance, that is, the gradation value corresponding to the binary value we need, but this algorithm the effect is not very good, used in cell separation more.

Use opencvAPI: (otsu and triangle)

. 1  DEF threshold_binary (the src):
 2      # The BGR images into gray scale image 
. 3      Gray = cv.cvtColor (the src, cv.COLOR_BGR2GRAY)
 . 4      # obtained histogram is used to adjust the algorithm 
. 5      plt.hist (src.ravel (), 256, [0, 256 ])
 . 6      plt.show ()
 . 7      # several binarization method 
. 8      RET, binary = cv.threshold (Gray, 50, 255, cv.THRESH_BINARY) # specified threshold 50 
. 9      Print ( " binary threshold:% S " % RET)
 10      cv.imshow ( " threshold_binary " , binary)
 . 11     ret_otsu, binary_otsu = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
12     print("二值阈值_otsu: %s" %ret_otsu)
13     cv.imshow("threshold_binary_otsu", binary_otsu)
14     ret_tri, binary_tri = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_TRIANGLE)
15     print("二值阈值_tri: %s" % ret_tri)

Run the sample:

Binary threshold value: 50.0
binary threshold _otsu: 173.0
binary threshold _tri: 250.0

 

The above is the global threshold of the binarization result, the following are local binarization threshold. Local threshold personally feel better depict the outline of the picture.

1 def local_threshold_binary(src):
2     gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
3     binary_mean = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 25, 10)
4     cv.imshow("binary_mean", binary_mean)5 
6     binary_gau = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 25, 10)
7     cv.imshow("binary_gau", binary_gau)

 

两种方法,

 

超大图像及其二值化

经常会遇到一些比较大的图像,我们可以把图片拆分成很多个小方块分别进行全局二值化或者使用局部阈值。这时候可以用opencv处理图像显示不全,需要把图片保存到本地才行。

 1 def big_image_binary(src):
 2     print(src.shape)
 3     cw = 256
 4     ch = 256
 5     h, w, c = src.shape
 6     gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
 7     for row in range(0, h, ch):
 8         for col in range(0, w, cw):
 9             roi = gray[row:row+ch, col:col+cw]
10             dst = cv.adaptiveThreshold(roi, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 25, 10)
11            # ret_otsu, dst = cv.threshold(roi, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
12             gray[row:row+ch, col:col+cw] = dst
13             #计算方差和平均值,当这两个数值小于某一值时,可以认为图像为空白,做空白图像过滤
14             print(np.std(dst), np.mean(dst))
15     cv.imwrite("E:/photo/big_img_binary.jpg", gray)

 

                                                     

 

Guess you like

Origin www.cnblogs.com/lzying/p/11368294.html