python OpenCV image pyramid example analysis
May 11, 2023 pm 08:40 PM1.圖像金字塔理論基礎
圖像金字塔是圖像多尺度表達的一種,是一種以多分辨率來解釋圖像的有效但概念簡單的結構。一幅圖像的金字塔是一系列以金字塔形狀排列的分辨率逐步降低,且來源于同一張原始圖的圖像集合。其通過梯次向下采樣獲得,直到達到某個終止條件才停止采樣。我們將一層一層的圖像比喻成金字塔,層級越高,則圖像越小,分辨率越低。
那我們?yōu)槭裁匆鰣D像金字塔呢?這就是因為改變像素大小有時候并不會改變它的特征,比方說給你看1000萬像素的圖片,你能知道里面有個人,給你看十萬像素的,你也能知道里面有個人,但是對計算機而言,處理十萬像素可比處理1000萬像素要容易太多了。就是為了讓計算機識別特征這個事變得更加簡便,后期我們也會講到識別特征這個實戰(zhàn)項目,大概就是說比如你在高中打籃球,遠遠的看見你班主任出來了,你們離500米,你依然可以根據(jù)特征取認識出來你的老師,和你班主任離你2米的時候一樣。
也就是說圖像金字塔式同一個圖像不同分辨率子圖的集合。在這里我們可以舉一個例子就是原始圖像是一個400400的圖像,那么向上取就可以是200200的一張圖像然后100*100,這樣分辨率降低,但是始終是同一個圖像。
那么從第i層到第i+1層,他具體是怎么做的呢?
1.計算輸入圖像減少的分辨率的近似值。這可以通過對輸入進行濾波并以2為步長進行抽樣(即子抽樣)。可以采用的濾波操作有很多,如鄰域平均(可生成平均值金字塔),高斯低通濾波器(可生成高斯金字塔),或者不進行濾波,生成子抽樣金字塔。生成近似值的質量是所選濾波器的函數(shù)。沒有濾波器,在金字塔的上一層中的混淆變得很顯著,子抽樣點對所選取的區(qū)域沒有很好的代表性。
2.對上一步的輸出進行內(nèi)插(因子仍為2)并進行過濾。這將生成與輸入等分辨率的預測圖像。由于在步驟1的輸出像素之間進行插值運算,所以插入濾波器決定了預測值與步驟1的輸入之間的近似程度。如果插入濾波器被忽略了,則預測值將是步驟1輸出的內(nèi)插形式,復制像素的塊效應將變得很明顯。
3.計算步驟2的預測值和步驟1的輸入之間的差異。以j級預測殘差進行標識的這個差異將用于原始圖像的重建。在沒有量化差異的情況下,預測殘差金字塔可以用于生成相應的近似金字塔(包括原始圖像),而沒有誤差。
執(zhí)行上述過程P次將產(chǎn)生密切相關的P+1級近似值和預測殘差金字塔。j-1級近似值的輸出用于提供近似值金字塔,而j級預測殘差的輸出放在預測殘差金字塔中。如果不需要預測殘差金字塔,則步驟2和3、內(nèi)插器、插入濾波器以及圖中的加法器都可以省略。
這里面下取樣表示圖像的縮小。向上取樣表示的是圖像的增大。
1.對圖像Gi進行高斯核卷積。
高斯核卷積就是我們所說的高斯濾波操縱,我們之前就已經(jīng)講過,使用一個高斯卷積核,讓臨近的像素值所占的權重較大,然后和目標圖像做相關操作。
2.刪除所有的偶數(shù)行和列。
原始圖像 M * N 處理結果 M/2 * N/2。每次處理后,結果圖像是原來的1/4。這個操作被稱為Octave。重復執(zhí)行該過程,構造圖像金字塔。直至圖像不能繼續(xù)下分為止。這個過程會丟失圖像信息。
而向上取樣恰恰和向下取樣相反,是在原始圖像上,**在每個方向上擴大為原來的2倍,新增的行和列以0填充。使用與“向下采用”同樣的卷積核乘以4,獲取“新增像素”的新值。**經(jīng)過向上取樣后的圖像會模糊。
向上采樣、向下采樣不是互逆操作。經(jīng)過兩種操作后,無法恢復原有圖像。
2.向下取樣函數(shù)及使用
圖像金字塔向下取樣函數(shù):
dst=cv2.pyrDown(src)
import cv2 import numpy as np o=cv2.imread("image\\man.bmp") r1=cv2.pyrDown(o) r2=cv2.pyrDown(r1) r3=cv2.pyrDown(r2) cv2.imshow("original",o) cv2.imshow("PyrDown1",r1) cv2.imshow("PyrDown2",r2) cv2.imshow("PyrDown3",r3) cv2.waitKey() cv2.destroyAllWindows()
這里我們對圖像做三次向下取樣,結果為:
向下取樣會丟失信息?。?!
3.向上取樣函數(shù)及使用
圖像金字塔向上取樣函數(shù):
dst=cv2.pyrUp(src)
這里代碼我們就不做介紹了。
直接看一下我們的結果:
4.采樣可逆性研究
這里我們具體看一下圖像進行向下取樣然后進行向上取樣操作后,是不是一致的。還有就是圖像進行向上取樣然后向下取樣后,是不是一致的呢?這里我們以小女孩的圖像做一下研究。
首先我們來分析一下:當圖像做向下取樣一次之后,圖像由MN變成了M/2N/2,然后再次經(jīng)過向上取樣之后又變成了M*N。那么可以證明對于圖片的size是不發(fā)生變化的。
import cv2 o=cv2.imread("image\\girl.bmp") down=cv2.pyrDown(o) up=cv2.pyrUp(down) cv2.imshow("original",o) cv2.imshow("down",down) cv2.imshow("up",up) cv2.waitKey() cv2.destroyAllWindows()
那么我們來看一下到底發(fā)生了什么變化呢?
這里可以很清晰的看出來小女孩的照片變得模糊了,那么是為什么呢?因為我們上面說到了就是當圖像變小的時候,那么就會損失一些信息,再次放大之后,由于卷積核變大了,那么圖像會變模糊。所以不會和原始圖像保持一致。
然后我們再來看一下先進行向上取樣操作,然后進行向下取樣操作會是什么樣子呢?由于圖像變大太大所以我們省去中間圖像向上取樣的圖片。
那么我們用肉眼也不是特別容易發(fā)現(xiàn)差異,那么我們用圖像減法去看一下。
import cv2 o=cv2.imread("image\\girl.bmp") up=cv2.pyrUp(o) down=cv2.pyrDown(up) diff=down-o #構造diff圖像,查看down與o的區(qū)別 cv2.imshow("difference",diff) cv2.waitKey() cv2.destroyAllWindows()
5.拉普拉斯金字塔
我們先來看一下拉普拉斯金字塔是一個什么東西:
Li = Gi - PyrUp(PyrDown(Gi))
我們根據(jù)他這個式子可以知道,拉普拉斯金字塔就是使用原始圖像減去圖像向下取樣然后向上取樣的這樣一個過程。
展示在圖像當中就是:
核心函數(shù)就是:
od=cv2.pyrDown(o) odu=cv2.pyrUp(od) lapPyr=o-odu
6.圖像輪廓介紹
首先我們先要說明這樣一個事情就是彼圖像輪廓和圖像的邊緣是不一樣的,邊緣是零零散散的,但是輪廓是一個整體。
邊緣檢測能夠測出邊緣,但是邊緣是不連續(xù)的。將邊緣連接為一個整體,構成輪廓。
注意:
對象是二值圖像。所以需要預先進行閾值分割或者邊緣檢測處理。查找輪廓需要更改原始圖像。因此,通常使用原始圖像的一份拷貝操作。在OpenCV中,是從黑色背景中查找白色對象。因此,對象必須是白色的,背景必須是黑色的。
對于圖像輪廓的檢測需要的函數(shù)是:
cv2.findContours( )和cv2.drawContours( )
查找圖像輪廓的函數(shù)是cv2.findContours(),通過cv2.drawContours()將查找到的輪廓繪制到圖像上。
對于cv2.findContours( )函數(shù):
image, contours, hierarchy = cv2.findContours( image, mode, method)
這里需要注意在最新的版本中,查找輪廓中的返回函數(shù)只有兩個即可:
contours, hierarchy = cv2.findContours( image, mode, method)
contours ,輪廓
hierarchy ,圖像的拓撲信息(輪廓層次)
image ,原始圖像
mode ,輪廓檢索模式
method ,輪廓的近似方法
這里我們需要介紹mode:也就是輪廓檢索模式:
cv2.RETR_EXTERNAL :表示只檢測外輪廓
cv2.RETR_LIST :檢測的輪廓不建立等級關系
cv2.RETR_CCOMP :建立兩個等級的輪廓,上面的一層為外邊界,里面的一層為內(nèi)孔的邊 界信息。如果內(nèi)孔內(nèi)還有一
個連通物體,這個物體的邊界也在頂層
cv2.RETR_TREE :建立一個等級樹結構的輪廓。
然后我們介紹一下method ,輪廓的近似方法:
cv2.CHAIN_APPROX_NONE :存儲所有的輪廓點,相鄰的兩個點的像素位置差不超過1, 即max(abs(x1-x2),abs(y2-y1))==1
cv2.CHAIN_APPROX_SIMPLE:壓縮水平方向,垂直方向,對角線方向的元素, 只保留該方向的終點坐標,例如一個矩形輪廓只需4個點來保存輪廓信息
cv2.CHAIN_APPROX_TC89_L1:使用teh-Chinl chain 近似算法
cv2.CHAIN_APPROX_TC89_KCOS:使用teh-Chinl chain 近似算法
比如對一個矩形做輪廓檢測,使用cv2.CHAIN_APPROX_NONE和cv2.CHAIN_APPROX_SIMPLE得結果是這樣:
可以看出后者省出來很多計算空間。
對于cv2.drawContours( ):
r=cv2.drawContours(o, contours, contourIdx, color[, thickness])
r :目標圖像,直接修改目標的像素點,實現(xiàn)繪制。
o :原始圖像
contours :需要繪制的邊緣數(shù)組。
contourIdx :需要繪制的邊緣索引,如果全部繪制則為 -1。
color :繪制的顏色,為 BGR 格式的 Scalar 。
thickness :可選,繪制的密度,即描繪輪廓時所用的畫筆粗細。
import cv2 import numpy as np o = cv2.imread('image\\boyun.png') gray = cv2.cvtColor(o,cv2.COLOR_BGR2GRAY) ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY) image,contours, hierarchy =cv2.findContours(binary,cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) co=o.copy() r=cv2.drawContours(co,contours,-1,(0,0,255),1) cv2.imshow("original",o) cv2.imshow("result",r) cv2.waitKey() cv2.destroyAllWindows()
對于多個輪廓,我們也可以指定畫哪一個輪廓。
gray = cv2.cvtColor(o,cv2.COLOR_BGR2GRAY) ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY) image,contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) co=o.copy() r=cv2.drawContours(co,contours,0,(0,0,255),6)
如果設置成-1,那么就是全部顯示?。?!
輪廓近似
當輪廓有毛刺的時候,我們希望能夠做輪廓近似,將毛刺去掉,大體思想是將曲線用直線代替,但是有個長度的閾值需要自己設定。
我們還可以做額外的操作,比如外接矩形,外接圓,外界橢圓等等。
img = cv2.imread('contours.png') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) binary, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) cnt = contours[0] x,y,w,h = cv2.boundingRect(cnt) img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2) cv_show(img,'img') area = cv2.contourArea(cnt) x, y, w, h = cv2.boundingRect(cnt) rect_area = w * h extent = float(area) / rect_area print ('輪廓面積與邊界矩形比',extent) 外接圓 (x,y),radius = cv2.minEnclosingCircle(cnt) center = (int(x),int(y)) radius = int(radius) img = cv2.circle(img,center,radius,(0,255,0),2) cv_show(img,'img')
模板匹配和卷積原理很像,模板在原圖像上從原點開始滑動,計算模板與(圖像被模板覆蓋的地方)的差別程度,這個差別程度的計算方法在opencv里有六種,然后將每次計算的結果放入一個矩陣里,作為結果輸出。假如原圖形是AXB大小,而模板是axb大小,則輸出結果的矩陣是(A-a+1)x(B-b+1)。
TM_SQDIFF:計算平方不同,計算出來的值越小,越相關
TM_CCORR:計算相關性,計算出來的值越大,越相關
TM_CCOEFF:計算相關系數(shù),計算出來的值越大,越相關
TM_SQDIFF_NORMED:計算歸一化平方不同,計算出來的值越接近0,越相關
TM_CCORR_NORMED:計算歸一化相關性,計算出來的值越接近1,越相關
TM_CCOEFF_NORMED:計算歸一化相關系數(shù),計算出來的值越接近1,越相關
The above is the detailed content of python OpenCV image pyramid example analysis. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

User voice input is captured and sent to the PHP backend through the MediaRecorder API of the front-end JavaScript; 2. PHP saves the audio as a temporary file and calls STTAPI (such as Google or Baidu voice recognition) to convert it into text; 3. PHP sends the text to an AI service (such as OpenAIGPT) to obtain intelligent reply; 4. PHP then calls TTSAPI (such as Baidu or Google voice synthesis) to convert the reply to a voice file; 5. PHP streams the voice file back to the front-end to play, completing interaction. The entire process is dominated by PHP to ensure seamless connection between all links.

To realize text error correction and syntax optimization with AI, you need to follow the following steps: 1. Select a suitable AI model or API, such as Baidu, Tencent API or open source NLP library; 2. Call the API through PHP's curl or Guzzle and process the return results; 3. Display error correction information in the application and allow users to choose whether to adopt it; 4. Use php-l and PHP_CodeSniffer for syntax detection and code optimization; 5. Continuously collect feedback and update the model or rules to improve the effect. When choosing AIAPI, focus on evaluating accuracy, response speed, price and support for PHP. Code optimization should follow PSR specifications, use cache reasonably, avoid circular queries, review code regularly, and use X

When choosing a suitable PHP framework, you need to consider comprehensively according to project needs: Laravel is suitable for rapid development and provides EloquentORM and Blade template engines, which are convenient for database operation and dynamic form rendering; Symfony is more flexible and suitable for complex systems; CodeIgniter is lightweight and suitable for simple applications with high performance requirements. 2. To ensure the accuracy of AI models, we need to start with high-quality data training, reasonable selection of evaluation indicators (such as accuracy, recall, F1 value), regular performance evaluation and model tuning, and ensure code quality through unit testing and integration testing, while continuously monitoring the input data to prevent data drift. 3. Many measures are required to protect user privacy: encrypt and store sensitive data (such as AES

Use Seaborn's jointplot to quickly visualize the relationship and distribution between two variables; 2. The basic scatter plot is implemented by sns.jointplot(data=tips,x="total_bill",y="tip",kind="scatter"), the center is a scatter plot, and the histogram is displayed on the upper and lower and right sides; 3. Add regression lines and density information to a kind="reg", and combine marginal_kws to set the edge plot style; 4. When the data volume is large, it is recommended to use "hex"

The core idea of PHP combining AI for video content analysis is to let PHP serve as the backend "glue", first upload video to cloud storage, and then call AI services (such as Google CloudVideoAI, etc.) for asynchronous analysis; 2. PHP parses the JSON results, extract people, objects, scenes, voice and other information to generate intelligent tags and store them in the database; 3. The advantage is to use PHP's mature web ecosystem to quickly integrate AI capabilities, which is suitable for projects with existing PHP systems to efficiently implement; 4. Common challenges include large file processing (directly transmitted to cloud storage with pre-signed URLs), asynchronous tasks (introducing message queues), cost control (on-demand analysis, budget monitoring) and result optimization (label standardization); 5. Smart tags significantly improve visual

To integrate AI sentiment computing technology into PHP applications, the core is to use cloud services AIAPI (such as Google, AWS, and Azure) for sentiment analysis, send text through HTTP requests and parse returned JSON results, and store emotional data into the database, thereby realizing automated processing and data insights of user feedback. The specific steps include: 1. Select a suitable AI sentiment analysis API, considering accuracy, cost, language support and integration complexity; 2. Use Guzzle or curl to send requests, store sentiment scores, labels, and intensity information; 3. Build a visual dashboard to support priority sorting, trend analysis, product iteration direction and user segmentation; 4. Respond to technical challenges, such as API call restrictions and numbers

The core of PHP's development of AI text summary is to call external AI service APIs (such as OpenAI, HuggingFace) as a coordinator to realize text preprocessing, API requests, response analysis and result display; 2. The limitation is that the computing performance is weak and the AI ecosystem is weak. The response strategy is to leverage APIs, service decoupling and asynchronous processing; 3. Model selection needs to weigh summary quality, cost, delay, concurrency, data privacy, and abstract models such as GPT or BART/T5 are recommended; 4. Performance optimization includes cache, asynchronous queues, batch processing and nearby area selection. Error processing needs to cover current limit retry, network timeout, key security, input verification and logging to ensure the stable and efficient operation of the system.

String lists can be merged with join() method, such as ''.join(words) to get "HelloworldfromPython"; 2. Number lists must be converted to strings with map(str, numbers) or [str(x)forxinnumbers] before joining; 3. Any type list can be directly converted to strings with brackets and quotes, suitable for debugging; 4. Custom formats can be implemented by generator expressions combined with join(), such as '|'.join(f"[{item}]"foriteminitems) output"[a]|[
