Add Java and Python code for the following imgproc tutorials: Canny, Remap, threshold and threshold inRange. Use HSV colorspace instead of RGB for inRange threshold tutorial.
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
from __future__ import print_function
|
||||
import cv2 as cv
|
||||
import argparse
|
||||
|
||||
max_lowThreshold = 100
|
||||
window_name = 'Edge Map'
|
||||
title_trackbar = 'Min Threshold:'
|
||||
ratio = 3
|
||||
kernel_size = 3
|
||||
|
||||
def CannyThreshold(val):
|
||||
low_threshold = val
|
||||
img_blur = cv.blur(src_gray, (3,3))
|
||||
detected_edges = cv.Canny(img_blur, low_threshold, low_threshold*ratio, kernel_size)
|
||||
mask = detected_edges != 0
|
||||
dst = src * (mask[:,:,None].astype(src.dtype))
|
||||
cv.imshow(window_name, dst)
|
||||
|
||||
parser = argparse.ArgumentParser(description='Code for Canny Edge Detector tutorial.')
|
||||
parser.add_argument('--input', help='Path to input image.', default='../data/fruits.jpg')
|
||||
args = parser.parse_args()
|
||||
|
||||
src = cv.imread(args.input)
|
||||
if src is None:
|
||||
print('Could not open or find the image: ', args.input)
|
||||
exit(0)
|
||||
|
||||
src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
|
||||
|
||||
cv.namedWindow(window_name)
|
||||
cv.createTrackbar(title_trackbar, window_name , 0, max_lowThreshold, CannyThreshold)
|
||||
|
||||
CannyThreshold(0)
|
||||
cv.waitKey()
|
||||
@@ -0,0 +1,65 @@
|
||||
from __future__ import print_function
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
import argparse
|
||||
|
||||
## [Update]
|
||||
def update_map(ind, map_x, map_y):
|
||||
if ind == 0:
|
||||
for i in range(map_x.shape[0]):
|
||||
for j in range(map_x.shape[1]):
|
||||
if j > map_x.shape[1]*0.25 and j < map_x.shape[1]*0.75 and i > map_x.shape[0]*0.25 and i < map_x.shape[0]*0.75:
|
||||
map_x[i,j] = 2 * (j-map_x.shape[1]*0.25) + 0.5
|
||||
map_y[i,j] = 2 * (i-map_y.shape[0]*0.25) + 0.5
|
||||
else:
|
||||
map_x[i,j] = 0
|
||||
map_y[i,j] = 0
|
||||
elif ind == 1:
|
||||
for i in range(map_x.shape[0]):
|
||||
map_x[i,:] = [x for x in range(map_x.shape[1])]
|
||||
for j in range(map_y.shape[1]):
|
||||
map_y[:,j] = [map_y.shape[0]-y for y in range(map_y.shape[0])]
|
||||
elif ind == 2:
|
||||
for i in range(map_x.shape[0]):
|
||||
map_x[i,:] = [map_x.shape[1]-x for x in range(map_x.shape[1])]
|
||||
for j in range(map_y.shape[1]):
|
||||
map_y[:,j] = [y for y in range(map_y.shape[0])]
|
||||
elif ind == 3:
|
||||
for i in range(map_x.shape[0]):
|
||||
map_x[i,:] = [map_x.shape[1]-x for x in range(map_x.shape[1])]
|
||||
for j in range(map_y.shape[1]):
|
||||
map_y[:,j] = [map_y.shape[0]-y for y in range(map_y.shape[0])]
|
||||
## [Update]
|
||||
|
||||
parser = argparse.ArgumentParser(description='Code for Remapping tutorial.')
|
||||
parser.add_argument('--input', help='Path to input image.', default='../data/chicky_512.png')
|
||||
args = parser.parse_args()
|
||||
|
||||
## [Load]
|
||||
src = cv.imread(args.input, cv.IMREAD_COLOR)
|
||||
if src is None:
|
||||
print('Could not open or find the image: ', args.input)
|
||||
exit(0)
|
||||
## [Load]
|
||||
|
||||
## [Create]
|
||||
map_x = np.zeros((src.shape[0], src.shape[1]), dtype=np.float32)
|
||||
map_y = np.zeros((src.shape[0], src.shape[1]), dtype=np.float32)
|
||||
## [Create]
|
||||
|
||||
## [Window]
|
||||
window_name = 'Remap demo'
|
||||
cv.namedWindow(window_name)
|
||||
## [Window]
|
||||
|
||||
## [Loop]
|
||||
ind = 0
|
||||
while True:
|
||||
update_map(ind, map_x, map_y)
|
||||
ind = (ind + 1) % 4
|
||||
dst = cv.remap(src, map_x, map_y, cv.INTER_LINEAR)
|
||||
cv.imshow(window_name, dst)
|
||||
c = cv.waitKey(1000)
|
||||
if c == 27:
|
||||
break
|
||||
## [Loop]
|
||||
@@ -0,0 +1,54 @@
|
||||
from __future__ import print_function
|
||||
import cv2 as cv
|
||||
import argparse
|
||||
|
||||
max_value = 255
|
||||
max_type = 4
|
||||
max_binary_value = 255
|
||||
trackbar_type = 'Type: \n 0: Binary \n 1: Binary Inverted \n 2: Truncate \n 3: To Zero \n 4: To Zero Inverted'
|
||||
trackbar_value = 'Value'
|
||||
window_name = 'Threshold Demo'
|
||||
|
||||
## [Threshold_Demo]
|
||||
def Threshold_Demo(val):
|
||||
#0: Binary
|
||||
#1: Binary Inverted
|
||||
#2: Threshold Truncated
|
||||
#3: Threshold to Zero
|
||||
#4: Threshold to Zero Inverted
|
||||
threshold_type = cv.getTrackbarPos(trackbar_type, window_name)
|
||||
threshold_value = cv.getTrackbarPos(trackbar_value, window_name)
|
||||
_, dst = cv.threshold(src_gray, threshold_value, max_binary_value, threshold_type )
|
||||
cv.imshow(window_name, dst)
|
||||
## [Threshold_Demo]
|
||||
|
||||
parser = argparse.ArgumentParser(description='Code for Basic Thresholding Operations tutorial.')
|
||||
parser.add_argument('--input', help='Path to input image.', default='../data/stuff.jpg')
|
||||
args = parser.parse_args()
|
||||
|
||||
## [load]
|
||||
# Load an image
|
||||
src = cv.imread(args.input)
|
||||
if src is None:
|
||||
print('Could not open or find the image: ', args.input)
|
||||
exit(0)
|
||||
# Convert the image to Gray
|
||||
src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY);
|
||||
## [load]
|
||||
|
||||
## [window]
|
||||
# Create a window to display results
|
||||
cv.namedWindow(window_name)
|
||||
## [window]
|
||||
|
||||
## [trackbar]
|
||||
# Create Trackbar to choose type of Threshold
|
||||
cv.createTrackbar(trackbar_type, window_name , 3, max_type, Threshold_Demo)
|
||||
# Create Trackbar to choose Threshold value
|
||||
cv.createTrackbar(trackbar_value, window_name , 0, max_value, Threshold_Demo)
|
||||
## [trackbar]
|
||||
|
||||
# Call the function to initialize
|
||||
Threshold_Demo(0)
|
||||
# Wait until user finishes program
|
||||
cv.waitKey()
|
||||
@@ -0,0 +1,107 @@
|
||||
from __future__ import print_function
|
||||
import cv2 as cv
|
||||
import argparse
|
||||
|
||||
max_value = 255
|
||||
max_value_H = 360//2
|
||||
low_H = 0
|
||||
low_S = 0
|
||||
low_V = 0
|
||||
high_H = max_value_H
|
||||
high_S = max_value
|
||||
high_V = max_value
|
||||
window_capture_name = 'Video Capture'
|
||||
window_detection_name = 'Object Detection'
|
||||
low_H_name = 'Low H'
|
||||
low_S_name = 'Low S'
|
||||
low_V_name = 'Low V'
|
||||
high_H_name = 'High H'
|
||||
high_S_name = 'High S'
|
||||
high_V_name = 'High V'
|
||||
|
||||
## [low]
|
||||
def on_low_H_thresh_trackbar(val):
|
||||
global low_H
|
||||
global high_H
|
||||
low_H = val
|
||||
low_H = min(high_H-1, low_H)
|
||||
cv.setTrackbarPos(low_H_name, window_detection_name, low_H)
|
||||
## [low]
|
||||
|
||||
## [high]
|
||||
def on_high_H_thresh_trackbar(val):
|
||||
global low_H
|
||||
global high_H
|
||||
high_H = val
|
||||
high_H = max(high_H, low_H+1)
|
||||
cv.setTrackbarPos(high_H_name, window_detection_name, high_H)
|
||||
## [high]
|
||||
|
||||
def on_low_S_thresh_trackbar(val):
|
||||
global low_S
|
||||
global high_S
|
||||
low_S = val
|
||||
low_S = min(high_S-1, low_S)
|
||||
cv.setTrackbarPos(low_S_name, window_detection_name, low_S)
|
||||
|
||||
def on_high_S_thresh_trackbar(val):
|
||||
global low_S
|
||||
global high_S
|
||||
high_S = val
|
||||
high_S = max(high_S, low_S+1)
|
||||
cv.setTrackbarPos(high_S_name, window_detection_name, high_S)
|
||||
|
||||
def on_low_V_thresh_trackbar(val):
|
||||
global low_V
|
||||
global high_V
|
||||
low_V = val
|
||||
low_V = min(high_V-1, low_V)
|
||||
cv.setTrackbarPos(low_V_name, window_detection_name, low_V)
|
||||
|
||||
def on_high_V_thresh_trackbar(val):
|
||||
global low_V
|
||||
global high_V
|
||||
high_V = val
|
||||
high_V = max(high_V, low_V+1)
|
||||
cv.setTrackbarPos(high_V_name, window_detection_name, high_V)
|
||||
|
||||
parser = argparse.ArgumentParser(description='Code for Thresholding Operations using inRange tutorial.')
|
||||
parser.add_argument('--camera', help='Camera devide number.', default=0, type=int)
|
||||
args = parser.parse_args()
|
||||
|
||||
## [cap]
|
||||
cap = cv.VideoCapture(args.camera)
|
||||
## [cap]
|
||||
|
||||
## [window]
|
||||
cv.namedWindow(window_capture_name)
|
||||
cv.namedWindow(window_detection_name)
|
||||
## [window]
|
||||
|
||||
## [trackbar]
|
||||
cv.createTrackbar(low_H_name, window_detection_name , low_H, max_value_H, on_low_H_thresh_trackbar)
|
||||
cv.createTrackbar(high_H_name, window_detection_name , high_H, max_value_H, on_high_H_thresh_trackbar)
|
||||
cv.createTrackbar(low_S_name, window_detection_name , low_S, max_value, on_low_S_thresh_trackbar)
|
||||
cv.createTrackbar(high_S_name, window_detection_name , high_S, max_value, on_high_S_thresh_trackbar)
|
||||
cv.createTrackbar(low_V_name, window_detection_name , low_V, max_value, on_low_V_thresh_trackbar)
|
||||
cv.createTrackbar(high_V_name, window_detection_name , high_V, max_value, on_high_V_thresh_trackbar)
|
||||
## [trackbar]
|
||||
|
||||
while True:
|
||||
## [while]
|
||||
ret, frame = cap.read()
|
||||
if frame is None:
|
||||
break
|
||||
|
||||
frame_HSV = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
|
||||
frame_threshold = cv.inRange(frame_HSV, (low_H, low_S, low_V), (high_H, high_S, high_V));
|
||||
## [while]
|
||||
|
||||
## [show]
|
||||
cv.imshow(window_capture_name, frame)
|
||||
cv.imshow(window_detection_name, frame_threshold)
|
||||
## [show]
|
||||
|
||||
key = cv.waitKey(30)
|
||||
if key == ord('q') or key == 27:
|
||||
break
|
||||
Reference in New Issue
Block a user