REF1 - OpenCV Basics
REF1 - OpenCV Basics
LOAD IMAGE
# compute the center of the image, which is simply the width and height
# divided by two
(cX, cY) = (w // 2, h // 2)
# since we are using NumPy arrays, we can apply array slicing to grab
# large chunks/regions of interest from the image -- here we grab he
# top-left corner of the image
tl = image[0:cY, 0:cX]
cv2.imshow("Top-Left Corner", tl)
DRAWING ON AN IMAGE
● cv2.line: Draws a line on image, starting at a specified (x, y) and ending at another (x, y)
● cv2.circle: Draws a circle on an image specified by the center (x, y)-coordinate and a supplied radius
● cv2.rectangle: Draws a rectangle on an image specified by the top-left And bottom-right corner (x, y)
● cv2.ellipse: Draws an ellipse on an image
● cv2.polylines: Draws the outline of a polygon specified by a set of (x, y)-coordinates
● cv2.fillPoly: Draws a polygon, but instead of drawing the outline, instead fills it in
● cv2.arrowedLine: Draws an arrow pointing from a starting (x, y) to an ending (x, y)
4. TRANSLATION
# use the imutils helper function to translate the image 100 pixels
# down in a single function call
shifted = imutils.translate(image, 0, 100)
cv2.imshow("Shifted Down”, shifted)
cv2.waitKey(0)
5. ROTATION
In general, cv2.INTER_NEAREST is quite fast but does not provide the highest quality results. So in very
resource-constrained environments, consider using nearest-neighbor interpolation.
When increasing (upsampling) the size of an image, consider using cv2.INTER_LINEAR and
cv2.INTER_CUBIC. The cv2.INTER_LINEAR method tends to be slightly faster than the
cv2.INTER_CUBIC method, but go with whichever method provides the best results for your images.
When decreasing (downsampling) the size of an image, the OpenCV documentation suggests using
cv2.INTER_AREA. Again, you could also use cv2.INTER_NEAREST for downsampling as well, but
cv2.INTER_AREA typically yields more aesthetically pleasing results.
Finally, as a general rule, the cv2.INTER_LINEAR interpolation method is recommended as the default for
whenever you’re upsampling or downsampling — it merely provides the highest quality results at a modest
computation cost.
7. FLIP
# similarly, we can subtract 50 from all pixels in our image and make it
# darker
M = np.ones(image.shape, dtype="uint8") * 50
subtracted = cv2.subtract(image, M)
cv2.imshow("Darker", subtracted)
cv2.waitKey(0)
10. BITWISE OPERATIONS
# a bitwise 'AND' is only 'True' when both inputs have a value that
# is 'ON' -- in this case, the cv2.bitwise_and function examines
# every pixel in the rectangle and circle; if *BOTH* pixels have a
# value greater than zero then the pixel is turned 'ON' (i.e., 255)
# in the output image; otherwise, the output value is set to
# 'OFF' (i.e., 0)
bitwiseAnd = cv2.bitwise_and(rectangle, circle)
cv2.imshow("AND", bitwiseAnd)
cv2.waitKey(0)
# a mask is the same size as our image, but has only two pixel
# values, 0 and 255 -- pixels with a value of 0 (background) are
# ignored in the original image while mask pixels with a value of
# 255 (foreground) are allowed to be kept
mask = np.zeros(image.shape[:2], dtype="uint8")
cv2.rectangle(mask, (0, 90), (290, 450), 255, -1)
cv2.imshow("Rectangular Mask", mask)
# apply our mask -- notice how only the person in the image is
# cropped out
masked = cv2.bitwise_and(image, image, mask=mask)
cv2.imshow("Mask Applied to Image", masked)
cv2.waitKey(0)
# now, let's make a circular mask with a radius of 100 pixels and
# apply the mask again
mask = np.zeros(image.shape[:2], dtype="uint8")
cv2.circle(mask, (145, 200), 100, 255, -1)
masked = cv2.bitwise_and(image, image, mask=mask)
# show the output images
cv2.imshow("Circular Mask", mask)
cv2.imshow("Mask Applied to Image", masked)
cv2.waitKey(0)
12. SPLITTING AND MERGING CHANNELS