import cv2 import matplotlib.pyplot as plt import mahotas as mh import numpy as np %matplotlib inline ## read ## cv2 read BGR color, but most other libraries read RGB, so convert im = cv2.imread("data/images/beach.png")[:,:,::-1] print im.shape print im.dtype print im.min(), im.max() ## display plt.imshow(im) ## otherwise ... plt.imshow(im[:,:,::-1]) ## read as gray scale directly im_gray = cv2.imread("data/images/trex.png", cv2.IMREAD_GRAYSCALE) print im_gray.shape print im_gray.dtype print im_gray.min(), im_gray.max() plt.imshow(im_gray, cmap = plt.cm.gray) ## interactive - but got some problems with ipython notebook display system cv2.imshow("dianosou", im) cv2.waitKey() ## convert to other formats ## convert from BGR to RGB again!! cv2.imwrite("data/images/beach.bmp", im[:,:,::-1]) !file data/images/beach.bmp !file data/images/beach.png ## manipulate pixles - cv2 store them as bgr instead of rgb imm = im.copy() imm[:, :, 2] = 255 plt.imshow(imm) ## manipulate pixles imm = im.copy() imm[:100, :50] = (255, 0, 0) # red corner plt.imshow(imm) imm = im.copy() ## params: img, (x1, y1), (x2, y2), color (of the current img channel repr.) cv2.line(imm, (0, 0), (100, 150), (255, 0, 0), ) #red line cv2.circle(imm, (200, 100), 20, color = (0, 0, 255), thickness=5) #blue circle ## filled rectangle cv2.rectangle(imm, (100, 50), (150, 120), color = (0, 255, 0), thickness = -1) plt.imshow(imm) imm = im.copy().astype(np.uint16) cv2.rectangle(imm, (100, 50), (150, 120), color = (0, 255, 0), thickness = -1) plt.imshow(imm) ## it only works with np.unit8 imm = im.copy().astype(np.int64) cv2.rectangle(imm, (100, 50), (150, 120), color = (0, 255, 0), thickness = -1) plt.imshow(imm) ## canvas drawing - RGB because we are gonna use plt to plot canvas = np.zeros((100, 100, 3), dtype=np.uint8) ## draw 25 random circles centers = np.random.randint(10, 90, size = (25, 2)) radius = np.random.randint(1, 50, size = 25) colors = np.random.randint(0, 256, size = (25, 3)) for c, r, col in zip(centers, radius, colors): ## somehow cv2 insists on tuples as control parameters cv2.circle(canvas, tuple(c), r, tuple(col), thickness = -1) plt.imshow(canvas) trex = cv2.imread("data/images/trex.png")[:, :, ::-1] shifted_trex = cv2.warpAffine(trex, np.array([[1, 0, 30], [0, 1, 100]], dtype = np.float), dsize = (trex.shape[1], trex.shape[0])) # right down shift plt.imshow(trex) plt.figure() plt.imshow(shifted_trex) trex = cv2.imread("data/images/trex.png")[:, :, ::-1] center = trex.shape[1]/2, trex.shape[0]/2 M = cv2.getRotationMatrix2D(center, -90, 1.) rotated_trex = cv2.warpAffine(trex, M, (trex.shape[1], trex.shape[0])) plt.imshow(trex) plt.figure() plt.imshow(rotated_trex) trex = cv2.imread("data/images/trex.png")[:, :, ::-1] center = trex.shape[1]/2, trex.shape[0]/2 M = cv2.getRotationMatrix2D(center, 0, 0.3) rotated_trex = cv2.warpAffine(trex, M, (trex.shape[1], trex.shape[0])) plt.imshow(trex) plt.figure() plt.imshow(rotated_trex) print rotated_trex.shape trex = cv2.imread("data/images/trex.png")[:, :, ::-1] width, height = int(trex.shape[1]*0.3), int(trex.shape[0]*0.3) resized_trex = cv2.resize(trex, (width, height), interpolation = cv2.INTER_AREA,) plt.imshow(trex) plt.figure(figsize = (1.5, 1.5)) plt.imshow(resized_trex) print resized_trex.shape cv2.imwrite("data/images/small_trex.png", resized_trex) trex = cv2.imread("data/images/trex.png")[:, :, ::-1] fig, axes = plt.subplots(2, 2, figsize = (8, 8)) fig.tight_layout() axes = axes.ravel() axes[0].imshow(trex) axes[1].imshow(trex[:, ::-1, :]) axes[2].imshow(trex[::-1, :, :]) axes[3].imshow(trex[::-1, ::-1, :]) a = cv2.add(trex, trex) plt.imshow(a) plt.figure() b = trex + trex ## overflow plt.imshow(b) print a.min(), a.max() print b.min(), b.max() beach = cv2.imread("data/images/beach.png")[:,:,::-1] plt.imshow(beach) h, w = beach.shape[:-1] canvas = np.zeros((h, w), dtype = np.uint8) mask = cv2.circle(canvas, (w/2, h/2), 30, color = 255,thickness = -1) plt.figure() plt.imshow(mask, cmap = plt.cm.gray) masked_beach = cv2.bitwise_and(beach, beach, mask = mask) plt.figure() plt.imshow(masked_beach) wave = cv2.imread("data/images/wave.png")[:,:,::-1] fig, axes = plt.subplots(2, 2, figsize = (8, 8)) axes = axes.ravel() fig.tight_layout() axes[0].imshow(wave) axes[0].set_title("original") axes[1].imshow(wave[:, :, 0], cmap = plt.cm.gray) axes[1].set_title("red") axes[2].imshow(wave[:, :, 1], cmap = plt.cm.gray) axes[2].set_title("green") axes[3].imshow(wave[:, :, 2], cmap = plt.cm.gray) axes[3].set_title("blue") ## confusing, confusing, RGB, BGR, ... plt.imshow(cv2.cvtColor(beach, cv2.COLOR_RGB2HSV)[:,:,::-1])