forked from s444420/AL-2020
36 lines
1.1 KiB
Python
36 lines
1.1 KiB
Python
import numpy as np
|
|
import argparse
|
|
import imutils
|
|
import cv2
|
|
import matplotlib.pyplot as plt
|
|
import torch
|
|
from PIL import Image
|
|
|
|
path = "test1.jpg"
|
|
|
|
img = cv2.imread(path)
|
|
|
|
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
|
img_gray = cv2.GaussianBlur(img_gray, (5, 5), 0)
|
|
|
|
ret, im_th = cv2.threshold(img_gray, 90, 255, cv2.THRESH_BINARY_INV)
|
|
|
|
ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
|
|
|
rects = [cv2.boundingRect(ctr) for ctr in ctrs]
|
|
|
|
for rect in rects:
|
|
# Draw the rectangles
|
|
cv2.rectangle(img, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 3)
|
|
# Make the rectangular region around the digit
|
|
leng = int(rect[3] * 1.6)
|
|
pt1 = int(rect[1] + rect[3] // 2 - leng // 2)
|
|
pt2 = int(rect[0] + rect[2] // 2 - leng // 2)
|
|
roi = im_th[pt1:pt1+leng, pt2:pt2+leng]
|
|
# Resize the image
|
|
roi = cv2.resize(roi, (28, 28), interpolation=cv2.INTER_AREA)
|
|
roi = cv2.dilate(roi, (3, 3))
|
|
# Calculate the HOG features
|
|
|
|
cv2.imshow("Resulting Image with Rectangular ROIs", img)
|
|
cv2.waitKey() |