import imageutils import cv2 def rotated(image, degrees, x_offset=None, y_offset=None): (h, w) = image.shape[:2] (cX, cY) = (w / 2, h / 2) if x_offset is int: cX += x_offset if y_offset is int: cY += y_offset rot_mat = cv2.getRotationMatrix2D((cX, cY), degrees, 1.0) return cv2.warpAffine(image, rot_mat, (w, h)) image = imageutils.image_arg() cv2.imshow("Original", image) cv2.imshow("Rotated by 45 degrees counter-clockwise", rotated(image, 45)) cv2.imshow("Rotated by 45 degrees c-c minus 50px offset on both axis", rotated(image, 45, x_offset=-50, y_offset=-50)) cv2.waitKey(0)
import numpy as np import matplotlib matplotlib.use('macosx') from matplotlib import pyplot as plt from imageutils import image_arg def plot_histogram(image, title, mask=None): chans = cv2.split(image) colors = ("b", "g", "r") plt.figure() plt.title(title) plt.xlabel("Bins") plt.ylabel("# of Pixels") for chan, color in zip(chans, colors): hist = cv2.calcHist([chan], [0], mask, [256], [0, 256]) plt.plot(hist, color=color) plt.xlim([0, 256]) image = image_arg() cv2.imshow("Original", image) plot_histogram(image, "Histogram for Original") mask = np.zeros(image.shape[:2], dtype="uint8") cv2.rectangle(mask, (60, 290), (210, 390), 255, -1) cv2.imshow("Mask", mask) masked = cv2.bitwise_and(image, image, mask=mask) plot_histogram(image, "Histogram for Masked", mask=mask) cv2.imshow("Masked", masked) plt.show()
import cv2 from imageutils import image_arg def empty_canvas(): return np.zeros(img.shape[:2], dtype="uint8") def circle_mask(): mask = empty_canvas() cv2.circle(mask, (145, 200), radius=100, color=255, thickness=-1) return mask def rect_mask(): mask = empty_canvas() cv2.rectangle(mask, (0, 90), (290, 450), color=255, thickness=-1) return mask img = image_arg() r_mask = rect_mask() c_mask = circle_mask() r_masked_img = cv2.bitwise_and(img, img, mask=r_mask) c_masked_img = cv2.bitwise_and(img, img, mask=c_mask) cv2.imshow("Original", img) cv2.imshow("Mask", r_mask) cv2.imshow("Rect masked image", r_masked_img) cv2.imshow("Circle masked image", c_masked_img) cv2.waitKey(0)