4월, 2020의 게시물 표시

Image Processing #9 - Image Splitting

이미지
Recently, I had to separate a image containing multiple balls. In the case of one or two balls, you can use an image editing program to separate them, but if the number of images increases, it can be much faster to create a simple program to separate them. <lotto.png> If you use numpy's hsplit and vsplit functions, you can separate the images into N by M pieces. You can separate lottery balls into 1 ~ 45.png files with the following simple code. #-*- coding:utf-8 -*- import numpy as np import cv2 # split into 10 X 5 H_Count = 10 V_Count = 5 file = './lotto.png' img = cv2 . imread(file, cv2 . IMREAD_COLOR) height, width, channels = img . shape count = 1 h_img = np . vsplit(img, V_Count) for i in h_img: j = np . hsplit(i, H_Count) for k in j: name = "%d.png" % (count) cv2 . imwrite(name, k) count += 1 <img_split.py> <splitted image files>

Image Processing #8 - Image Append

이미지
Occasionally, images need to be pasted horizontally or vertically. In this article, we will implement this function using the numpy function commonly used in OpenCV and PIL. Convert image to numpy array  The first thing you must do is to convert the image to a numpy array. This is explained at https://opencvcooking.blogspot.com/2019/11/basic-cooking-1.html . OpenCV Image to numpy vice versa OpenCV images (Mat) have a numpy array and can be used directly without conversion. import cv2 img = cv2 . imread( "dog.jpg" , cv2 . IMREAD_COLOR) print(type(img)); And you can convert numpy array to an OpenCV image using fromarray function. import cv2 img = cv . fromarray(array) height, width, channels = img . shape PIL Image to numpy vice versa from PIL import Image im = Image . fromarray(np . uint8(array)) array = np . asarray(im, dtype = "uint8" ) And you can convert numpy array to an PIL image using fromarray functio

Image Processing #5 - WaterMark using alpha channel

이미지
In the previous article, we saw how to implement a watermark without an alpha channel. This method uses a method of overlaying the part where the color exists except for the black color (0,0,0) part of the mask image. However, in the case of a png file having an alpha channel, this can be simplified because the density can be adjusted by the size of the alpha channel value. The source you implement is almost the same as the existing one. However, if the existing code finds black, this time, read the alpha channel value and divide it by 255 to reflect this ratio. New watermark function using alpha channel def process_alpha_masking (base, mask, pos): h, w, c = mask . shape hb, wb, _ = base . shape x = pos[ 0 ] y = pos[ 1 ] #check mask position if (x > wb or y > hb): print( ' invalid overlay position(%d,%d)' % (x, y)) return None #alpha channel check if c != 4 : print

Video Cooking #3 - Read frames from gif

Reading and creating gif files is much easier using PIL than OpenCV. Read frame from gif file import argparse from PIL import Image, ImageSequence parser = argparse . ArgumentParser(description = 'gif run' ) parser . add_argument( '--file' , type = str, required = True , help = 'gif file name' ) args = parser . parse_args() im = Image . open(args . file) index = 1 for frame in ImageSequence . Iterator(im): frame . save( "frame%d.png" % index) index += 1