程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[OpenCV-Python-Course Study (Jia)] OpenCV3.3 Course Study Notes: Image Binarization Method (inRange, threshold), Image Thresholding

編輯:Python

1. Image binarization - inRange()

The syntax of inRange is as follows

dst = inRange(src, upper, lower)

  • src is the image data
  • upper can be a single value or an array of the same size as src, representing the highest boundary of the filter range
  • lower can be a single value or an array of the same size as src, representing the lowest boundary of the filter range
  • dst can either be written inside parentheses or received as the return value of a function

How the API works

If the value of the pixel in the image is within the range defined by upper and lower, it will return 255 at the location of the pixel; otherwise, it will return 0.

Second, extract a specific color - inRange()

The first part talked about the working principle of inRange(), and now we talk about its application case - extracting a specific color from an image, such as extracting the green color in the image.

We need to know the respective value ranges of the three channels of green. Some articles on the Internet have summarized the value ranges of each channel of each color, for example[OpenCV]HSV Color Recognition-HSV Basic Color Component Range- Tencent Cloud Developer Community.

We can find that basically in the color space HSV, such a range of values ​​is meaningful; so we also need to use cvtColor() to convert other color spaces into HSV color space.

From the table we can see that the minimum bound for green is [35, 43, 46] and the maximum bound is [77, 255, 255].

So we can write dst = inRange(src, np.array([35, 43, 46]), np.array([77, 255, 255])) to extract the green area in the image src, and thenAfter executing src[dst==0] = 0, the resulting image is black except for the green area.

Three, image threshold processing - threshold()

Refer to the official API documentation: OpenCV: Miscellaneous Image Transformations-Threshold()

Image thresholding is a method in image binarization. Image binarization does not mean that the final pixel values ​​​​are 0 and 1 (if it is really 0 and 1, the color is completely black, which is obviously not the case).

Gives the syntax format of the API directly.

cv.threshold(src, thresh, maxval, type[, dst]) ->retval, dst

  • src is an image; although a multi-channel color image can be passed in (thresholding each channel separately at this time), it is not recommended that grayscale images are the most suitable for thresholding.
  • thresh is the threshold; if the pixel value is greater than the threshold, take maxval, otherwise take 0; if the flag (cv.THRESH_OTSU and cv.THRESH_TRIANGLE) to automatically obtain the threshold is set in type, then thresh is generally set to 255, then nohave any effect.
  • maxval is the maximum padding value; there are only two pixel values ​​in the final image. If the type is cv.THRESH_BINARY and cv.THRESH_BINARY_INV, maxval will be used. The small padding value is 0, and the large padding value ismaxval, generally set to 255 (grayscale -> white).
  • There are many types of
  • types, which are used to specify the type of thresholding; if it is binary thresholding, it is cv.THRESH_BINARY and cv.THRESH_BINARY_INV, if it is truncation thresholding, it is cv.THRESH_TRUNC, and if it is zero-trending thresholding, it iscv.THRESH_TOZERO and cv.THRESH_TOZERO_INV; in addition, you can also set whether to automatically obtain the threshold, the corresponding flags are cv.THRESH_OTSU and cv.THRESH_TRIANGLE, corresponding to two algorithms for automatically obtaining the threshold; the last two types mentioned above can only be selectedOne, and only one of the first five types can be selected. A correct example is type=cv.THRESH_BINARY | cv.THRESH_OTSU. Of course, the vertical bar sign can also be replaced with a plus sign.
  • retval is the return value. If the threshold is not obtained automatically, the final retval is equal to the set threshold, and if the threshold is obtained automatically, the threshold obtained using the automatic threshold algorithm is returned.
  • dst is also the image result after thresholding.

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved