#!/usr/bin/python3 # https://pyimagesearch.com/2018/07/19/opencv-tutorial-a-guide-to-learn-opencv/ # import the necessary packages import cv2 import imutils import numpy as np import argparse def rescale_frame(frame, percent): # rescale image considering aspect ratio width = int(frame.shape[1] * percent/ 100) height = int(frame.shape[0] * percent/ 100) dim = (width, height) return cv2.resize(frame, dim, interpolation =cv2.INTER_AREA) # alternative: resized = imutils.resize(image, width=300) => give goal height or width def extract_channels(image,color_space): (ch1,ch2,ch3) = cv2.split(image) cv2.imshow("Channel "+color_space[0],ch1) cv2.imshow("Channel "+color_space[1],ch2) cv2.imshow("Channel "+color_space[2],ch3) def rotate_bound(image, angle): (h, w) = image.shape[:2] # image dimesnsions (cX, cY) = (w // 2, h // 2) # center of image M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0) # neg angle to rotate clockwise cos = np.abs(M[0, 0]) # cos & sin value from rotation matrix sin = np.abs(M[0, 1]) # compute the new bounding dimensions of the image nW = int((h * sin) + (w * cos)) nH = int((h * cos) + (w * sin)) # adjust the rotation matrix to take into account translation M[0, 2] += (nW / 2) - cX M[1, 2] += (nH / 2) - cY # perform the actual rotation and return the image return cv2.warpAffine(image, M, (nW, nH)) # load the input image and show its dimensions, keeping in mind that # images are represented as a multi-dimensional NumPy array with # shape no. rows (height) x no. columns (width) x no. channels (depth) image = cv2.imread('/home/caroline/Pictures/first.jpg') #numpy array # resize image ignoring aspect ratio resized_image = cv2.resize(image, (300, 300)) cv2.imshow("Fixed Resizing", resized_image) cv2.waitKey(0) image = rescale_frame(image,20) (h, w, d) = image.shape #height=rows, width=columns, depth=channels => 3 for RGB print("width={}, height={}, depth={}".format(w, h, d)) # display the image to our screen -- we will need to click the window # open by OpenCV and press a key on our keyboard to continue execution cv2.imshow("Image", image) cv2.waitKey(0) # grayscale value 0-255 # (B, G, R) with range [0, 255] # access the RGB pixel located at x=50, y=100, keepind in mind that # OpenCV stores images in BGR order rather than RGB (B, G, R) = image[100, 50] print("R={}, G={}, B={}".format(R, G, B)) # array slicing and cropping # extract a 100x100 pixel square ROI (Region of Interest) from the # input image starting at x=320,y=60 at ending at x=420,y=160 """ roi = image[60:160, 320:420] cv2.imshow("ROI", roi) cv2.waitKey(0) """ # transform image into different color spaces and split channels """ image_lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB) extract_channels(image_lab,"LAB") # lightness, red/green value, blue/yellow value => very vast color space cv2.waitKey(0) image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # hue:color 0°-360°, saturation: amount of grey 0-1, value: brightness/intensity 0-1 extract_channels(image_hsv,"HSV") cv2.waitKey(0) """ # rotate image # loop over rotation angles without cutting image """ for angle in np.arange(0, 360, 30): # range [0, 360] in 15° increments rotated = rotate_bound(image, angle) cv2.imshow("Rotated", rotated) cv2.waitKey(0) """ # blurring: reduce high-frequency noise => gaussianBlur function blurred = cv2.GaussianBlur(image, (11, 11), 0) # 11x11 kernel: larger kernel create more blurry images cv2.imshow("Blurred", blurred) cv2.waitKey(0)