728x90
728x90
OpenCV에서 URL로 이미지 로딩
request 모듈을 활용하면 간단하게 URL을 사용하여 이미지 로딩이 된다.
reauest로 url을get메서드로 가져옴.request.content는 immutablebytes이므로 이를bytearray로 변경: mutable- 해당
bytearray객체를 통해 ndarray 객체를 얻어냄: 일반적으로 image는dtype=np.unit8을 사용. - 해당 ndarray 객체를 image로 디코딩:
cv2.imdecode()이용. cv2.imdecode로 반환된 ndarray객체가 바로 image에 해당함.
URL에 대한 개념이 부족하면 다음 접은 글을 참고할 것
다음은 예제 코드임.
import requests
import cv2
import numpy as np
import matplotlib.pyplot as plt
url = 'https://www.gachon.ac.kr/Web-home/_UI/images/gachon.jpg'
image_ndarray = np.asarray(bytearray(requests.get(url).content), dtype=np.uint8)
img = cv2.imdecode(image_ndarray, cv2.IMREAD_COLOR)
plt.imshow(img[...,::-1])
# Grayscale로 load
# img = cv2.imdecode(image_ndarray, cv2.IMREAD_GRAYSCALE)
# plt.imshow(img, cmap='gray')
즉, colab등에서 DIP코딩시 굳이 google drive를 마운트 하지 않더라도 URL에서 읽어서 처리해도 됨.

같이보면 좋은 자료들
2023.04.09 - [Computer/CE] - [CE] URL, URI and UNC
[CE] URL, URI and UNC
URI or URL URI는 Uniform Resource Identifier의 abbreviation 이고, URL은 Uniform Resource Locator의 abbreviation임. 인터넷 또는 WAN 상에서 특정 resource(HTML, 이미지, 동영상 등을 resource라고 지칭함)에 접근할 목적으로
dsaint31.tistory.com
[Python] bytes and bytearray: Binary Data for Python
Python에서 bytes와 bytearray는 binary data를 byte 단위 (1byte = 8bit)로 다루는데 사용되는 Data Type임. bytes: bytes 는 immutable byte sequence 로서 일종의 byte로 구성된 tuple에 해당 함. byte literal은 다음과 같이 b라
ds31x.tistory.com
728x90
'Programming > DIP' 카테고리의 다른 글
| [DIP] Local Contrast: (2) | 2022.09.26 |
|---|---|
| [DIP] Modulation Transfer Function and Contrast (1) | 2022.09.26 |
| [DIP] Color Space or Color Model (1) | 2022.09.21 |
| [OpenCV] cv2.cvtColor (0) | 2022.07.14 |
| [Math] Hessian: Summary (0) | 2022.06.05 |