메뉴 건너뛰기

XEDITION

study

https://amehta.github.io/posts/2019/09/create-and-apply-simple-filters-to-an-image-using-opencv-and-python/


In this blog post, I will show you how we can enhance our images using colored filters and add border backgrounds.

Original Original w/ color filter Original w/ color border
Blagaj Blagaj Blagaj

Let’s get started.

1. Simple color filter

Lets start with first creating a color filter - an image with just a single color. This is as simple as creating a 3-D array and filling each pixel with the same value. In the code below I create an array of the same size (682,512,3) as that of the target image and fill each pixel with the color red (0,0,255).

red_img  = np.full((682,512,3), (0,0,255), np.uint8)

Next, we read the target image using cv2.imread() and add the color filter (created above) to the image. This can be done using the function cv2.add()

image = 'blagaj_resized.jpg'
target_img = cv2.imread(image)
fused_img  = cv2.add(target_img,red_img)

The problem with the above approach is that it does a simple addition. Imagine looking at a picture through a red colored paper, and mostl likely you won’t see anything. Instead we are trying to create a a colored glass and look at the picture through it. This transluceny can be achieved by using a different function cv2.addWeighted()

fused_img  = cv2.addWeighted(target_img, 0.8, red_img, 0.2, 0)

In the code above, we add the target image and the colored image in the ratio 8:2 to get the effect as seen above. Feel free to play with other weights and colors.

Full program listing

Here is the full program listing

import cv2
import numpy as np

# read the target file
image = 'blagaj_resized.jpg'
target_img = cv2.imread(image)

# create an image with a single color (here: red)
red_img  = np.full((682,512,3), (0,0,255), np.uint8)

# add the filter  with a weight factor of 20% to the target image
fused_img  = cv2.addWeighted(target_img, 0.8, red_img, 0.2, 0)

cv2.imshow("Red Filter", fused_img)
cv2.waitKey(0)

cv2.imwrite('blagaj_red_filter.jpg', fused_img)

2. Simple color border

This is an even simpler task than the one we just performed. All we need to do is pass the color and the dimensions of the needed border to the target image using v2.copyMakeBorder()

BLACK = [0,0,0]
black_border_img = cv2.copyMakeBorder(target_img, 20,20,20,20, cv2.BORDER_CONSTANT, value=BLACK)

Full program listing

Here is the full program listing

import cv2
import numpy as np

# read the target file
image = 'blagaj_resized.jpg'
target_img = cv2.imread(image)

# create a border of a specfic color (here: black) and apply to image
BLACK = [0,0,0]
black_border_img = cv2.copyMakeBorder(target_img, 20,20,20,20, cv2.BORDER_CONSTANT, value=BLACK)

cv2.imshow("BORDER_CONSTANT", black_border_img)
cv2.waitKey(0)

cv2.imwrite('blagaj_red_filter.jpg', black_border_img)

Stay tuned for more posts on OpenCV.

 


 


 

번호 제목 글쓴이 날짜 조회 수
» Create and apply simple filters to an image using OpenCV and Python proin 2020.08.31 0
83 Flutter 소개와 개발환경 구축 proin 2020.08.29 2
82 Sudo pip install은 안돼요! proin 2020.08.28 0
81 Memory(Private Bytes, Working Set, Virtual Bytes) proin 2020.07.29 0
80 [윈도우 배치 프로세스] 프로세스 메모리 사용량 체크하기 proin 2020.07.27 0
79 특정 프로세스 메모리 사용량 모니터링 로그 프로그램 file proin 2020.07.27 0
78 성능 모니터 주요 체크 카운터 proin 2020.07.27 0
77 성능 모니터 주요 체크 카운터 proin 2020.07.27 0
76 성능 모니터 주요 카운터 정리 proin 2020.07.27 0
75 프로세스 메모리 사용량 로그 기록 - 성능 모니터 이용 proin 2020.07.27 0
74 [기타] 윈도우에서 삼바 로그아웃 하기 proin 2020.05.15 1
73 openmediavault 이용하여 라즈베리파이 나스(NAS) 만들기 (FTP 파일서버 구축 방법) proin 2019.09.18 0
72 ITS : OpenProject 설치 & 초기 환경설정 proin 2019.09.18 0
71 초보를 위한 도커 안내서 - 설치하고 컨테이너 실행하기 proin 2019.09.18 0
70 vmware 에 헤놀로지 (synology) 및 DSM 설치 proin 2019.06.16 1
69 VMware에서 XPEnology DSM6.2구동 proin 2019.06.16 0
68 Xpenology(해놀로지) 6.1.5 DSM 설치 proin 2019.06.14 0
67 [Ubuntu] Could not get lock 에러 해결 방법 proin 2019.06.14 0
66 [아두이노] 아두이노/라즈베리파이 호환 카메라 모듈 / Mini moduleCamera Shield w/5 MP OV5642 forArduino UNOMega2560 board proin 2019.06.14 0
65 [아두이노] Arducam MINI Camera Demo Tutorial for Arduino proin 2019.06.14 0
위로