adding image extraction script

This commit is contained in:
2021-03-19 04:19:14 +00:00
parent 5728f34b3b
commit 6326de12f1
3 changed files with 27 additions and 0 deletions

BIN
.image-extraction/ROI_0.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 344 KiB

BIN
.image-extraction/img.jpeg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 128 KiB

27
.image-extraction/main.py Normal file
View File

@@ -0,0 +1,27 @@
import cv2
import numpy as np
image = cv2.imread('./img.jpeg')
original = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (3, 3), 0)
canny = cv2.Canny(blurred, 120, 255, 1)
kernel = np.ones((5,5),np.uint8)
dilate = cv2.dilate(canny, kernel, iterations=1)
# Find contours
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
# Iterate thorugh contours and filter for ROI
image_number = 0
for c in cnts:
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)
ROI = original[y:y+h, x:x+w]
cv2.imwrite("ROI_{}.png".format(image_number), ROI)
image_number += 1
cv2.imshow('canny', canny)
cv2.imshow('image', image)
cv2.waitKey(0)