[DIP] OpenCV 에서 URL로 이미지 로딩

2022. 9. 21. 20:22·Programming/DIP
728x90
728x90

OpenCV에서 URL로 이미지 로딩

request 모듈을 활용하면 간단하게 URL을 사용하여 이미지 로딩이 된다.

  1. reauest로 url을 get메서드로 가져옴.
  2. request.content 는 immutable bytes이므로 이를 bytearray로 변경: mutable
  3. 해당 bytearray 객체를 통해 ndarray 객체를 얻어냄: 일반적으로 image는 dtype=np.unit8을 사용.
  4. 해당 ndarray 객체를 image로 디코딩: cv2.imdecode() 이용.
  5. cv2.imdecode로 반환된 ndarray객체가 바로 image에 해당함.

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


다음은 예제 코드임.

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

https://ds31x.tistory.com/182

 

[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


 

'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
'Programming/DIP' 카테고리의 다른 글
  • [DIP] Local Contrast:
  • [DIP] Modulation Transfer Function and Contrast
  • [DIP] Color Space or Color Model
  • [OpenCV] cv2.cvtColor
dsaint31x
dsaint31x
    반응형
    250x250
  • dsaint31x
    Dsaint31's blog
    dsaint31x
  • 전체
    오늘
    어제
    • 분류 전체보기 (748)
      • Private Life (13)
      • Programming (56)
        • DIP (112)
        • ML (26)
      • Computer (119)
        • CE (53)
        • ETC (33)
        • CUDA (3)
        • Blog, Markdown, Latex (4)
        • Linux (9)
      • ... (351)
        • Signals and Systems (103)
        • Math (172)
        • Linear Algebra (33)
        • Physics (42)
        • 인성세미나 (1)
      • 정리필요. (54)
        • 의료기기의 이해 (6)
        • PET, MRI and so on. (1)
        • PET Study 2009 (1)
        • 방사선 장해방호 (4)
        • 방사선 생물학 (3)
        • 방사선 계측 (9)
        • 기타 방사능관련 (3)
        • 고시 (9)
        • 정리 (18)
      • RI (0)
      • 원자력,방사능 관련법 (2)
  • 블로그 메뉴

    • Math
    • Programming
    • SS
    • DIP
  • 링크

    • Convex Optimization For All
  • 공지사항

    • Test
    • PET Study 2009
    • 기타 방사능관련.
  • 인기 글

  • 태그

    fourier transform
    cv2
    linear algebra
    signal_and_system
    Probability
    SIGNAL
    Python
    Vector
    Convolution
    인허가제도
    math
    DIP
    Programming
    numpy
    opencv
    Optimization
    function
    Term
    signals_and_systems
    SS
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
dsaint31x
[DIP] OpenCV 에서 URL로 이미지 로딩
상단으로

티스토리툴바