1 | initial version |
Using mouseevent. It is straight forward. It will save iterator files
Press "c" to exit Code:
import cv2 as cv
refPt = [] cropping = False
def click_crop(event, x, y, flags, param): global refPt, cropping, num if event == cv.EVENT_LBUTTONDOWN: refPt = [(x, y)] cropping = True
if event == cv.EVENT_LBUTTONUP:
num += 1
refPt.append((x, y))
cropping = False
# draw a rectangle around the region of interest
cv.rectangle(img, refPt[0], refPt[1], (0, 255, 0), 2)
if __name__ == "__main__": num = 0 # Create a window windowName = 'Click and Crop' img = cv.imread('text.png', cv.IMREAD_COLOR) clone = img.copy() cv.namedWindow(windowName) # bind the callback function to window cv.setMouseCallback(windowName, click_crop)
num = 0
if len(refPt) == 2:
num += 1
while True:
cv.imshow(windowName, img)
key = cv.waitKey(1)
if key == ord("r"): #pressed "r" to reset
img = clone.copy()
elif key == ord("s"): #pressed "s" to save
roi = clone[refPt[0][1]:refPt[1][1], refPt[0][0]:refPt[1][0]]
cv.imwrite('roi{}.jpg'.format(num), roi)
elif key == ord("c"): #pressed "c" to exit
break
cv.destroyAllWindows()
Output:
2 | No.2 Revision |
Using mouseevent. It is straight forward. It will save iterator files
import cv2 as
Output:
3 | No.3 Revision |
Using mouseevent. It is straight forward. It will save iterator files
import cv2 as cv
cv refPt = []
cropping = False
# mouse callback function
False def click_crop(event, x, y, flags, param):
global refPt, cropping, num
if event == cv.EVENT_LBUTTONDOWN:
refPt = [(x, y)]
cropping = True
True
if event == cv.EVENT_LBUTTONUP:
num += 1
refPt.append((x, y))
cropping = False
# draw a rectangle around the region of interest
False
cv.rectangle(img, refPt[0], refPt[1], (0, 255, 0), 2)
if __name__ == "__main__":
num = 0
# Create a window
0
windowName = 'Click and Crop'
img = cv.imread('text.png', cv.IMREAD_COLOR)
clone = img.copy()
cv.namedWindow(windowName)
# bind the callback function to window
cv.setMouseCallback(windowName, click_crop)
num = 0
if len(refPt) == 2:
num += 1
while True:
cv.imshow(windowName, img)
key = cv.waitKey(1)
if key == ord("r"): #pressed "r" to reset
img = clone.copy()
elif key == ord("s"): #pressed "s" to save
roi = clone[refPt[0][1]:refPt[1][1], refPt[0][0]:refPt[1][0]]
cv.imwrite('roi{}.jpg'.format(num), roi)
elif key == ord("c"): #pressed "c" to exit
break
cv.destroyAllWindows()
Output:
4 | No.4 Revision |
Using mouseevent. It is straight forward. It will save iterator files
Code:
import cv2 as
Output:
5 | No.5 Revision |
Using mouseevent. It is straight forward. It will save iterator files
Code:
import cv2 as cv
refPt = []
cropping = False
def click_crop(event, x, y, flags, param):
global refPt, cropping, num
if event == cv.EVENT_LBUTTONDOWN:
refPt = [(x, y)]
cropping = True
if event == cv.EVENT_LBUTTONUP:
num += 1
refPt.append((x, y))
cropping = False
cv.rectangle(img, refPt[0], refPt[1], (0, 255, 0), 2)
if __name__ == "__main__":
num = 0
windowName = 'Click and Crop'
img = cv.imread('text.png', cv.IMREAD_COLOR)
clone = img.copy()
cv.namedWindow(windowName)
cv.setMouseCallback(windowName, click_crop)
num = 0
if len(refPt) == 2:
num += 1
while True:
cv.imshow(windowName, img)
key = cv.waitKey(1)
if key == ord("r"):
img = clone.copy()
elif key == ord("s"):
roi = clone[refPt[0][1]:refPt[1][1], refPt[0][0]:refPt[1][0]]
cv.imwrite('roi{}.jpg'.format(num), roi)
elif key == ord("c"):
break
cv.destroyAllWindows()
Output:
6 | No.6 Revision |
Using mouseevent. It is straight forward. It will save iterator files
Code:
import cv2 as cv
refPt = []
cropping = False
def click_crop(event, x, y, flags, param):
global refPt, cropping, num
if event == cv.EVENT_LBUTTONDOWN:
refPt = [(x, y)]
cropping = True
if event == cv.EVENT_LBUTTONUP:
num += 1
refPt.append((x, y))
cropping = False
cv.rectangle(img, refPt[0], refPt[1], (0, 255, 0), 2)
if __name__ == "__main__":
num = 0
windowName = 'Click and Crop'
img = cv.imread('text.png', cv.IMREAD_COLOR)
clone = img.copy()
cv.namedWindow(windowName)
cv.setMouseCallback(windowName, click_crop)
num = 0
if len(refPt) == 2:
num += 1
while True:
cv.imshow(windowName, img)
key = cv.waitKey(1)
if key == ord("r"):
img = clone.copy()
elif key == ord("s"):
roi = clone[refPt[0][1]:refPt[1][1], refPt[0][0]:refPt[1][0]]
cv.imwrite('roi{}.jpg'.format(num), roi)
elif key == ord("c"):
break
cv.destroyAllWindows()
Output:
@Agustin. Don't used cv2.drawContours
. You get both rectangles. I am not going to post screenshot for cropped.
I merely got 2 images. Second images contained purple rectangle and 3rd image contained heavy grey border. I am running out of my time. I will attempt to get it back sooner.
import cv2
image = cv2.imread('text.png')
#image = cv2.imread('text4.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
thresh = cv2.threshold(blur, 127, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Fill contours
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
morph = 255 - cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=3)
#morph = 255 - cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=6)
close = 255 - morph
opening = cv2.morphologyEx(close, cv2.MORPH_OPEN, kernel, iterations=3)
# Busca los contornos y dibuja los resultados
ROI_number = 0
cnts = cv2.findContours(opening, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
x,y,w,h = cv2.boundingRect(c)
ROI = image[y:y+h, x:x+w]
cv2.rectangle(image,(x,y),(x+w,y+h),(36,255,12),2)
#cv2.imwrite('images_{}.png'.format(ROI_number), ROI)
cv2.imwrite('text3.jpg', image)
ROI_number += 1
print("Cantidad de entidades ", len(cnts))
cv2.imshow('thresh', thresh)
cv2.imshow('opening', blur)
cv2.imshow('image', image)
cv2.waitKey()
Output: for text:
Output for text4 using iterations=6:
7 | No.7 Revision |
Using mouseevent. It is straight forward. It will save iterator files
Code:
import cv2 as cv
refPt = []
cropping = False
def click_crop(event, x, y, flags, param):
global refPt, cropping, num
if event == cv.EVENT_LBUTTONDOWN:
refPt = [(x, y)]
cropping = True
if event == cv.EVENT_LBUTTONUP:
num += 1
refPt.append((x, y))
cropping = False
cv.rectangle(img, refPt[0], refPt[1], (0, 255, 0), 2)
if __name__ == "__main__":
num = 0
windowName = 'Click and Crop'
img = cv.imread('text.png', cv.IMREAD_COLOR)
clone = img.copy()
cv.namedWindow(windowName)
cv.setMouseCallback(windowName, click_crop)
num = 0
if len(refPt) == 2:
num += 1
while True:
cv.imshow(windowName, img)
key = cv.waitKey(1)
if key == ord("r"):
img = clone.copy()
elif key == ord("s"):
roi = clone[refPt[0][1]:refPt[1][1], refPt[0][0]:refPt[1][0]]
cv.imwrite('roi{}.jpg'.format(num), roi)
elif key == ord("c"):
break
cv.destroyAllWindows()
Output:
@Agustin. Don't used cv2.drawContours
. You get both rectangles. I am not going to post screenshot for cropped.
I merely got 2 images. Second images contained purple rectangle and 3rd image contained heavy grey border. I am running out of my time. I will attempt to get it back sooner.
import cv2
image = cv2.imread('text.png')
#image = cv2.imread('text4.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
thresh = cv2.threshold(blur, 127, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Fill contours
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
morph = 255 - cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=3)
#morph = 255 - cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=6)
close = 255 - morph
opening = cv2.morphologyEx(close, cv2.MORPH_OPEN, kernel, iterations=3)
# Busca los contornos y dibuja los resultados
ROI_number = 0
cnts = cv2.findContours(opening, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
x,y,w,h = cv2.boundingRect(c)
ROI = image[y:y+h, x:x+w]
cv2.rectangle(image,(x,y),(x+w,y+h),(36,255,12),2)
#cv2.imwrite('images_{}.png'.format(ROI_number), cv2.imwrite('images_{}.png'.format(ROI_number), ROI)
cv2.imwrite('text3.jpg', image)
ROI_number += 1
print("Cantidad de entidades ", len(cnts))
cv2.imshow('thresh', thresh)
cv2.imshow('opening', blur)
cv2.imshow('image', image)
cv2.waitKey()
Output: for text:
Output for text4 using iterations=6: