<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          【OpenCV入門】手把手教你圖片預(yù)處理

          共 2714字,需瀏覽 6分鐘

           ·

          2021-04-13 13:19

          子豪 發(fā)自 凹非寺 
          量子位 報道 | 公眾號 QbitAI

          OpenCV是一個跨平臺計算機視覺和機器學(xué)習(xí)算法庫。它不僅能用來實現(xiàn)各種復(fù)雜的算法,還能夠?qū)D像進(jìn)行預(yù)處理包括圖像的平移、旋轉(zhuǎn)、縮放、翻轉(zhuǎn)、裁剪。希望把這些知識分享給初學(xué)者。

          圖像平移

          我們使用OpenCV提供的仿射變換函數(shù)cv.warpAffine()沿x和y軸移動圖像。

          Step1. 調(diào)用一個函數(shù)cv.warpAffine()。

          Step2. 創(chuàng)建一個平移矩陣,這一步需要借助NumPy進(jìn)行。

          Step3. 將img(需要變換的圖像)、transMAT(平移矩陣)和Dimensions(維度)代入仿射變換函數(shù)cv.warpAffine(),輸入x和y以確定平移多少。

          向左、向上移動,則x、y設(shè)為負(fù)數(shù),反之則為正數(shù)。

          Step4. 采用cv.imshow()函數(shù)顯示圖像。

          ## Translation
          def translation(img,x,y):
              tranMAT = np.float32([[1,0,x],[0,1,y]])
              dimensions = (img.shape[1],img.shape[0])
              return cv.warpAffine(img,transMAT,dimensions)
          translated = translation(img,-100,100)
          cv.imshow("Translated",translated)
          cv.waitKey(0)

          圖像旋轉(zhuǎn)

          在OpenCV中旋轉(zhuǎn)圖像,可以將任何點用作旋轉(zhuǎn)的中心,同樣使用cv.warpAffine()函數(shù)以及和上面相同的參數(shù)。但是旋轉(zhuǎn)矩陣與圖像平移是不同的。

          Step1. 創(chuàng)建一個旋轉(zhuǎn)函數(shù),定義圖像img、旋轉(zhuǎn)角度angle和旋轉(zhuǎn)點rotPoint。

          Step2. 利用cv.getRotationMatrix2D()(矩陣旋轉(zhuǎn)與縮放)創(chuàng)建一個旋轉(zhuǎn)矩陣rotMAT,其中包括旋轉(zhuǎn)點、旋轉(zhuǎn)角度和比例因子。如果不需要比例因子,則將其設(shè)為1.0。

          Step3. 輸入旋轉(zhuǎn)角度、旋轉(zhuǎn)點對應(yīng)數(shù)值。(如不需要設(shè)置旋轉(zhuǎn)點,則在Step1中設(shè)為空值None)

          ## Rotation
          def rotate(img,angle,rotPoint=None):
              (height,width) = img,shape[:2]

              if rotPoint is None:
                 rotPoint = (width//2,height//2)
              rotMAT = cv.getRotationMatrix2D(rotPoint,angle,1.0)
              dimension = (width,height)
              return cv.warpAffine(img,rotMat,dimension)

          rotated = rotated(img,20)
          cv.imshow("Rotated",rotated)
          cv.waitKey(0)



          圖片縮放

          在OpenCV中需要用到函數(shù)cv.resize(img, (500, 500), interpolation=cv.INTER_LINEAR)。

          根據(jù)縮小或放大圖像的需要,參數(shù)指定插值方法可以選擇cv.INTER_AREA(區(qū)域插值)、cv.INTER_CUBIC(三次樣條插值)、cv.INTER_LINEAR(線性插值)。

          如需放大圖像,可以采用<> INTER_LINEAR(效率較高)或INTER_CUBIC(效率較低);
          如需縮小圖像,可以采用<> INTER_AREA。

          ## Resize
          resized = cv.resize(img(500,500),interpolation=cv.INTER_LINEAR)
          cv.imshow("resized",resized)
          cv.waitKey(0)


          圖片翻轉(zhuǎn)

          使用OpenCV中的cv.flip()函數(shù),該函數(shù)支持圖像的翻轉(zhuǎn)(垂直翻轉(zhuǎn)、水平翻轉(zhuǎn),以及同時翻轉(zhuǎn)均可)。

          cv.flip(img,flipcode)翻轉(zhuǎn)模式有三種:0為垂直翻轉(zhuǎn),1為水平翻轉(zhuǎn),-1 為兩個方向同時翻轉(zhuǎn)。

          ## Flipping
          flipped = cv.flip(img,0)
          cv.imshow("Flipped",flipped)
          cv.waitKey(0)


          圖像裁剪

          運用Cropped = image[a1:a2,b1:b2] 裁剪圖像img得到一個矩形,高度從a1到a2,寬度從b1到b2。

          ## Cropping
          cropped = img[100:200,100:200]
          cv.imshow("Cropped",cropped)
          cv.waitKey(0)


          除此之外,OpenCV還能用于空間顏色轉(zhuǎn)換、物體跟蹤、圖像分割、輪廓提取、結(jié)構(gòu)分析、人臉識別等,功能十分強大。

          Raoof Naushad不僅發(fā)布了這篇教程,還分享了用于基本和高級圖像處理的OpenCV速查表,感興趣的朋友可以通過下方鏈接查看。

          原文鏈接:
          https://medium.com/analytics-vidhya/image-transformations-opencv-2021-de3ca97373ed

           End 


          聲明:部分內(nèi)容來源于網(wǎng)絡(luò),僅供讀者學(xué)術(shù)交流之目的。文章版權(quán)歸原作者所有。如有不妥,請聯(lián)系刪除。


          點「在看」的人都變好看了哦!
          瀏覽 56
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  淫淫五月天婷婷 | 久久久久7777 | 亚洲第一页中文 | 婷婷亚洲综合 | 青草青视频 |