Aidemy 2020/9/23
Hello, it is Yope! I'm a liberal arts college student, but I'm interested in the AI field, so I'm studying at the AI-specialized school "Aidemy". I would like to share the knowledge gained here with you, and I am summarizing it on Qiita. I am very happy that many people have read the previous summary article. Thank you! This is the third post of data cleansing. Nice to meet you.
What to learn this time ・ About image data ・ What you can do with OpenCV (library that handles images)
-In the data, the color is expressed as RGB data in red, green, and blue (255,0,255, etc.). -The image is made up of a collection of points __ (pixels) __. -The number of color elements to represent one pixel is called channel number (“3” because there are three colors in RGB).
・ BMP cannot be compressed and is large in size ・ JPG Highly compressed but not decompressable -PNG compression / decompression possible, transparent processing possible ・ Supports GIF animation, transparent processing possible
2 OpenCV
-OpenCV is a library used when handling images. Import and use cv2. -Read with __cv2.imread ("filename") __ __cv2.imshow ("window name", read image data) Display as __ (window name can be freely assigned)
import cv2
#sample.Load an image with the file name jpg
img = cv2.imread("./4050_data_cleansing_data/sample.jpg ")
#Output img with window name as window
cv2.imshow("window",img)
-To create an image, use the _np.array () function __ (a function that creates a NumPy matrix). In addition, range () is looped multiple times without variables to set vertical and horizontal pixel information (the order is blue-green-red). __ · np.array ([[[B, G, R values] for _ in range (horizontal size)] for in range (vertical size)], dtype = "uint8") __
#(512*512)One side green(0,255,0)Create an image of
img = np.array([[[0,255,0]for _ in range(512)]for _ in range(512)],dtype="uint8")
#Save img(The file name is "green".img」)
cv2.imwrite("green.img",img)
-Trimming is __ image data [y-axis start point: y-axis end point, x-axis start point: x-axis end point] __ 0 is the upper left -Resize is changed with __cv2.resize (image data, (width, height)) __.
#Get image size for cropping(height,width,Number of colors)
size = img.shape #(1000,667,3)
#Height is 1/2, width 1/Trimming to 3 (do not specify the start point, divide so that there is no remainder)
new_img=img[:size[0]//2,:size[1]//3]
#Resized to double height and triple width
new_img=cv2.resize(new_img,(new_img.shape[0]*2,new_img.shape[1]*3))
-The rotation of the image requires a transformation called affine transformation, and the transformation matrix must be obtained. -Get the transformation matrix with __cv2.getRotationMatrix2D (center coordinates of image, rotation angle, scaling magnification) __. -And actually rotate with __cv2.warpAffine (image, transformation matrix, output size) __.
-Invert is done with __cv2.flip (image, flipCode) __. For the second argument, if "0" is specified, it will be inverted vertically, if a positive number is specified, it will be inverted horizontally, and if a negative number is specified, it will be inverted vertically and horizontally.
#Get the transformation matrix(Rotate 90 degrees at 2x magnification)
mat=cv2.getRotationMatrix(tuple(np.array([img.shape[1],img.shape[0]])/2),90,2.0)
#Actually rotate
cv2.warpAffine(img,mat,img.shape[::-1][1:3])
-Until now, only "BGR color space" was used, but there are other color spaces on OpenCV. -You can change the color space with __cv2.cvtColor (image, conversion code) __. The conversion code is, for example, "cv2.COLOR_BGR2LAB" when you want to convert the BGR color space to a color space called Lab.
-Color inversion can also be done by taking out each pixel in turn and setting the value x to "255-x". You can easily flip it with __cv2.bitwise_not (image) __.
#Change the color space of img to Lab
c_img=cv2.cvtColor(img,cv2.COLOR_BGR2LAB)
#Invert img color
r_img=cv2.bitwise_not(img)
-In order to reduce the size of the image, color data should be limited to "white" and "black". -__Cv2.threshold (image, threshold, maximum value (concentration), type of threshold processing) __
#Threshold 127, maximum value 255, type cv2.THRESH_TOZERO(0 below the threshold, no change above the threshold)
new_img=cv2.threshold(img,127,255,cv2.THRESH_TOZERO)
-If a black and white mask image is prepared separately and combined with the original image using the cv2.bitwise_and () function, only the white part of the mask image will be output from the original image. This is called "masking". -__Cv2.bitwise_and (original image 1, original image 2 (used when masking), mask = black and white image for mask) __
#Read the mask image (black and white image with 1 channel), resize it, and make it the same size.
mask=cv2.imread("./4050_cleansing_data/mask.png ", 0)
mask=cv2.resize(mask,(img.shape[1],img.shape[0]))
#Masking
cv2.bitwise_and(img,img,mask=mask)
-Use the "Gaussian filter" to blur the image. Average and blur n * n (n is an odd number) around one pixel. · __Cv2.GaussianBlur (image, (n * n value), standard deviation) __
#(51*51)Blur
new_img=cv2.GaussianBlur(img,(51,51),0)
・ __Cv2.fastNlMeansDenoisingColored (image) __. If it is not a color image, the function name "Colored" may be omitted. (Nl Means refers to a noise reduction filter called Non-local Means Filter, Denoising means noise reduction)
new_img=cv2.fastNlMeansDenoisingColored(img)
-Another method of noise processing is to shrink the image once and then expand it again. This method is often used for "threshold processing" noise removal. -Expand with __cv2.dilate (image, filter) __, shrink with __cv2.erode (image, filter) __.
#Threshold processing of img
new_img=cv2.threshold(img,127,255,cv2.THRESH_BYNARY)
#Filter definition
filter=np.array([[0,1,0],[1,0,1],[0,1,0]],np.uint8)
#Shrink and expand
new_img=cv2.erode(new_img,filter)
new_img=cv2.dilate(new_img,filter)
-Image data is processed by OpenCV. -In OpenCV, you can read images with __imread () __, output images with __imshow () __, resize with __resize () __, invert with __flip () __, and invert colors with ** bitwise_not **. In addition, you can create images, crop, and rotate. -As an application, use __threshold () __ for threshold processing, ** bitwise_and () ** for masking, and __GaussianBlur () __ for blurring. In addition, noise can be removed.