99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀利用Python和OpenCV庫將URL轉(zhuǎn)換為OpenCV格式的方法
利用Python和OpenCV庫將URL轉(zhuǎn)換為OpenCV格式的方法
2017-10-04
收藏

利用Python和OpenCV庫將URL轉(zhuǎn)換為OpenCV格式的方法

過去幾個月,有些PyImageSearch讀者電郵問我:“如何獲取URL指向的圖片并將其轉(zhuǎn)換成OpenCV格式(不用將其寫入磁盤再讀回)”。這篇文章我將展示一下怎么實現(xiàn)這個功能。
額外的,我們也會看到如何利用scikit-image從URL下載一幅圖像。當然前行之路也會有一個常見的錯誤,它可能讓你跌個跟頭。
繼續(xù)往下閱讀,學習如何利用利用Python和OpenCV將URL轉(zhuǎn)換為圖像
方法1:OpenCV、NumPy、urllib
第一個方法:我們使用OpenCV、NumPy、urllib庫從URL獲取圖像,并將其轉(zhuǎn)換為圖像。打開并新建一個文件,取名url_to_image.py,我們開始吧:
 
# import the necessary packages
import numpy as np
import urllib
import cv2
 
# METHOD #1: OpenCV, NumPy, and urllib
def url_to_image(url):
  # download the image, convert it to a NumPy array, and then read
  # it into OpenCV format
  resp = urllib.urlopen(url)
  image = np.asarray(bytearray(resp.read()), dtype="uint8")
  image = cv2.imdecode(image, cv2.IMREAD_COLOR)
 
  # return the image
  return image

首先要做的就是導入我們必需的包。我們將使用NumPy轉(zhuǎn)換下載的字節(jié)序為NumPy數(shù)組,使用urllib來執(zhí)行實際的網(wǎng)絡請求,使用cv2來綁定OpenCV接口。

在第7行,我們定義了我們的url_to_image函數(shù)。這個函數(shù)帶一個url參數(shù),也就是我們想要下載的圖像地址。
接下來,在第10行,我們使用urllib庫來打開這個圖像鏈接。11行則將這個下載下來的字節(jié)序轉(zhuǎn)換為NumPy數(shù)組。
至此,NumPy數(shù)組還是一個1維數(shù)組(也就是一個長長的像素鏈表)。為了將其轉(zhuǎn)換為2維格式,假設每個像素3個通道(意即分別為紅,綠,藍通道),在12行我們使用cv.imdecode函數(shù)。最后,在15行我們返回解碼出來的圖像給調(diào)用函數(shù)。
一切就緒,該到讓它工作的時候了:
 
# initialize the list of image URLs to download
urls = [
  "http://www.pyimagesearch.com/wp-content/uploads/2015/01/opencv_logo.png",
  "http://www.pyimagesearch.com/wp-content/uploads/2015/01/google_logo.png",
  "http://www.pyimagesearch.com/wp-content/uploads/2014/12/adrian_face_detection_sidebar.png",
]
 
# loop over the image URLs
for url in urls:
  # download the image URL and display it
  print "downloading %s" % (url)
  image = url_to_image(url)
  cv2.imshow("Image", image)
  cv2.waitKey(0)
3-5行定義了我們將要下載和轉(zhuǎn)換為OpenCV格式的圖像地址列表。
第9行我們遍歷這個列表,13行則調(diào)用url_to_image函數(shù),然后在14行和15行將獲取的圖像顯示到屏幕上。到此呢,我們就可以像正常情況下一樣,使用OpenCV來操作和處理這些圖像了。
眼見為實,打開終端,執(zhí)行如下指令:
代碼如下:
$ python url_to_image.py

如果一切順利的話,你會看到OpenCV的logo:

2015327144145476.jpg (690×553)

圖1:從URL下載OpenCV logo并轉(zhuǎn)換為OpenCV格式
接下來是Google的logo:

2015327144648394.jpg (690×296)

圖2:從URL下載Gooogle并轉(zhuǎn)換為OpenCV格式
這里也有在我書中驗證人臉檢測的例子,《Practical Python and OpenCV》:

2015327145507830.jpg (690×292)

圖3:轉(zhuǎn)換一個URL圖像為OpenCV格式
現(xiàn)在,我們來看另一種獲取圖像并轉(zhuǎn)換為OpenCV格式的方法。
方法2:使用scikit-image
第二種方法假定你已經(jīng)在你計算機上安裝好了scikit-image庫。讓我們看看怎樣采用scikit-image從URL獲取圖像并將其轉(zhuǎn)換為OpenCV格式:
# METHOD #2: scikit-image
from skimage import io
 
# loop over the image URLs
for url in urls:
  # download the image using scikit-image
  print "downloading %s" % (url)
  image = io.imread(url)
  cv2.imshow("Incorrect", image)
  cv2.imshow("Correct", cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
  cv2.waitKey(0)
scikit-image庫中做得很漂亮的一點是:io子庫中的imread函數(shù)能夠區(qū)分圖像路徑到底在磁盤上還是一個URL(第9行)。
盡管這樣,這里有一個很嚴重的錯誤可能讓你跌一個跟頭!
OpenCV以BGR順序表達一幅圖像,然而scikit-image則是RGB順序。如果你使用scikit-iamge的imread函數(shù),而且還想在下載完成后使用OpenCV的函數(shù),那么你要小心了。如41行所述,你需要將圖像從RBG轉(zhuǎn)換為BGR。
如果你沒有這一步,那么你可能得到錯誤的結(jié)果:

2015327145042117.jpg (690×368)

圖4:在用scikit-image時,需要特別注意將RGB轉(zhuǎn)換為BGR。左邊的圖像就是不正確的RGB順序,右邊的則是將RGB轉(zhuǎn)換為BGR,所以能正常顯示。
看看Google的logo就更明顯了

2015327145209903.jpg (690×147)

圖5:順序很重要。確保將RGB轉(zhuǎn)換為BGR,否則就留下了一個很難發(fā)現(xiàn)的bug。
到此為止,你明白了吧!這兩種方法分別使用Python、OpenCV、urllib,和scikit-image來將URL指向的圖片轉(zhuǎn)換為圖像。
總結(jié)
本文中,我們學會了如何從URL獲取圖像,且使用Python和OpenCV將其轉(zhuǎn)換為OpenCV格式。
第一種方法使用urllib包獲取圖像,使用Numpy轉(zhuǎn)換為數(shù)組,最后使用OpenCV重新構(gòu)建數(shù)組產(chǎn)生我們的圖像。
第二種方式使用scikit-image中的io.imread函數(shù)。
所以,哪種更好呢?
這完全取決于你的安裝。
如果你已經(jīng)安裝scikit-image,那么我可能就用io.imread(只是不要忘記如果要用OpenCV函數(shù)的話,要將RGB轉(zhuǎn)換為BGR)。
如果你沒有安裝scikit-image,那么url_to_image就是手邊現(xiàn)成的工具。具體細節(jié)參考本文開始處。
我很快會在Github上將這個函數(shù)添加到imutils庫中。

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }