數學之路-python計算實戰(8)-機器視覺-映像二值化,-python實戰
二值化
hreshold
Applies a fixed-level threshold to each array element.
-
C++: double threshold(InputArray
src, OutputArray
dst, double
thresh, double
maxval, int
type)
-
Python: cv2.threshold(src, thresh, maxval, type[, dst]) → retval, dst
-
C: double cvThreshold(const CvArr*
src, CvArr*
dst, double
threshold, double
max_value, int
threshold_type)
-
| Parameters: |
- src – input array (single-channel, 8-bit or 32-bit floating point).
- dst – output array of the same size and type as src.
- thresh – threshold value.
- maxval – maximum value to use with the THRESH_BINARY andTHRESH_BINARY_INV thresholding types.
- type – thresholding type (see the details below).
|
The function applies fixed-level thresholding to a single-channel array. The function is typically used to get a bi-level (binary) image out of a grayscale image (compare() could be also used for this purpose) or for removing a noise, that is, filtering out pixels with too small or too large values. There are several types of thresholding supported by the function. They are determined by type :
THRESH_BINARY
THRESH_BINARY_INV
THRESH_TRUNC
THRESH_TOZERO
THRESH_TOZERO_INV
Also, the special value THRESH_OTSU may be combined with one of the above values. In this case, the function determines the optimal threshold value using the Otsu’s algorithm and uses it instead of the specified thresh . The function returns the computed threshold value. Currently, the Otsu’s method is implemented only for 8-bit images.
import cv2fn="test3.jpg"myimg=cv2.imread(fn)img=cv2.cvtColor(myimg,cv2.COLOR_BGR2GRAY)retval, newimg=cv2.threshold(img,40,255,cv2.THRESH_BINARY)cv2.imshow('preview',newimg)cv2.waitKey()cv2.destroyAllWindows()
本部落格所有內容是原創,如果轉載請註明來源http://blog.csdn.net/myhaspl/
自適應二值化
adaptiveThreshold函數可以二值化,也可以提取邊緣:
Python: cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst]) → dst
-
C: void cvAdaptiveThreshold(const CvArr*
src, CvArr*
dst, double
max_value, int
adaptive_method=CV_ADAPTIVE_THRESH_MEAN_C, int
threshold_type=CV_THRESH_BINARY, int
block_size=3, double
param1=5 )
| |
- src – Source 8-bit single-channel image.
- dst – Destination image of the same size and the same type as src .
- maxValue – Non-zero value assigned to the pixels for which the condition is satisfied. See the details below.
- adaptiveMethod – Adaptive thresholding algorithm to use,ADAPTIVE_THRESH_MEAN_C orADAPTIVE_THRESH_GAUSSIAN_C . See the details below.
- thresholdType – Thresholding type that must be eitherTHRESH_BINARY or THRESH_BINARY_INV .
- blockSize – Size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on.
- C – Constant subtracted from the mean or weighted mean (see the details below). Normally, it is positive but may be zero or negative as well.
- block_size參數決定局部閾值的block的大小,block很小時,如block_size=3 or 5 or 7時,表現為邊緣提取函數。當把block_size設為比較大的值時,如block_size=21、51等,便是二值化
下面是提取邊緣import cv2fn="test3.jpg"myimg=cv2.imread(fn)img=cv2.cvtColor(myimg,cv2.COLOR_BGR2GRAY)newimg=cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,5,2)cv2.imshow('preview',newimg)cv2.waitKey()cv2.destroyAllWindows()
二值化如下:
import cv2fn="test3.jpg"myimg=cv2.imread(fn)img=cv2.cvtColor(myimg,cv2.COLOR_BGR2GRAY)newimg=cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,51,2)cv2.imshow('preview',newimg)cv2.waitKey()cv2.destroyAllWindows()
|
opencv 映像二值化後計算框定地區內白點個數
這個就是簡單的遍曆映像,計算選定地區內值為255的點的個數不就可以了嗎?
已知一幅映像,對其二值化之後怎檢測兩幅映像的相似性,也就是怎知道兩幅映像的誤差值,使得二值化後
是史蒂芬·霍金的觀點比較讓人容易接受:宇宙有限而無界,只不過比地球多了幾維。比如,我們的地球就是有限而無界的。在地球上,無論從南極走到北極,還是從北極走到南極,你始終不可能找到地球的邊界,但你不能由此認為地球是無限的。實際上,我們都知道地球是有限的。地球如此,宇宙亦是如此。
怎麼理解宇宙比地球多了幾維呢?舉個例子:一個小球沿地面滾動並掉進了一個小洞中,在我們看來,小球是存在的,它還在洞裡面,因為我們人類是“三維”的;而對於一個動物來說,它得出的結論就會是:小球已經不存在了!它消失了。為什麼會得出這樣的結論呢?因為它生活在“二維”世界裡,對“三維”事件是無法清楚理解的。同樣的道理,我們人類生活在“三維”世界裡,對於比我們多幾維的宇宙,也是很難理解清楚的。這也正是對於“宇宙是什麼樣子”這個問題無法解釋清楚的原因。
1、均勻的宇宙
長期以來,人們相信地球是宇宙的中心。哥白尼把這個觀點顛倒了過來,他認為太陽才是宇宙的中心。地球和其他行星都圍繞著太陽轉動,恒星則鑲嵌在天球的最外層上。布魯諾進一步認為,宇宙沒有中心,恒星都是遙遠的太陽。
無論是托勒密的地心說還是哥白尼的日心說,都認為宇宙是有限的。教會支援宇宙有限的論點。但是,布魯諾居然敢說宇宙.是無限的,從而挑起了宇宙究竟有限還是無限的長期論戰。這場論戰並沒有因為教會燒死布魯諾而停止下來。主張宇宙有限的人說:“宇宙怎麼可能是無限的呢?”這個問題確實不容易說清楚。主張宇宙無限的人則反問:“宇宙怎麼可能是有限的呢?”這個問題同樣也不好回答。
隨著天文觀測技術的發展,人們看到,確實像布魯諾所說的那樣,恒星是遙遠的太陽。人們還進一步認識到,銀河是由無數個太陽系組成的大星系,我們的太陽系處在銀河系的邊緣,圍繞著銀河系的中心旋轉,轉速大約每秒250千米,圍繞銀心轉一圈約需2.5億年。太陽系的直徑充其量約1光年,而銀河系的直徑則高達10萬光年。銀河系由1000多億顆恒星組成,太陽系在銀河系中的地位,真像一粒砂子處在北京城中。後來又發現,我們的銀河系還與其他銀河系組成更大的星系團,星系團的直徑約為107光年(1000萬光年)。目前,望遠鏡觀測距離已達100億光年以上,在所見的範圍內,有無數的星系團存在,這些星系團不再組成更大的團,而是均勻各向同性地分布著。這就是說,在10的7次方光年的尺度以下,物質是成團分布的。衛星繞著行星轉動,行星、彗星則繞著恒星轉動,形成一個個太陽系。這些太陽系分別由一個、兩個、三個或更多個太陽以及它們的行星組成。有兩個太陽的稱為雙星系,有三個以上太陽的稱為聚星系。成千億個太陽系聚集在一起,形成銀河系,組成銀河系的恒星(太陽系)都圍繞著共同的重心——銀心轉動。無數的銀河系組成星系團,團中的各銀河系同樣也圍繞它們共同的重心轉動。但是,星系團之間,不再有成團結構。各個星系團均勻地分布著,無規則地運動著。從我們地球上往四面八方看,情況都差不多。粗略地說,星系固有點像容器中的氣體分子,均勻分布著,做著無規則運動。這就是說,在10的8次方光年(一億光年)的尺度以上,宇宙中物質的分布不再是成團的,而是均勻分布的。由於光的傳播需要時間,我們看到的距離我們一億光年的星系,實際上是那個星系一億年以前的樣子。所以,我們用望遠鏡看到的,不僅是空間距離遙遠的星系,而且是它們的過去。從望遠鏡看來,不管多遠距離的星系團,都均勻各向同性地分布著。
因而我們可以認為,宇觀尺度上(10的5次方光年以上)物質分布的均勻狀態,不是現在才有的,而是早已如此。
於是,天體物理學家提出一條規律,即所謂宇宙學原理。這條......餘下全文>>