위 이미지를 전처리 해보겠습니다.
전처리 전 크기 8679 바이트입니다.
#requirements.txt : 작업 환경
#pip freeze > requirements.txt : 자신의 환경에 있는 패키지를 모두 저장 해 둔 txt파일
pip freeze > requirements.txt
pip install -r requirments.txt
환경구성은 위 requirements.txt파일을 이용하겠습니다.
import matplotlib.pyplot as plt
import cv2
import numpy as np
import pandas as pd
import os
#본인 dir 경로가 맞지 않다면 체크해주세요.####
#현 경로 확인
os.getcwd()
#경로 변경 코드
os.chdir('path')
#경로 존재하는지 여부 체크
os.path.exist('path')
저는 image가 로드가 안되길래 경로가 잘못되어있어서 수정해주었습니다.
이미지 전처리 과정은 다음과 같습니다.
1. imread() : 이미지 읽어오기
2. cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) : 그레이 스케일
3. cv2.resize(image, (64,64), interpolation = cv2.INTER_AREA) : 리사이징
4. image/255 : 정규화
5. cv2.Gaussianblur(image,(5,5),0) : 블러처리
6. 정규화
7.imwrite('저장.png',image)
img = cv2.imread('pastry.jpg')
#img 반응이 없음. 경로 문제 발생
img
os.getcwd()
#'C:\\Users\\PJS\\Documents\\study\\전공파일\\4학년\\데이터전처리'
os.chdir('C:/Users/PJS/Documents/study/전공파일/4학년/데이터전처리/240528')
os.getcwd()
#'C:\\Users\\PJS\\Documents\\study\\전공파일\\4학년\\데이터전처리\\240528'
img = cv2.imread('pastry.jpg')
#1. 그레이 스케일
gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#2. 리사이징 보간법 사용
resized_img = cv2.resize(gray_img, (128,128), interpolation = cv2.INTER_AREA)
#3. 정규화
resized_img/255
#4. 블러 처리
blur_img = cv2.GaussianBlur(resized_img, (5,5), 0)
#5. 정규화
reg_blur_img = resized_img/255
#6. 저장
cv2.imwrite('prep_pastry.png',reg_blur_img)
#시각화
plt.figure()
plt.subplot(1,4,1)
plt.imshow(img)
plt.subplot(1,4,2)
plt.imshow(gray_img,cmap='gray')
plt.subplot(1,4,3)
plt.imshow(resized_img,cmap='gray')
plt.subplot(1,4,4)
plt.imshow(reg_blur_img,cmap='gray')
전처리 후 684 바이트로 전과 후에 비해 10배 이상 줄었습니다.
영상에서의 전처리는 아래 링크를 남겨두겠습니다.
https://pastryofjsmath.tistory.com/38
동영상 전처리
import cv2import matplotlib.pyplot as pltimport numpy as npimport osvideo_path = 'C:/Users/PJS/Documents/study/전공파일/4학년/데이터전처리/240528/image/AI.mp4'cap = cv2.VideoCapture(video_path)# 비디오가 올바르게 열렸는지 확인#if
pastryofjsmath.tistory.com
'데이터 전처리 > python' 카테고리의 다른 글
동영상 전처리 (1) | 2024.05.28 |
---|---|
이미지전처리2 - 노이즈 제거, 이미지 정규화, Gray Scale, masking, flip, 색 대비, 조명변화 (0) | 2024.05.28 |
이미지 전처리1 - RGB, HSV, resizing (0) | 2024.05.15 |
날짜,시간 데이터 전처리 (feat. Series,Dataframe) (0) | 2024.04.19 |
파이썬 범주화,결측치 처리기법 (0) | 2024.04.17 |