Learn OpenCV (Python): Basic image manipulations / Operations
System information:
- OS: Ubuntu 16.04
- OpenCV: 3.1.0
- Convert Array to Image
import numpy import os import cv2 random_byte_array = bytearray(os.urandom(120000)) # or random_byte_array = numpy.random.randint(0, 256, 120000) flat_numpy_array = numpy.array(random_byte_array) # reshape to an grayscale image with 300px in height, 400px in width # which is a 2D array gray_image = flat_numpy_array.reshape(300, 400) cv2.imwrite('../data/random_gray.png', gray_image) # reshape to an BGR image with 100px in heigth, 400px in width # and 3 channels (BGR) # which is a 3D array bgr_image = flat_numpy_array.reshape(100, 400, 3) cv2.imwrite('../data/random_bgr.png', bgr_image) # *Note: this is only an example, the paths is not necessarily to be # the same on your system
- Change pixel values in gray-scale image
import numpy as np import cv2 import random import datetime # seed the current time for random every time program starts random.seed(datetime.datetime.now()) # help code more readable ESCAPE_KEY = 27 # read image and change to gray scale img = cv2.imread('../../data/planet_glow.jpg', cv2.IMREAD_GRAYSCALE) # img.shapre on a gray-scale image return (row, col) row_count, column_count = img.shape # change all pixels value to random value from 0-255 for i in xrange(row_count): for j in xrange(column_count): img[i, j] = random.randint(0, 255) # show image with title "show" cv2.imshow("show", img) # pause the program # if you remove this if block # the window will close immediately after the program starts # causing you see nothing if cv2.waitKey() == ESCAPE_KEY: cv2.destroyAllWindows()
- Change pixel values in BGR image (in open cv, BGR = RGB, weird!)
import numpy as np import cv2 import random import datetime # help code more readable ESCAPE_KEY = 27 # seed every time start a new program random.seed(datetime.datetime.now()) # read image img = cv2.imread('../../data/planet_glow.jpg', cv2.IMREAD_COLOR) # with color image, img.shape returns (row, column, channels) row, column, channels = img.shape # change all pixels to random value for i in xrange(row): for j in xrange(column): B_random = random.randint(0, 255) G_random = random.randint(0, 255) R_random = random.randint(0, 255) img[i, j] = [B_random, G_random, R_random] # show image cv2.imshow("Image", img) # pause program, to help you see the image # press ESC key to exit if cv2.waitKey() == ESCAPE_KEY: cv2.destroyAllWindows()
But changing pixel values by indexing is not an appropriate way to do, instead using item (to retrieve value) and itemset (to change pixel value) method.
import numpy as np import cv2 ESCAPE_KEY = 27 # B, G, R = 0, 1, 2 respectively B, G, R = 0, 1, 2 # read image img = cv2.imread('../../data/planet_glow.jpg', cv2.IMREAD_COLOR) # img.shape return (row, col, channels) row, col, channels = img.shape # for example # we want to set all B values of each pixel to 0 # itemset((i, j, channel), new_val), channel could be B, G, R (or 0, 1, 2) for i in xrange(row): for j in xrange(col): new_val = 0 img.itemset((i, j, B), new_val) # show image cv2.imshow("Tittle", img) if cv2.waitKey() == ESCAPE_KEY: cv2.destroyAllWindows()
Here’s how you can change all pixel values without for loop, using numpy array syntax.
import cv2 import numpy as np import random import datetime ESCAPE_KEYS = 27 random.seed(datetime.datetime.now()) B, G, R = 0, 1, 2 img = cv2.imread('../../data/planet_glow.jpg', cv2.IMREAD_COLOR) # change all pixels in original image to a random color # create a solid-random-color image B_random = random.randint(0, 255) G_random = random.randint(0, 255) R_random = random.randint(0, 255) img[:, :] = [B_random, G_random, R_random] # or change only channel B to random value img[:, :, B] = B_random # or change channels B, G to random value img[:, :, (B, G)] = (B_random, G_random) # or change all three channels # this have the same effect with line 17 img[:, :, (B, G, R)] = (B_random, G_random, R_random) cv2.imshow("show", img) if cv2.waitKey() == ESCAPE_KEYS: cd2.destroyAllWindows()
- Copy a portion of an image, and place it overlap to other portion
The code below copy all pixel values in a 100×100 portion, starting index (0,0) and place overlap it on portion with starting index (300, 300)
import numpy as np import cv2 ESCAPE_KEY = 27 img = cv2.imread('../../data/planet_glow.jpg', cv2.IMREAD_COLOR) # get 100x100 portion with starting index at (0, 0) # and place it overlap portion with starting index at (300, 300) my_roi = img[0:100, 0:100] img[300:400, 300:400] = my_roi cv2.imshow("show", img) if cv2.waitKey() == ESCAPE_KEY: cv2.destroyAllWindows()
Advertisements
Reply