python: 'cv2.' -> 'cv.' via 'import cv2 as cv'
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
@brief Sample code that shows how to implement your own linear filters by using filter2D function
|
||||
"""
|
||||
import sys
|
||||
import cv2
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ def main(argv):
|
||||
imageName = argv[0] if len(argv) > 0 else "../data/lena.jpg"
|
||||
|
||||
# Loads an image
|
||||
src = cv2.imread(imageName, cv2.IMREAD_COLOR)
|
||||
src = cv.imread(imageName, cv.IMREAD_COLOR)
|
||||
|
||||
# Check if image is loaded fine
|
||||
if src is None:
|
||||
@@ -37,11 +37,11 @@ def main(argv):
|
||||
## [update_kernel]
|
||||
## [apply_filter]
|
||||
# Apply filter
|
||||
dst = cv2.filter2D(src, ddepth, kernel)
|
||||
dst = cv.filter2D(src, ddepth, kernel)
|
||||
## [apply_filter]
|
||||
cv2.imshow(window_name, dst)
|
||||
cv.imshow(window_name, dst)
|
||||
|
||||
c = cv2.waitKey(500)
|
||||
c = cv.waitKey(500)
|
||||
if c == 27:
|
||||
break
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import sys
|
||||
import cv2
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ def main(argv):
|
||||
filename = argv[0] if len(argv) > 0 else default_file
|
||||
|
||||
# Loads an image
|
||||
src = cv2.imread(filename, cv2.IMREAD_COLOR)
|
||||
src = cv.imread(filename, cv.IMREAD_COLOR)
|
||||
|
||||
# Check if image is loaded fine
|
||||
if src is None:
|
||||
@@ -20,17 +20,17 @@ def main(argv):
|
||||
|
||||
## [convert_to_gray]
|
||||
# Convert it to gray
|
||||
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
|
||||
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
|
||||
## [convert_to_gray]
|
||||
|
||||
## [reduce_noise]
|
||||
# Reduce the noise to avoid false circle detection
|
||||
gray = cv2.medianBlur(gray, 5)
|
||||
gray = cv.medianBlur(gray, 5)
|
||||
## [reduce_noise]
|
||||
|
||||
## [houghcircles]
|
||||
rows = gray.shape[0]
|
||||
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, rows / 8,
|
||||
circles = cv.HoughCircles(gray, cv.HOUGH_GRADIENT, 1, rows / 8,
|
||||
param1=100, param2=30,
|
||||
minRadius=1, maxRadius=30)
|
||||
## [houghcircles]
|
||||
@@ -41,15 +41,15 @@ def main(argv):
|
||||
for i in circles[0, :]:
|
||||
center = (i[0], i[1])
|
||||
# circle center
|
||||
cv2.circle(src, center, 1, (0, 100, 100), 3)
|
||||
cv.circle(src, center, 1, (0, 100, 100), 3)
|
||||
# circle outline
|
||||
radius = i[2]
|
||||
cv2.circle(src, center, radius, (255, 0, 255), 3)
|
||||
cv.circle(src, center, radius, (255, 0, 255), 3)
|
||||
## [draw]
|
||||
|
||||
## [display]
|
||||
cv2.imshow("detected circles", src)
|
||||
cv2.waitKey(0)
|
||||
cv.imshow("detected circles", src)
|
||||
cv.waitKey(0)
|
||||
## [display]
|
||||
|
||||
return 0
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
"""
|
||||
import sys
|
||||
import math
|
||||
import cv2
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ def main(argv):
|
||||
filename = argv[0] if len(argv) > 0 else default_file
|
||||
|
||||
# Loads an image
|
||||
src = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
|
||||
src = cv.imread(filename, cv.IMREAD_GRAYSCALE)
|
||||
|
||||
# Check if image is loaded fine
|
||||
if src is None:
|
||||
@@ -25,16 +25,16 @@ def main(argv):
|
||||
|
||||
## [edge_detection]
|
||||
# Edge detection
|
||||
dst = cv2.Canny(src, 50, 200, None, 3)
|
||||
dst = cv.Canny(src, 50, 200, None, 3)
|
||||
## [edge_detection]
|
||||
|
||||
# Copy edges to the images that will display the results in BGR
|
||||
cdst = cv2.cvtColor(dst, cv2.COLOR_GRAY2BGR)
|
||||
cdst = cv.cvtColor(dst, cv.COLOR_GRAY2BGR)
|
||||
cdstP = np.copy(cdst)
|
||||
|
||||
## [hough_lines]
|
||||
# Standard Hough Line Transform
|
||||
lines = cv2.HoughLines(dst, 1, np.pi / 180, 150, None, 0, 0)
|
||||
lines = cv.HoughLines(dst, 1, np.pi / 180, 150, None, 0, 0)
|
||||
## [hough_lines]
|
||||
## [draw_lines]
|
||||
# Draw the lines
|
||||
@@ -49,29 +49,29 @@ def main(argv):
|
||||
pt1 = (int(x0 + 1000*(-b)), int(y0 + 1000*(a)))
|
||||
pt2 = (int(x0 - 1000*(-b)), int(y0 - 1000*(a)))
|
||||
|
||||
cv2.line(cdst, pt1, pt2, (0,0,255), 3, cv2.LINE_AA)
|
||||
cv.line(cdst, pt1, pt2, (0,0,255), 3, cv.LINE_AA)
|
||||
## [draw_lines]
|
||||
|
||||
## [hough_lines_p]
|
||||
# Probabilistic Line Transform
|
||||
linesP = cv2.HoughLinesP(dst, 1, np.pi / 180, 50, None, 50, 10)
|
||||
linesP = cv.HoughLinesP(dst, 1, np.pi / 180, 50, None, 50, 10)
|
||||
## [hough_lines_p]
|
||||
## [draw_lines_p]
|
||||
# Draw the lines
|
||||
if linesP is not None:
|
||||
for i in range(0, len(linesP)):
|
||||
l = linesP[i][0]
|
||||
cv2.line(cdstP, (l[0], l[1]), (l[2], l[3]), (0,0,255), 3, cv2.LINE_AA)
|
||||
cv.line(cdstP, (l[0], l[1]), (l[2], l[3]), (0,0,255), 3, cv.LINE_AA)
|
||||
## [draw_lines_p]
|
||||
## [imshow]
|
||||
# Show results
|
||||
cv2.imshow("Source", src)
|
||||
cv2.imshow("Detected Lines (in red) - Standard Hough Line Transform", cdst)
|
||||
cv2.imshow("Detected Lines (in red) - Probabilistic Line Transform", cdstP)
|
||||
cv.imshow("Source", src)
|
||||
cv.imshow("Detected Lines (in red) - Standard Hough Line Transform", cdst)
|
||||
cv.imshow("Detected Lines (in red) - Probabilistic Line Transform", cdstP)
|
||||
## [imshow]
|
||||
## [exit]
|
||||
# Wait and Exit
|
||||
cv2.waitKey()
|
||||
cv.waitKey()
|
||||
return 0
|
||||
## [exit]
|
||||
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
@brief Sample code showing how to detect edges using the Laplace operator
|
||||
"""
|
||||
import sys
|
||||
import cv2
|
||||
import cv2 as cv
|
||||
|
||||
def main(argv):
|
||||
# [variables]
|
||||
# Declare the variables we are going to use
|
||||
ddepth = cv2.CV_16S
|
||||
ddepth = cv.CV_16S
|
||||
kernel_size = 3
|
||||
window_name = "Laplace Demo"
|
||||
# [variables]
|
||||
@@ -16,7 +16,7 @@ def main(argv):
|
||||
# [load]
|
||||
imageName = argv[0] if len(argv) > 0 else "../data/lena.jpg"
|
||||
|
||||
src = cv2.imread(imageName, cv2.IMREAD_COLOR) # Load an image
|
||||
src = cv.imread(imageName, cv.IMREAD_COLOR) # Load an image
|
||||
|
||||
# Check if image is loaded fine
|
||||
if src is None:
|
||||
@@ -27,30 +27,30 @@ def main(argv):
|
||||
|
||||
# [reduce_noise]
|
||||
# Remove noise by blurring with a Gaussian filter
|
||||
src = cv2.GaussianBlur(src, (3, 3), 0)
|
||||
src = cv.GaussianBlur(src, (3, 3), 0)
|
||||
# [reduce_noise]
|
||||
|
||||
# [convert_to_gray]
|
||||
# Convert the image to grayscale
|
||||
src_gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
|
||||
src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
|
||||
# [convert_to_gray]
|
||||
|
||||
# Create Window
|
||||
cv2.namedWindow(window_name, cv2.WINDOW_AUTOSIZE)
|
||||
cv.namedWindow(window_name, cv.WINDOW_AUTOSIZE)
|
||||
|
||||
# [laplacian]
|
||||
# Apply Laplace function
|
||||
dst = cv2.Laplacian(src_gray, ddepth, kernel_size)
|
||||
dst = cv.Laplacian(src_gray, ddepth, kernel_size)
|
||||
# [laplacian]
|
||||
|
||||
# [convert]
|
||||
# converting back to uint8
|
||||
abs_dst = cv2.convertScaleAbs(dst)
|
||||
abs_dst = cv.convertScaleAbs(dst)
|
||||
# [convert]
|
||||
|
||||
# [display]
|
||||
cv2.imshow(window_name, abs_dst)
|
||||
cv2.waitKey(0)
|
||||
cv.imshow(window_name, abs_dst)
|
||||
cv.waitKey(0)
|
||||
# [display]
|
||||
|
||||
return 0
|
||||
|
||||
@@ -4,20 +4,20 @@
|
||||
"""
|
||||
import sys
|
||||
from random import randint
|
||||
import cv2
|
||||
import cv2 as cv
|
||||
|
||||
|
||||
def main(argv):
|
||||
## [variables]
|
||||
# First we declare the variables we are going to use
|
||||
borderType = cv2.BORDER_CONSTANT
|
||||
borderType = cv.BORDER_CONSTANT
|
||||
window_name = "copyMakeBorder Demo"
|
||||
## [variables]
|
||||
## [load]
|
||||
imageName = argv[0] if len(argv) > 0 else "../data/lena.jpg"
|
||||
|
||||
# Loads an image
|
||||
src = cv2.imread(imageName, cv2.IMREAD_COLOR)
|
||||
src = cv.imread(imageName, cv.IMREAD_COLOR)
|
||||
|
||||
# Check if image is loaded fine
|
||||
if src is None:
|
||||
@@ -33,7 +33,7 @@ def main(argv):
|
||||
' ** Press \'r\' to set the border to be replicated \n'
|
||||
' ** Press \'ESC\' to exit the program ')
|
||||
## [create_window]
|
||||
cv2.namedWindow(window_name, cv2.WINDOW_AUTOSIZE)
|
||||
cv.namedWindow(window_name, cv.WINDOW_AUTOSIZE)
|
||||
## [create_window]
|
||||
## [init_arguments]
|
||||
# Initialize arguments for the filter
|
||||
@@ -47,20 +47,20 @@ def main(argv):
|
||||
value = [randint(0, 255), randint(0, 255), randint(0, 255)]
|
||||
## [update_value]
|
||||
## [copymakeborder]
|
||||
dst = cv2.copyMakeBorder(src, top, bottom, left, right, borderType, None, value)
|
||||
dst = cv.copyMakeBorder(src, top, bottom, left, right, borderType, None, value)
|
||||
## [copymakeborder]
|
||||
## [display]
|
||||
cv2.imshow(window_name, dst)
|
||||
cv.imshow(window_name, dst)
|
||||
## [display]
|
||||
## [check_keypress]
|
||||
c = cv2.waitKey(500)
|
||||
c = cv.waitKey(500)
|
||||
|
||||
if c == 27:
|
||||
break
|
||||
elif c == 99: # 99 = ord('c')
|
||||
borderType = cv2.BORDER_CONSTANT
|
||||
borderType = cv.BORDER_CONSTANT
|
||||
elif c == 114: # 114 = ord('r')
|
||||
borderType = cv2.BORDER_REPLICATE
|
||||
borderType = cv.BORDER_REPLICATE
|
||||
## [check_keypress]
|
||||
return 0
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
@brief Sample code using Sobel and/or Scharr OpenCV functions to make a simple Edge Detector
|
||||
"""
|
||||
import sys
|
||||
import cv2
|
||||
import cv2 as cv
|
||||
|
||||
|
||||
def main(argv):
|
||||
@@ -12,7 +12,7 @@ def main(argv):
|
||||
window_name = ('Sobel Demo - Simple Edge Detector')
|
||||
scale = 1
|
||||
delta = 0
|
||||
ddepth = cv2.CV_16S
|
||||
ddepth = cv.CV_16S
|
||||
## [variables]
|
||||
|
||||
## [load]
|
||||
@@ -24,7 +24,7 @@ def main(argv):
|
||||
return -1
|
||||
|
||||
# Load the image
|
||||
src = cv2.imread(argv[0], cv2.IMREAD_COLOR)
|
||||
src = cv.imread(argv[0], cv.IMREAD_COLOR)
|
||||
|
||||
# Check if image is loaded fine
|
||||
if src is None:
|
||||
@@ -34,38 +34,38 @@ def main(argv):
|
||||
|
||||
## [reduce_noise]
|
||||
# Remove noise by blurring with a Gaussian filter ( kernel size = 3 )
|
||||
src = cv2.GaussianBlur(src, (3, 3), 0)
|
||||
src = cv.GaussianBlur(src, (3, 3), 0)
|
||||
## [reduce_noise]
|
||||
|
||||
## [convert_to_gray]
|
||||
# Convert the image to grayscale
|
||||
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
|
||||
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
|
||||
## [convert_to_gray]
|
||||
|
||||
## [sobel]
|
||||
# Gradient-X
|
||||
# grad_x = cv2.Scharr(gray,ddepth,1,0)
|
||||
grad_x = cv2.Sobel(gray, ddepth, 1, 0, ksize=3, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT)
|
||||
# grad_x = cv.Scharr(gray,ddepth,1,0)
|
||||
grad_x = cv.Sobel(gray, ddepth, 1, 0, ksize=3, scale=scale, delta=delta, borderType=cv.BORDER_DEFAULT)
|
||||
|
||||
# Gradient-Y
|
||||
# grad_y = cv2.Scharr(gray,ddepth,0,1)
|
||||
grad_y = cv2.Sobel(gray, ddepth, 0, 1, ksize=3, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT)
|
||||
# grad_y = cv.Scharr(gray,ddepth,0,1)
|
||||
grad_y = cv.Sobel(gray, ddepth, 0, 1, ksize=3, scale=scale, delta=delta, borderType=cv.BORDER_DEFAULT)
|
||||
## [sobel]
|
||||
|
||||
## [convert]
|
||||
# converting back to uint8
|
||||
abs_grad_x = cv2.convertScaleAbs(grad_x)
|
||||
abs_grad_y = cv2.convertScaleAbs(grad_y)
|
||||
abs_grad_x = cv.convertScaleAbs(grad_x)
|
||||
abs_grad_y = cv.convertScaleAbs(grad_y)
|
||||
## [convert]
|
||||
|
||||
## [blend]
|
||||
## Total Gradient (approximate)
|
||||
grad = cv2.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)
|
||||
grad = cv.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)
|
||||
## [blend]
|
||||
|
||||
## [display]
|
||||
cv2.imshow(window_name, grad)
|
||||
cv2.waitKey(0)
|
||||
cv.imshow(window_name, grad)
|
||||
cv.waitKey(0)
|
||||
## [display]
|
||||
|
||||
return 0
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
|
||||
import cv2
|
||||
import cv2 as cv
|
||||
|
||||
alpha = 0.5
|
||||
|
||||
@@ -15,8 +15,8 @@ else:
|
||||
if 0 <= alpha <= 1:
|
||||
alpha = input_alpha
|
||||
## [load]
|
||||
src1 = cv2.imread('../../../../data/LinuxLogo.jpg')
|
||||
src2 = cv2.imread('../../../../data/WindowsLogo.jpg')
|
||||
src1 = cv.imread('../../../../data/LinuxLogo.jpg')
|
||||
src2 = cv.imread('../../../../data/WindowsLogo.jpg')
|
||||
## [load]
|
||||
if src1 is None:
|
||||
print ("Error loading src1")
|
||||
@@ -26,10 +26,10 @@ elif src2 is None:
|
||||
exit(-1)
|
||||
## [blend_images]
|
||||
beta = (1.0 - alpha)
|
||||
dst = cv2.addWeighted(src1, alpha, src2, beta, 0.0)
|
||||
dst = cv.addWeighted(src1, alpha, src2, beta, 0.0)
|
||||
## [blend_images]
|
||||
## [display]
|
||||
cv2.imshow('dst', dst)
|
||||
cv2.waitKey(0)
|
||||
cv.imshow('dst', dst)
|
||||
cv.waitKey(0)
|
||||
## [display]
|
||||
cv2.destroyAllWindows()
|
||||
cv.destroyAllWindows()
|
||||
|
||||
+13
-13
@@ -1,4 +1,4 @@
|
||||
import cv2
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
|
||||
W = 400
|
||||
@@ -7,7 +7,7 @@ def my_ellipse(img, angle):
|
||||
thickness = 2
|
||||
line_type = 8
|
||||
|
||||
cv2.ellipse(img,
|
||||
cv.ellipse(img,
|
||||
(W / 2, W / 2),
|
||||
(W / 4, W / 16),
|
||||
angle,
|
||||
@@ -22,7 +22,7 @@ def my_filled_circle(img, center):
|
||||
thickness = -1
|
||||
line_type = 8
|
||||
|
||||
cv2.circle(img,
|
||||
cv.circle(img,
|
||||
center,
|
||||
W / 32,
|
||||
(0, 0, 255),
|
||||
@@ -45,16 +45,16 @@ def my_polygon(img):
|
||||
[W / 4, 3 * W / 8], [13 * W / 32, 3 * W / 8],
|
||||
[5 * W / 16, 13 * W / 16], [W / 4, 13 * W / 16]], np.int32)
|
||||
ppt = ppt.reshape((-1, 1, 2))
|
||||
cv2.fillPoly(img, [ppt], (255, 255, 255), line_type)
|
||||
cv.fillPoly(img, [ppt], (255, 255, 255), line_type)
|
||||
# Only drawind the lines would be:
|
||||
# cv2.polylines(img, [ppt], True, (255, 0, 255), line_type)
|
||||
# cv.polylines(img, [ppt], True, (255, 0, 255), line_type)
|
||||
## [my_polygon]
|
||||
## [my_line]
|
||||
def my_line(img, start, end):
|
||||
thickness = 2
|
||||
line_type = 8
|
||||
|
||||
cv2.line(img,
|
||||
cv.line(img,
|
||||
start,
|
||||
end,
|
||||
(0, 0, 0),
|
||||
@@ -92,7 +92,7 @@ my_filled_circle(atom_image, (W / 2, W / 2))
|
||||
my_polygon(rook_image)
|
||||
## [rectangle]
|
||||
# 2.b. Creating rectangles
|
||||
cv2.rectangle(rook_image,
|
||||
cv.rectangle(rook_image,
|
||||
(0, 7 * W / 8),
|
||||
(W, W),
|
||||
(0, 255, 255),
|
||||
@@ -106,10 +106,10 @@ my_line(rook_image, (W / 4, 7 * W / 8), (W / 4, W))
|
||||
my_line(rook_image, (W / 2, 7 * W / 8), (W / 2, W))
|
||||
my_line(rook_image, (3 * W / 4, 7 * W / 8), (3 * W / 4, W))
|
||||
## [draw_rook]
|
||||
cv2.imshow(atom_window, atom_image)
|
||||
cv2.moveWindow(atom_window, 0, 200)
|
||||
cv2.imshow(rook_window, rook_image)
|
||||
cv2.moveWindow(rook_window, W, 200)
|
||||
cv.imshow(atom_window, atom_image)
|
||||
cv.moveWindow(atom_window, 0, 200)
|
||||
cv.imshow(rook_window, rook_image)
|
||||
cv.moveWindow(rook_window, W, 200)
|
||||
|
||||
cv2.waitKey(0)
|
||||
cv2.destroyAllWindows()
|
||||
cv.waitKey(0)
|
||||
cv.destroyAllWindows()
|
||||
|
||||
+15
-15
@@ -1,7 +1,7 @@
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
|
||||
import cv2
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
|
||||
|
||||
@@ -19,34 +19,34 @@ def main(argv):
|
||||
|
||||
filename = argv[0] if len(argv) > 0 else "../../../../data/lena.jpg"
|
||||
|
||||
I = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
|
||||
I = cv.imread(filename, cv.IMREAD_GRAYSCALE)
|
||||
if I is None:
|
||||
print('Error opening image')
|
||||
return -1
|
||||
## [expand]
|
||||
rows, cols = I.shape
|
||||
m = cv2.getOptimalDFTSize( rows )
|
||||
n = cv2.getOptimalDFTSize( cols )
|
||||
padded = cv2.copyMakeBorder(I, 0, m - rows, 0, n - cols, cv2.BORDER_CONSTANT, value=[0, 0, 0])
|
||||
m = cv.getOptimalDFTSize( rows )
|
||||
n = cv.getOptimalDFTSize( cols )
|
||||
padded = cv.copyMakeBorder(I, 0, m - rows, 0, n - cols, cv.BORDER_CONSTANT, value=[0, 0, 0])
|
||||
## [expand]
|
||||
## [complex_and_real]
|
||||
planes = [np.float32(padded), np.zeros(padded.shape, np.float32)]
|
||||
complexI = cv2.merge(planes) # Add to the expanded another plane with zeros
|
||||
complexI = cv.merge(planes) # Add to the expanded another plane with zeros
|
||||
## [complex_and_real]
|
||||
## [dft]
|
||||
cv2.dft(complexI, complexI) # this way the result may fit in the source matrix
|
||||
cv.dft(complexI, complexI) # this way the result may fit in the source matrix
|
||||
## [dft]
|
||||
# compute the magnitude and switch to logarithmic scale
|
||||
# = > log(1 + sqrt(Re(DFT(I)) ^ 2 + Im(DFT(I)) ^ 2))
|
||||
## [magnitude]
|
||||
cv2.split(complexI, planes) # planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
|
||||
cv2.magnitude(planes[0], planes[1], planes[0])# planes[0] = magnitude
|
||||
cv.split(complexI, planes) # planes[0] = Re(DFT(I), planes[1] = Im(DFT(I))
|
||||
cv.magnitude(planes[0], planes[1], planes[0])# planes[0] = magnitude
|
||||
magI = planes[0]
|
||||
## [magnitude]
|
||||
## [log]
|
||||
matOfOnes = np.ones(magI.shape, dtype=magI.dtype)
|
||||
cv2.add(matOfOnes, magI, magI) # switch to logarithmic scale
|
||||
cv2.log(magI, magI)
|
||||
cv.add(matOfOnes, magI, magI) # switch to logarithmic scale
|
||||
cv.log(magI, magI)
|
||||
## [log]
|
||||
## [crop_rearrange]
|
||||
magI_rows, magI_cols = magI.shape
|
||||
@@ -69,12 +69,12 @@ def main(argv):
|
||||
magI[0:cx, cy:cy + cy] = tmp
|
||||
## [crop_rearrange]
|
||||
## [normalize]
|
||||
cv2.normalize(magI, magI, 0, 1, cv2.NORM_MINMAX) # Transform the matrix with float values into a
|
||||
cv.normalize(magI, magI, 0, 1, cv.NORM_MINMAX) # Transform the matrix with float values into a
|
||||
## viewable image form(float between values 0 and 1).
|
||||
## [normalize]
|
||||
cv2.imshow("Input Image" , I ) # Show the result
|
||||
cv2.imshow("spectrum magnitude", magI)
|
||||
cv2.waitKey()
|
||||
cv.imshow("Input Image" , I ) # Show the result
|
||||
cv.imshow("spectrum magnitude", magI)
|
||||
cv.waitKey()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
||||
|
||||
@@ -3,7 +3,7 @@ import sys
|
||||
import time
|
||||
|
||||
import numpy as np
|
||||
import cv2
|
||||
import cv2 as cv
|
||||
|
||||
## [basic_method]
|
||||
def is_grayscale(my_image):
|
||||
@@ -23,7 +23,7 @@ def sharpen(my_image):
|
||||
if is_grayscale(my_image):
|
||||
height, width = my_image.shape
|
||||
else:
|
||||
my_image = cv2.cvtColor(my_image, cv2.CV_8U)
|
||||
my_image = cv.cvtColor(my_image, cv.CV_8U)
|
||||
height, width, n_channels = my_image.shape
|
||||
|
||||
result = np.zeros(my_image.shape, my_image.dtype)
|
||||
@@ -47,13 +47,13 @@ def sharpen(my_image):
|
||||
def main(argv):
|
||||
filename = "../../../../data/lena.jpg"
|
||||
|
||||
img_codec = cv2.IMREAD_COLOR
|
||||
img_codec = cv.IMREAD_COLOR
|
||||
if argv:
|
||||
filename = sys.argv[1]
|
||||
if len(argv) >= 2 and sys.argv[2] == "G":
|
||||
img_codec = cv2.IMREAD_GRAYSCALE
|
||||
img_codec = cv.IMREAD_GRAYSCALE
|
||||
|
||||
src = cv2.imread(filename, img_codec)
|
||||
src = cv.imread(filename, img_codec)
|
||||
|
||||
if src is None:
|
||||
print("Can't open image [" + filename + "]")
|
||||
@@ -61,10 +61,10 @@ def main(argv):
|
||||
print("mat_mask_operations.py [image_path -- default ../../../../data/lena.jpg] [G -- grayscale]")
|
||||
return -1
|
||||
|
||||
cv2.namedWindow("Input", cv2.WINDOW_AUTOSIZE)
|
||||
cv2.namedWindow("Output", cv2.WINDOW_AUTOSIZE)
|
||||
cv.namedWindow("Input", cv.WINDOW_AUTOSIZE)
|
||||
cv.namedWindow("Output", cv.WINDOW_AUTOSIZE)
|
||||
|
||||
cv2.imshow("Input", src)
|
||||
cv.imshow("Input", src)
|
||||
t = round(time.time())
|
||||
|
||||
dst0 = sharpen(src)
|
||||
@@ -72,8 +72,8 @@ def main(argv):
|
||||
t = (time.time() - t) / 1000
|
||||
print("Hand written function time passed in seconds: %s" % t)
|
||||
|
||||
cv2.imshow("Output", dst0)
|
||||
cv2.waitKey()
|
||||
cv.imshow("Output", dst0)
|
||||
cv.waitKey()
|
||||
|
||||
t = time.time()
|
||||
## [kern]
|
||||
@@ -82,17 +82,17 @@ def main(argv):
|
||||
[0, -1, 0]], np.float32) # kernel should be floating point type
|
||||
## [kern]
|
||||
## [filter2D]
|
||||
dst1 = cv2.filter2D(src, -1, kernel)
|
||||
dst1 = cv.filter2D(src, -1, kernel)
|
||||
# ddepth = -1, means destination image has depth same as input image
|
||||
## [filter2D]
|
||||
|
||||
t = (time.time() - t) / 1000
|
||||
print("Built-in filter2D time passed in seconds: %s" % t)
|
||||
|
||||
cv2.imshow("Output", dst1)
|
||||
cv.imshow("Output", dst1)
|
||||
|
||||
cv2.waitKey(0)
|
||||
cv2.destroyAllWindows()
|
||||
cv.waitKey(0)
|
||||
cv.destroyAllWindows()
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import cv2
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
|
||||
input_image = np.array((
|
||||
@@ -16,23 +16,23 @@ kernel = np.array((
|
||||
[1, -1, 1],
|
||||
[0, 1, 0]), dtype="int")
|
||||
|
||||
output_image = cv2.morphologyEx(input_image, cv2.MORPH_HITMISS, kernel)
|
||||
output_image = cv.morphologyEx(input_image, cv.MORPH_HITMISS, kernel)
|
||||
|
||||
rate = 50
|
||||
kernel = (kernel + 1) * 127
|
||||
kernel = np.uint8(kernel)
|
||||
|
||||
kernel = cv2.resize(kernel, None, fx = rate, fy = rate, interpolation = cv2.INTER_NEAREST)
|
||||
cv2.imshow("kernel", kernel)
|
||||
cv2.moveWindow("kernel", 0, 0)
|
||||
kernel = cv.resize(kernel, None, fx = rate, fy = rate, interpolation = cv.INTER_NEAREST)
|
||||
cv.imshow("kernel", kernel)
|
||||
cv.moveWindow("kernel", 0, 0)
|
||||
|
||||
input_image = cv2.resize(input_image, None, fx = rate, fy = rate, interpolation = cv2.INTER_NEAREST)
|
||||
cv2.imshow("Original", input_image)
|
||||
cv2.moveWindow("Original", 0, 200)
|
||||
input_image = cv.resize(input_image, None, fx = rate, fy = rate, interpolation = cv.INTER_NEAREST)
|
||||
cv.imshow("Original", input_image)
|
||||
cv.moveWindow("Original", 0, 200)
|
||||
|
||||
output_image = cv2.resize(output_image, None , fx = rate, fy = rate, interpolation = cv2.INTER_NEAREST)
|
||||
cv2.imshow("Hit or Miss", output_image)
|
||||
cv2.moveWindow("Hit or Miss", 500, 200)
|
||||
output_image = cv.resize(output_image, None , fx = rate, fy = rate, interpolation = cv.INTER_NEAREST)
|
||||
cv.imshow("Hit or Miss", output_image)
|
||||
cv.moveWindow("Hit or Miss", 500, 200)
|
||||
|
||||
cv2.waitKey(0)
|
||||
cv2.destroyAllWindows()
|
||||
cv.waitKey(0)
|
||||
cv.destroyAllWindows()
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import sys
|
||||
import cv2
|
||||
import cv2 as cv
|
||||
|
||||
|
||||
def main(argv):
|
||||
@@ -14,7 +14,7 @@ def main(argv):
|
||||
filename = argv[0] if len(argv) > 0 else "../data/chicky_512.png"
|
||||
|
||||
# Load the image
|
||||
src = cv2.imread(filename)
|
||||
src = cv.imread(filename)
|
||||
|
||||
# Check if image is loaded fine
|
||||
if src is None:
|
||||
@@ -26,25 +26,25 @@ def main(argv):
|
||||
while 1:
|
||||
rows, cols, _channels = map(int, src.shape)
|
||||
## [show_image]
|
||||
cv2.imshow('Pyramids Demo', src)
|
||||
cv.imshow('Pyramids Demo', src)
|
||||
## [show_image]
|
||||
k = cv2.waitKey(0)
|
||||
k = cv.waitKey(0)
|
||||
|
||||
if k == 27:
|
||||
break
|
||||
## [pyrup]
|
||||
elif chr(k) == 'i':
|
||||
src = cv2.pyrUp(src, dstsize=(2 * cols, 2 * rows))
|
||||
src = cv.pyrUp(src, dstsize=(2 * cols, 2 * rows))
|
||||
print ('** Zoom In: Image x 2')
|
||||
## [pyrup]
|
||||
## [pyrdown]
|
||||
elif chr(k) == 'o':
|
||||
src = cv2.pyrDown(src, dstsize=(cols // 2, rows // 2))
|
||||
src = cv.pyrDown(src, dstsize=(cols // 2, rows // 2))
|
||||
print ('** Zoom Out: Image / 2')
|
||||
## [pyrdown]
|
||||
## [loop]
|
||||
|
||||
cv2.destroyAllWindows()
|
||||
cv.destroyAllWindows()
|
||||
return 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import sys
|
||||
import cv2
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
|
||||
# Global Variables
|
||||
@@ -14,13 +14,13 @@ window_name = 'Smoothing Demo'
|
||||
|
||||
|
||||
def main(argv):
|
||||
cv2.namedWindow(window_name, cv2.WINDOW_AUTOSIZE)
|
||||
cv.namedWindow(window_name, cv.WINDOW_AUTOSIZE)
|
||||
|
||||
# Load the source image
|
||||
imageName = argv[0] if len(argv) > 0 else "../data/lena.jpg"
|
||||
|
||||
global src
|
||||
src = cv2.imread(imageName, 1)
|
||||
src = cv.imread(imageName, 1)
|
||||
if src is None:
|
||||
print ('Error opening image')
|
||||
print ('Usage: smoothing.py [image_name -- default ../data/lena.jpg] \n')
|
||||
@@ -40,7 +40,7 @@ def main(argv):
|
||||
|
||||
## [blur]
|
||||
for i in range(1, MAX_KERNEL_LENGTH, 2):
|
||||
dst = cv2.blur(src, (i, i))
|
||||
dst = cv.blur(src, (i, i))
|
||||
if display_dst(DELAY_BLUR) != 0:
|
||||
return 0
|
||||
## [blur]
|
||||
@@ -51,7 +51,7 @@ def main(argv):
|
||||
|
||||
## [gaussianblur]
|
||||
for i in range(1, MAX_KERNEL_LENGTH, 2):
|
||||
dst = cv2.GaussianBlur(src, (i, i), 0)
|
||||
dst = cv.GaussianBlur(src, (i, i), 0)
|
||||
if display_dst(DELAY_BLUR) != 0:
|
||||
return 0
|
||||
## [gaussianblur]
|
||||
@@ -62,7 +62,7 @@ def main(argv):
|
||||
|
||||
## [medianblur]
|
||||
for i in range(1, MAX_KERNEL_LENGTH, 2):
|
||||
dst = cv2.medianBlur(src, i)
|
||||
dst = cv.medianBlur(src, i)
|
||||
if display_dst(DELAY_BLUR) != 0:
|
||||
return 0
|
||||
## [medianblur]
|
||||
@@ -74,7 +74,7 @@ def main(argv):
|
||||
## [bilateralfilter]
|
||||
# Remember, bilateral is a bit slow, so as value go higher, it takes long time
|
||||
for i in range(1, MAX_KERNEL_LENGTH, 2):
|
||||
dst = cv2.bilateralFilter(src, i, i * 2, i / 2)
|
||||
dst = cv.bilateralFilter(src, i, i * 2, i / 2)
|
||||
if display_dst(DELAY_BLUR) != 0:
|
||||
return 0
|
||||
## [bilateralfilter]
|
||||
@@ -89,16 +89,16 @@ def display_caption(caption):
|
||||
global dst
|
||||
dst = np.zeros(src.shape, src.dtype)
|
||||
rows, cols, ch = src.shape
|
||||
cv2.putText(dst, caption,
|
||||
cv.putText(dst, caption,
|
||||
(int(cols / 4), int(rows / 2)),
|
||||
cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255))
|
||||
cv.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255))
|
||||
|
||||
return display_dst(DELAY_CAPTION)
|
||||
|
||||
|
||||
def display_dst(delay):
|
||||
cv2.imshow(window_name, dst)
|
||||
c = cv2.waitKey(delay)
|
||||
cv.imshow(window_name, dst)
|
||||
c = cv.waitKey(delay)
|
||||
if c >= 0 : return -1
|
||||
return 0
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import cv2
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
|
||||
img = cv2.imread('../data/sudoku.png')
|
||||
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
|
||||
edges = cv2.Canny(gray,50,150,apertureSize = 3)
|
||||
img = cv.imread('../data/sudoku.png')
|
||||
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
|
||||
edges = cv.Canny(gray,50,150,apertureSize = 3)
|
||||
|
||||
lines = cv2.HoughLines(edges,1,np.pi/180,200)
|
||||
lines = cv.HoughLines(edges,1,np.pi/180,200)
|
||||
for line in lines:
|
||||
rho,theta = line[0]
|
||||
a = np.cos(theta)
|
||||
@@ -17,6 +17,6 @@ for line in lines:
|
||||
x2 = int(x0 - 1000*(-b))
|
||||
y2 = int(y0 - 1000*(a))
|
||||
|
||||
cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2)
|
||||
cv.line(img,(x1,y1),(x2,y2),(0,0,255),2)
|
||||
|
||||
cv2.imwrite('houghlines3.jpg',img)
|
||||
cv.imwrite('houghlines3.jpg',img)
|
||||
|
||||
+7
-7
@@ -1,12 +1,12 @@
|
||||
import cv2
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
|
||||
img = cv2.imread('../data/sudoku.png')
|
||||
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
|
||||
edges = cv2.Canny(gray,50,150,apertureSize = 3)
|
||||
lines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength=100,maxLineGap=10)
|
||||
img = cv.imread('../data/sudoku.png')
|
||||
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
|
||||
edges = cv.Canny(gray,50,150,apertureSize = 3)
|
||||
lines = cv.HoughLinesP(edges,1,np.pi/180,100,minLineLength=100,maxLineGap=10)
|
||||
for line in lines:
|
||||
x1,y1,x2,y2 = line[0]
|
||||
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)
|
||||
cv.line(img,(x1,y1),(x2,y2),(0,255,0),2)
|
||||
|
||||
cv2.imwrite('houghlines5.jpg',img)
|
||||
cv.imwrite('houghlines5.jpg',img)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import sys
|
||||
import cv2
|
||||
import cv2 as cv
|
||||
|
||||
## [global_variables]
|
||||
use_mask = False
|
||||
@@ -23,14 +23,14 @@ def main(argv):
|
||||
## [load_image]
|
||||
global img
|
||||
global templ
|
||||
img = cv2.imread(sys.argv[1], cv2.IMREAD_COLOR)
|
||||
templ = cv2.imread(sys.argv[2], cv2.IMREAD_COLOR)
|
||||
img = cv.imread(sys.argv[1], cv.IMREAD_COLOR)
|
||||
templ = cv.imread(sys.argv[2], cv.IMREAD_COLOR)
|
||||
|
||||
if (len(sys.argv) > 3):
|
||||
global use_mask
|
||||
use_mask = True
|
||||
global mask
|
||||
mask = cv2.imread( sys.argv[3], cv2.IMREAD_COLOR )
|
||||
mask = cv.imread( sys.argv[3], cv.IMREAD_COLOR )
|
||||
|
||||
if ((img is None) or (templ is None) or (use_mask and (mask is None))):
|
||||
print 'Can\'t read one of the images'
|
||||
@@ -38,19 +38,19 @@ def main(argv):
|
||||
## [load_image]
|
||||
|
||||
## [create_windows]
|
||||
cv2.namedWindow( image_window, cv2.WINDOW_AUTOSIZE )
|
||||
cv2.namedWindow( result_window, cv2.WINDOW_AUTOSIZE )
|
||||
cv.namedWindow( image_window, cv.WINDOW_AUTOSIZE )
|
||||
cv.namedWindow( result_window, cv.WINDOW_AUTOSIZE )
|
||||
## [create_windows]
|
||||
|
||||
## [create_trackbar]
|
||||
trackbar_label = 'Method: \n 0: SQDIFF \n 1: SQDIFF NORMED \n 2: TM CCORR \n 3: TM CCORR NORMED \n 4: TM COEFF \n 5: TM COEFF NORMED'
|
||||
cv2.createTrackbar( trackbar_label, image_window, match_method, max_Trackbar, MatchingMethod )
|
||||
cv.createTrackbar( trackbar_label, image_window, match_method, max_Trackbar, MatchingMethod )
|
||||
## [create_trackbar]
|
||||
|
||||
MatchingMethod(match_method)
|
||||
|
||||
## [wait_key]
|
||||
cv2.waitKey(0)
|
||||
cv.waitKey(0)
|
||||
return 0
|
||||
## [wait_key]
|
||||
|
||||
@@ -63,32 +63,32 @@ def MatchingMethod(param):
|
||||
img_display = img.copy()
|
||||
## [copy_source]
|
||||
## [match_template]
|
||||
method_accepts_mask = (cv2.TM_SQDIFF == match_method or match_method == cv2.TM_CCORR_NORMED)
|
||||
method_accepts_mask = (cv.TM_SQDIFF == match_method or match_method == cv.TM_CCORR_NORMED)
|
||||
if (use_mask and method_accepts_mask):
|
||||
result = cv2.matchTemplate(img, templ, match_method, None, mask)
|
||||
result = cv.matchTemplate(img, templ, match_method, None, mask)
|
||||
else:
|
||||
result = cv2.matchTemplate(img, templ, match_method)
|
||||
result = cv.matchTemplate(img, templ, match_method)
|
||||
## [match_template]
|
||||
|
||||
## [normalize]
|
||||
cv2.normalize( result, result, 0, 1, cv2.NORM_MINMAX, -1 )
|
||||
cv.normalize( result, result, 0, 1, cv.NORM_MINMAX, -1 )
|
||||
## [normalize]
|
||||
## [best_match]
|
||||
_minVal, _maxVal, minLoc, maxLoc = cv2.minMaxLoc(result, None)
|
||||
_minVal, _maxVal, minLoc, maxLoc = cv.minMaxLoc(result, None)
|
||||
## [best_match]
|
||||
|
||||
## [match_loc]
|
||||
if (match_method == cv2.TM_SQDIFF or match_method == cv2.TM_SQDIFF_NORMED):
|
||||
if (match_method == cv.TM_SQDIFF or match_method == cv.TM_SQDIFF_NORMED):
|
||||
matchLoc = minLoc
|
||||
else:
|
||||
matchLoc = maxLoc
|
||||
## [match_loc]
|
||||
|
||||
## [imshow]
|
||||
cv2.rectangle(img_display, matchLoc, (matchLoc[0] + templ.shape[0], matchLoc[1] + templ.shape[1]), (0,0,0), 2, 8, 0 )
|
||||
cv2.rectangle(result, matchLoc, (matchLoc[0] + templ.shape[0], matchLoc[1] + templ.shape[1]), (0,0,0), 2, 8, 0 )
|
||||
cv2.imshow(image_window, img_display)
|
||||
cv2.imshow(result_window, result)
|
||||
cv.rectangle(img_display, matchLoc, (matchLoc[0] + templ.shape[0], matchLoc[1] + templ.shape[1]), (0,0,0), 2, 8, 0 )
|
||||
cv.rectangle(result, matchLoc, (matchLoc[0] + templ.shape[0], matchLoc[1] + templ.shape[1]), (0,0,0), 2, 8, 0 )
|
||||
cv.imshow(image_window, img_display)
|
||||
cv.imshow(result_window, result)
|
||||
## [imshow]
|
||||
pass
|
||||
|
||||
|
||||
+22
-22
@@ -4,14 +4,14 @@
|
||||
"""
|
||||
import numpy as np
|
||||
import sys
|
||||
import cv2
|
||||
import cv2 as cv
|
||||
|
||||
|
||||
def show_wait_destroy(winname, img):
|
||||
cv2.imshow(winname, img)
|
||||
cv2.moveWindow(winname, 500, 0)
|
||||
cv2.waitKey(0)
|
||||
cv2.destroyWindow(winname)
|
||||
cv.imshow(winname, img)
|
||||
cv.moveWindow(winname, 500, 0)
|
||||
cv.waitKey(0)
|
||||
cv.destroyWindow(winname)
|
||||
|
||||
|
||||
def main(argv):
|
||||
@@ -23,7 +23,7 @@ def main(argv):
|
||||
return -1
|
||||
|
||||
# Load the image
|
||||
src = cv2.imread(argv[0], cv2.IMREAD_COLOR)
|
||||
src = cv.imread(argv[0], cv.IMREAD_COLOR)
|
||||
|
||||
# Check if image is loaded fine
|
||||
if src is None:
|
||||
@@ -31,13 +31,13 @@ def main(argv):
|
||||
return -1
|
||||
|
||||
# Show source image
|
||||
cv2.imshow("src", src)
|
||||
cv.imshow("src", src)
|
||||
# [load_image]
|
||||
|
||||
# [gray]
|
||||
# Transform source image to gray if it is not already
|
||||
if len(src.shape) != 2:
|
||||
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
|
||||
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
|
||||
else:
|
||||
gray = src
|
||||
|
||||
@@ -47,9 +47,9 @@ def main(argv):
|
||||
|
||||
# [bin]
|
||||
# Apply adaptiveThreshold at the bitwise_not of gray, notice the ~ symbol
|
||||
gray = cv2.bitwise_not(gray)
|
||||
bw = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, \
|
||||
cv2.THRESH_BINARY, 15, -2)
|
||||
gray = cv.bitwise_not(gray)
|
||||
bw = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_MEAN_C, \
|
||||
cv.THRESH_BINARY, 15, -2)
|
||||
# Show binary image
|
||||
show_wait_destroy("binary", bw)
|
||||
# [bin]
|
||||
@@ -66,11 +66,11 @@ def main(argv):
|
||||
horizontal_size = cols / 30
|
||||
|
||||
# Create structure element for extracting horizontal lines through morphology operations
|
||||
horizontalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (horizontal_size, 1))
|
||||
horizontalStructure = cv.getStructuringElement(cv.MORPH_RECT, (horizontal_size, 1))
|
||||
|
||||
# Apply morphology operations
|
||||
horizontal = cv2.erode(horizontal, horizontalStructure)
|
||||
horizontal = cv2.dilate(horizontal, horizontalStructure)
|
||||
horizontal = cv.erode(horizontal, horizontalStructure)
|
||||
horizontal = cv.dilate(horizontal, horizontalStructure)
|
||||
|
||||
# Show extracted horizontal lines
|
||||
show_wait_destroy("horizontal", horizontal)
|
||||
@@ -82,11 +82,11 @@ def main(argv):
|
||||
verticalsize = rows / 30
|
||||
|
||||
# Create structure element for extracting vertical lines through morphology operations
|
||||
verticalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (1, verticalsize))
|
||||
verticalStructure = cv.getStructuringElement(cv.MORPH_RECT, (1, verticalsize))
|
||||
|
||||
# Apply morphology operations
|
||||
vertical = cv2.erode(vertical, verticalStructure)
|
||||
vertical = cv2.dilate(vertical, verticalStructure)
|
||||
vertical = cv.erode(vertical, verticalStructure)
|
||||
vertical = cv.dilate(vertical, verticalStructure)
|
||||
|
||||
# Show extracted vertical lines
|
||||
show_wait_destroy("vertical", vertical)
|
||||
@@ -94,7 +94,7 @@ def main(argv):
|
||||
|
||||
# [smooth]
|
||||
# Inverse vertical image
|
||||
vertical = cv2.bitwise_not(vertical)
|
||||
vertical = cv.bitwise_not(vertical)
|
||||
show_wait_destroy("vertical_bit", vertical)
|
||||
|
||||
'''
|
||||
@@ -107,20 +107,20 @@ def main(argv):
|
||||
'''
|
||||
|
||||
# Step 1
|
||||
edges = cv2.adaptiveThreshold(vertical, 255, cv2.ADAPTIVE_THRESH_MEAN_C, \
|
||||
cv2.THRESH_BINARY, 3, -2)
|
||||
edges = cv.adaptiveThreshold(vertical, 255, cv.ADAPTIVE_THRESH_MEAN_C, \
|
||||
cv.THRESH_BINARY, 3, -2)
|
||||
show_wait_destroy("edges", edges)
|
||||
|
||||
# Step 2
|
||||
kernel = np.ones((2, 2), np.uint8)
|
||||
edges = cv2.dilate(edges, kernel)
|
||||
edges = cv.dilate(edges, kernel)
|
||||
show_wait_destroy("dilate", edges)
|
||||
|
||||
# Step 3
|
||||
smooth = np.copy(vertical)
|
||||
|
||||
# Step 4
|
||||
smooth = cv2.blur(smooth, (2, 2))
|
||||
smooth = cv.blur(smooth, (2, 2))
|
||||
|
||||
# Step 5
|
||||
(rows, cols) = np.where(edges != 0)
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
import cv2
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
|
||||
SZ=20
|
||||
bin_n = 16 # Number of bins
|
||||
|
||||
|
||||
affine_flags = cv2.WARP_INVERSE_MAP|cv2.INTER_LINEAR
|
||||
affine_flags = cv.WARP_INVERSE_MAP|cv.INTER_LINEAR
|
||||
|
||||
## [deskew]
|
||||
def deskew(img):
|
||||
m = cv2.moments(img)
|
||||
m = cv.moments(img)
|
||||
if abs(m['mu02']) < 1e-2:
|
||||
return img.copy()
|
||||
skew = m['mu11']/m['mu02']
|
||||
M = np.float32([[1, skew, -0.5*SZ*skew], [0, 1, 0]])
|
||||
img = cv2.warpAffine(img,M,(SZ, SZ),flags=affine_flags)
|
||||
img = cv.warpAffine(img,M,(SZ, SZ),flags=affine_flags)
|
||||
return img
|
||||
## [deskew]
|
||||
|
||||
## [hog]
|
||||
def hog(img):
|
||||
gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
|
||||
gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
|
||||
mag, ang = cv2.cartToPolar(gx, gy)
|
||||
gx = cv.Sobel(img, cv.CV_32F, 1, 0)
|
||||
gy = cv.Sobel(img, cv.CV_32F, 0, 1)
|
||||
mag, ang = cv.cartToPolar(gx, gy)
|
||||
bins = np.int32(bin_n*ang/(2*np.pi)) # quantizing binvalues in (0...16)
|
||||
bin_cells = bins[:10,:10], bins[10:,:10], bins[:10,10:], bins[10:,10:]
|
||||
mag_cells = mag[:10,:10], mag[10:,:10], mag[:10,10:], mag[10:,10:]
|
||||
@@ -31,7 +31,7 @@ def hog(img):
|
||||
return hist
|
||||
## [hog]
|
||||
|
||||
img = cv2.imread('digits.png',0)
|
||||
img = cv.imread('digits.png',0)
|
||||
if img is None:
|
||||
raise Exception("we need the digits.png image from samples/data here !")
|
||||
|
||||
@@ -49,13 +49,13 @@ hogdata = [map(hog,row) for row in deskewed]
|
||||
trainData = np.float32(hogdata).reshape(-1,64)
|
||||
responses = np.repeat(np.arange(10),250)[:,np.newaxis]
|
||||
|
||||
svm = cv2.ml.SVM_create()
|
||||
svm.setKernel(cv2.ml.SVM_LINEAR)
|
||||
svm.setType(cv2.ml.SVM_C_SVC)
|
||||
svm = cv.ml.SVM_create()
|
||||
svm.setKernel(cv.ml.SVM_LINEAR)
|
||||
svm.setType(cv.ml.SVM_C_SVC)
|
||||
svm.setC(2.67)
|
||||
svm.setGamma(5.383)
|
||||
|
||||
svm.train(trainData, cv2.ml.ROW_SAMPLE, responses)
|
||||
svm.train(trainData, cv.ml.ROW_SAMPLE, responses)
|
||||
svm.save('svm_data.dat')
|
||||
|
||||
###### Now testing ########################
|
||||
|
||||
Reference in New Issue
Block a user