plt
- DO remember to convert BGR to RGB! But you don't have to do that when using cv2.imshow, and all other cv2 methods for color imagesnp.uint8
import cv2
import matplotlib.pyplot as plt
import mahotas as mh
import numpy as np
%matplotlib inline
*Read Images by cv2*
## 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()
(233, 350, 3) uint8 0 255
*Show Images by plt*
## display
plt.imshow(im)
<matplotlib.image.AxesImage at 0x7f0a30c22490>
## otherwise ...
plt.imshow(im[:,:,::-1])
<matplotlib.image.AxesImage at 0x7f0a30f5dbd0>
*Read & Display Grayscale directly*
## 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)
(228, 350) uint8 0 255
<matplotlib.image.AxesImage at 0x7f0a30ce2f90>
## interactive - but got some problems with ipython notebook display system
cv2.imshow("dianosou", im)
cv2.waitKey()
113
*Format Convertion by cv2*
## 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
data/images/beach.bmp: PC bitmap, Windows 3.x format, 350 x 233 x 24 data/images/beach.png: PNG image data, 350 x 233, 8-bit/color RGB, non-interlaced
*Pixel Manipulation by numpy*
## manipulate pixles - cv2 store them as bgr instead of rgb
imm = im.copy()
imm[:, :, 2] = 255
plt.imshow(imm)
<matplotlib.image.AxesImage at 0x7f0a30aea150>
## manipulate pixles
imm = im.copy()
imm[:100, :50] = (255, 0, 0) # red corner
plt.imshow(imm)
<matplotlib.image.AxesImage at 0x7f0a2c0d3610>
*modify image by drawing via cv2*
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)
<matplotlib.image.AxesImage at 0x7f0a27bc3b90>
imm = im.copy().astype(np.uint16)
cv2.rectangle(imm, (100, 50), (150, 120),
color = (0, 255, 0), thickness = -1)
plt.imshow(imm)
<matplotlib.image.AxesImage at 0x7f0a27afbb50>
## 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)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-11-925a55e866ad> in <module>() 3 4 cv2.rectangle(imm, (100, 50), (150, 120), ----> 5 color = (0, 255, 0), thickness = -1) 6 plt.imshow(imm) TypeError: Layout of the output array img is incompatible with cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels)
## 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)
<matplotlib.image.AxesImage at 0x7f0a279c8a50>
*Traditional Image Processing by cv2*
translation - cv.warpAffine
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)
<matplotlib.image.AxesImage at 0x7f0a278cbd50>
rotation-cv2.warpAffine
cv2.getRotationMatrix2D
cv2.getRotationMatrix2D
: center, angle(+ -> counterclockwise), scaletrex = 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)
<matplotlib.image.AxesImage at 0x7f0a2780bd10>
resizing - cv2.resize
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
(228, 350, 3)
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)
(68, 105, 3)
True
flipping
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, :])
<matplotlib.image.AxesImage at 0x7f0a27543fd0>
*Image Arithematics*
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()
0 255 0 254
*masking by cv2.draw and cv2.bitwise_and*
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)
<matplotlib.image.AxesImage at 0x7f0a269eb5d0>
*Split Color Channels*
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")
<matplotlib.text.Text at 0x7f0a25cef450>
## confusing, confusing, RGB, BGR, ...
plt.imshow(cv2.cvtColor(beach, cv2.COLOR_RGB2HSV)[:,:,::-1])
<matplotlib.image.AxesImage at 0x7f0a25ae0c10>