Tag Archives: python

Direct Drawing a rectangle on Images with a mouse with OpenCV Python

import cv2
import numpy as np

is_drawing = False
ix = -1
iy = -1

blank_image = np.zeros([512, 512, 3], dtype=np.uint8)


def draw_rectangle(event, x, y, flags, param):
    global is_drawing, ix, iy

    if event == cv2.EVENT_LBUTTONDOWN:
        is_drawing = True
        ix = x
        iy = y
    elif event == cv2.EVENT_LBUTTONUP:
        is_drawing = False
        cv2.rectangle(img=blank_image, pt1=(ix, iy), pt2=(x, y), color=(255, 0, 0), thickness=-1)
        ix = -1
        iy = -1
    elif event == cv2.EVENT_MOUSEMOVE:
        if is_drawing:
            cv2.rectangle(img=blank_image, pt1=(ix, iy), pt2=(x, y), color=(255, 0, 0), thickness=-1)


cv2.namedWindow("image")
cv2.setMouseCallback("image", draw_rectangle)

while True:
    cv2.imshow("image", blank_image)

    # wait 5 seconds then wait for ESC key
    if cv2.waitKey(5) & 0xFF == 27:
        break

cv2.destroyAllWindows()

References
https://github.com/mhdr/OpenCVSamples/tree/master/011

Direct Drawing on Images with a mouse with OpenCV Python

import cv2
import numpy as np

blank_image = np.zeros([512, 512, 3], dtype=np.uint8)


def draw_circle(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        cv2.circle(blank_image, center=(x, y), radius=20, color=(0, 0, 255), thickness=-1)
    elif event == cv2.EVENT_RBUTTONDOWN:
        cv2.circle(blank_image, center=(x, y), radius=20, color=(0, 255, 0), thickness=-1)


cv2.namedWindow("image")
cv2.setMouseCallback("image", draw_circle)

while True:
    cv2.imshow("image", blank_image)

    # wait 5 seconds then wait for ESC key
    if cv2.waitKey(5) & 0xFF == 27:
        break

cv2.destroyAllWindows()

References
https://github.com/mhdr/OpenCVSamples/tree/master/011

Create blank image using OpenCV Python

import cv2
import numpy as np

# black blank image
blank_image = np.zeros(shape=[512, 512, 3], dtype=np.uint8)
# print(blank_image.shape)
cv2.imshow("Black Blank", blank_image)

# white blank image
blank_image2 = 255 * np.ones(shape=[512, 512, 3], dtype=np.uint8)
cv2.imshow("White Blank", blank_image2)

cv2.waitKey(0)
cv2.destroyAllWindows()

References
https://stackoverflow.com/questions/10465747/how-to-create-a-white-image-in-python
https://github.com/mhdr/OpenCVSamples/tree/master/010

Flip image in OpenCV Python

import cv2
import numpy as np

image = cv2.imread("cow.jpg")
cv2.imshow("image", image)

# flip horizontally
flipped1 = cv2.flip(image, 0)
cv2.imshow("image1", flipped1)

# flip vertically
flipped2 = cv2.flip(image, 1)
cv2.imshow("image2", flipped2)

# flip both horizontally and vertically
flipped3 = cv2.flip(image, -1)
cv2.imshow("image3", flipped3)

cv2.waitKey(0)
cv2.destroyAllWindows()

References
https://github.com/mhdr/OpenCVSamples/tree/master/009

Resize image in OpenCV Python

import cv2
import numpy as np

image = cv2.imread("cow.jpg")
print(image.shape)  # (559, 838, 3)

old_width = image.shape[1]  # 838
old_height = image.shape[0]  # 559

# resize by custom size
new_width = 600
new_height = 400
resized = cv2.resize(image, (new_width, new_height))
cv2.imshow("image", resized)

# resize by ratio
width_ratio = 0.5
height_ration = 0.5
resized2 = cv2.resize(image, (0, 0), image, width_ratio, height_ration)
cv2.imshow("image2", resized2)

cv2.waitKey(0)
cv2.destroyAllWindows()

References
https://github.com/mhdr/OpenCVSamples/tree/master/008