feat: 切换后端至PaddleOCR-NCNN,切换工程为CMake
1.项目后端整体迁移至PaddleOCR-NCNN算法,已通过基本的兼容性测试 2.工程改为使用CMake组织,后续为了更好地兼容第三方库,不再提供QMake工程 3.重整权利声明文件,重整代码工程,确保最小化侵权风险 Log: 切换后端至PaddleOCR-NCNN,切换工程为CMake Change-Id: I4d5d2c5d37505a4a24b389b1a4c5d12f17bfa38c
This commit is contained in:
54
3rdparty/opencv-4.5.4/samples/python/tutorial_code/ImgTrans/Filter2D/filter2D.py
vendored
Normal file
54
3rdparty/opencv-4.5.4/samples/python/tutorial_code/ImgTrans/Filter2D/filter2D.py
vendored
Normal file
@ -0,0 +1,54 @@
|
||||
"""
|
||||
@file filter2D.py
|
||||
@brief Sample code that shows how to implement your own linear filters by using filter2D function
|
||||
"""
|
||||
import sys
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
|
||||
|
||||
def main(argv):
|
||||
window_name = 'filter2D Demo'
|
||||
|
||||
## [load]
|
||||
imageName = argv[0] if len(argv) > 0 else 'lena.jpg'
|
||||
|
||||
# Loads an image
|
||||
src = cv.imread(cv.samples.findFile(imageName), cv.IMREAD_COLOR)
|
||||
|
||||
# Check if image is loaded fine
|
||||
if src is None:
|
||||
print ('Error opening image!')
|
||||
print ('Usage: filter2D.py [image_name -- default lena.jpg] \n')
|
||||
return -1
|
||||
## [load]
|
||||
## [init_arguments]
|
||||
# Initialize ddepth argument for the filter
|
||||
ddepth = -1
|
||||
## [init_arguments]
|
||||
# Loop - Will filter the image with different kernel sizes each 0.5 seconds
|
||||
ind = 0
|
||||
while True:
|
||||
## [update_kernel]
|
||||
# Update kernel size for a normalized box filter
|
||||
kernel_size = 3 + 2 * (ind % 5)
|
||||
kernel = np.ones((kernel_size, kernel_size), dtype=np.float32)
|
||||
kernel /= (kernel_size * kernel_size)
|
||||
## [update_kernel]
|
||||
## [apply_filter]
|
||||
# Apply filter
|
||||
dst = cv.filter2D(src, ddepth, kernel)
|
||||
## [apply_filter]
|
||||
cv.imshow(window_name, dst)
|
||||
|
||||
c = cv.waitKey(500)
|
||||
if c == 27:
|
||||
break
|
||||
|
||||
ind += 1
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
59
3rdparty/opencv-4.5.4/samples/python/tutorial_code/ImgTrans/HoughCircle/hough_circle.py
vendored
Normal file
59
3rdparty/opencv-4.5.4/samples/python/tutorial_code/ImgTrans/HoughCircle/hough_circle.py
vendored
Normal file
@ -0,0 +1,59 @@
|
||||
import sys
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
|
||||
|
||||
def main(argv):
|
||||
## [load]
|
||||
default_file = 'smarties.png'
|
||||
filename = argv[0] if len(argv) > 0 else default_file
|
||||
|
||||
# Loads an image
|
||||
src = cv.imread(cv.samples.findFile(filename), cv.IMREAD_COLOR)
|
||||
|
||||
# Check if image is loaded fine
|
||||
if src is None:
|
||||
print ('Error opening image!')
|
||||
print ('Usage: hough_circle.py [image_name -- default ' + default_file + '] \n')
|
||||
return -1
|
||||
## [load]
|
||||
|
||||
## [convert_to_gray]
|
||||
# Convert it to gray
|
||||
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
|
||||
## [convert_to_gray]
|
||||
|
||||
## [reduce_noise]
|
||||
# Reduce the noise to avoid false circle detection
|
||||
gray = cv.medianBlur(gray, 5)
|
||||
## [reduce_noise]
|
||||
|
||||
## [houghcircles]
|
||||
rows = gray.shape[0]
|
||||
circles = cv.HoughCircles(gray, cv.HOUGH_GRADIENT, 1, rows / 8,
|
||||
param1=100, param2=30,
|
||||
minRadius=1, maxRadius=30)
|
||||
## [houghcircles]
|
||||
|
||||
## [draw]
|
||||
if circles is not None:
|
||||
circles = np.uint16(np.around(circles))
|
||||
for i in circles[0, :]:
|
||||
center = (i[0], i[1])
|
||||
# circle center
|
||||
cv.circle(src, center, 1, (0, 100, 100), 3)
|
||||
# circle outline
|
||||
radius = i[2]
|
||||
cv.circle(src, center, radius, (255, 0, 255), 3)
|
||||
## [draw]
|
||||
|
||||
## [display]
|
||||
cv.imshow("detected circles", src)
|
||||
cv.waitKey(0)
|
||||
## [display]
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
79
3rdparty/opencv-4.5.4/samples/python/tutorial_code/ImgTrans/HoughLine/hough_lines.py
vendored
Normal file
79
3rdparty/opencv-4.5.4/samples/python/tutorial_code/ImgTrans/HoughLine/hough_lines.py
vendored
Normal file
@ -0,0 +1,79 @@
|
||||
"""
|
||||
@file hough_lines.py
|
||||
@brief This program demonstrates line finding with the Hough transform
|
||||
"""
|
||||
import sys
|
||||
import math
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
|
||||
|
||||
def main(argv):
|
||||
## [load]
|
||||
default_file = 'sudoku.png'
|
||||
filename = argv[0] if len(argv) > 0 else default_file
|
||||
|
||||
# Loads an image
|
||||
src = cv.imread(cv.samples.findFile(filename), cv.IMREAD_GRAYSCALE)
|
||||
|
||||
# Check if image is loaded fine
|
||||
if src is None:
|
||||
print ('Error opening image!')
|
||||
print ('Usage: hough_lines.py [image_name -- default ' + default_file + '] \n')
|
||||
return -1
|
||||
## [load]
|
||||
|
||||
## [edge_detection]
|
||||
# Edge detection
|
||||
dst = cv.Canny(src, 50, 200, None, 3)
|
||||
## [edge_detection]
|
||||
|
||||
# Copy edges to the images that will display the results in BGR
|
||||
cdst = cv.cvtColor(dst, cv.COLOR_GRAY2BGR)
|
||||
cdstP = np.copy(cdst)
|
||||
|
||||
## [hough_lines]
|
||||
# Standard Hough Line Transform
|
||||
lines = cv.HoughLines(dst, 1, np.pi / 180, 150, None, 0, 0)
|
||||
## [hough_lines]
|
||||
## [draw_lines]
|
||||
# Draw the lines
|
||||
if lines is not None:
|
||||
for i in range(0, len(lines)):
|
||||
rho = lines[i][0][0]
|
||||
theta = lines[i][0][1]
|
||||
a = math.cos(theta)
|
||||
b = math.sin(theta)
|
||||
x0 = a * rho
|
||||
y0 = b * rho
|
||||
pt1 = (int(x0 + 1000*(-b)), int(y0 + 1000*(a)))
|
||||
pt2 = (int(x0 - 1000*(-b)), int(y0 - 1000*(a)))
|
||||
|
||||
cv.line(cdst, pt1, pt2, (0,0,255), 3, cv.LINE_AA)
|
||||
## [draw_lines]
|
||||
|
||||
## [hough_lines_p]
|
||||
# Probabilistic Line Transform
|
||||
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]
|
||||
cv.line(cdstP, (l[0], l[1]), (l[2], l[3]), (0,0,255), 3, cv.LINE_AA)
|
||||
## [draw_lines_p]
|
||||
## [imshow]
|
||||
# Show results
|
||||
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
|
||||
cv.waitKey()
|
||||
return 0
|
||||
## [exit]
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
59
3rdparty/opencv-4.5.4/samples/python/tutorial_code/ImgTrans/LaPlace/laplace_demo.py
vendored
Normal file
59
3rdparty/opencv-4.5.4/samples/python/tutorial_code/ImgTrans/LaPlace/laplace_demo.py
vendored
Normal file
@ -0,0 +1,59 @@
|
||||
"""
|
||||
@file laplace_demo.py
|
||||
@brief Sample code showing how to detect edges using the Laplace operator
|
||||
"""
|
||||
import sys
|
||||
import cv2 as cv
|
||||
|
||||
def main(argv):
|
||||
# [variables]
|
||||
# Declare the variables we are going to use
|
||||
ddepth = cv.CV_16S
|
||||
kernel_size = 3
|
||||
window_name = "Laplace Demo"
|
||||
# [variables]
|
||||
|
||||
# [load]
|
||||
imageName = argv[0] if len(argv) > 0 else 'lena.jpg'
|
||||
|
||||
src = cv.imread(cv.samples.findFile(imageName), cv.IMREAD_COLOR) # Load an image
|
||||
|
||||
# Check if image is loaded fine
|
||||
if src is None:
|
||||
print ('Error opening image')
|
||||
print ('Program Arguments: [image_name -- default lena.jpg]')
|
||||
return -1
|
||||
# [load]
|
||||
|
||||
# [reduce_noise]
|
||||
# Remove noise by blurring with a Gaussian filter
|
||||
src = cv.GaussianBlur(src, (3, 3), 0)
|
||||
# [reduce_noise]
|
||||
|
||||
# [convert_to_gray]
|
||||
# Convert the image to grayscale
|
||||
src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
|
||||
# [convert_to_gray]
|
||||
|
||||
# Create Window
|
||||
cv.namedWindow(window_name, cv.WINDOW_AUTOSIZE)
|
||||
|
||||
# [laplacian]
|
||||
# Apply Laplace function
|
||||
dst = cv.Laplacian(src_gray, ddepth, ksize=kernel_size)
|
||||
# [laplacian]
|
||||
|
||||
# [convert]
|
||||
# converting back to uint8
|
||||
abs_dst = cv.convertScaleAbs(dst)
|
||||
# [convert]
|
||||
|
||||
# [display]
|
||||
cv.imshow(window_name, abs_dst)
|
||||
cv.waitKey(0)
|
||||
# [display]
|
||||
|
||||
return 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
69
3rdparty/opencv-4.5.4/samples/python/tutorial_code/ImgTrans/MakeBorder/copy_make_border.py
vendored
Normal file
69
3rdparty/opencv-4.5.4/samples/python/tutorial_code/ImgTrans/MakeBorder/copy_make_border.py
vendored
Normal file
@ -0,0 +1,69 @@
|
||||
"""
|
||||
@file copy_make_border.py
|
||||
@brief Sample code that shows the functionality of copyMakeBorder
|
||||
"""
|
||||
import sys
|
||||
from random import randint
|
||||
import cv2 as cv
|
||||
|
||||
|
||||
def main(argv):
|
||||
## [variables]
|
||||
# First we declare the variables we are going to use
|
||||
borderType = cv.BORDER_CONSTANT
|
||||
window_name = "copyMakeBorder Demo"
|
||||
## [variables]
|
||||
## [load]
|
||||
imageName = argv[0] if len(argv) > 0 else 'lena.jpg'
|
||||
|
||||
# Loads an image
|
||||
src = cv.imread(cv.samples.findFile(imageName), cv.IMREAD_COLOR)
|
||||
|
||||
# Check if image is loaded fine
|
||||
if src is None:
|
||||
print ('Error opening image!')
|
||||
print ('Usage: copy_make_border.py [image_name -- default lena.jpg] \n')
|
||||
return -1
|
||||
## [load]
|
||||
# Brief how-to for this program
|
||||
print ('\n'
|
||||
'\t copyMakeBorder Demo: \n'
|
||||
' -------------------- \n'
|
||||
' ** Press \'c\' to set the border to a random constant value \n'
|
||||
' ** Press \'r\' to set the border to be replicated \n'
|
||||
' ** Press \'ESC\' to exit the program ')
|
||||
## [create_window]
|
||||
cv.namedWindow(window_name, cv.WINDOW_AUTOSIZE)
|
||||
## [create_window]
|
||||
## [init_arguments]
|
||||
# Initialize arguments for the filter
|
||||
top = int(0.05 * src.shape[0]) # shape[0] = rows
|
||||
bottom = top
|
||||
left = int(0.05 * src.shape[1]) # shape[1] = cols
|
||||
right = left
|
||||
## [init_arguments]
|
||||
while 1:
|
||||
## [update_value]
|
||||
value = [randint(0, 255), randint(0, 255), randint(0, 255)]
|
||||
## [update_value]
|
||||
## [copymakeborder]
|
||||
dst = cv.copyMakeBorder(src, top, bottom, left, right, borderType, None, value)
|
||||
## [copymakeborder]
|
||||
## [display]
|
||||
cv.imshow(window_name, dst)
|
||||
## [display]
|
||||
## [check_keypress]
|
||||
c = cv.waitKey(500)
|
||||
|
||||
if c == 27:
|
||||
break
|
||||
elif c == 99: # 99 = ord('c')
|
||||
borderType = cv.BORDER_CONSTANT
|
||||
elif c == 114: # 114 = ord('r')
|
||||
borderType = cv.BORDER_REPLICATE
|
||||
## [check_keypress]
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
74
3rdparty/opencv-4.5.4/samples/python/tutorial_code/ImgTrans/SobelDemo/sobel_demo.py
vendored
Normal file
74
3rdparty/opencv-4.5.4/samples/python/tutorial_code/ImgTrans/SobelDemo/sobel_demo.py
vendored
Normal file
@ -0,0 +1,74 @@
|
||||
"""
|
||||
@file sobel_demo.py
|
||||
@brief Sample code using Sobel and/or Scharr OpenCV functions to make a simple Edge Detector
|
||||
"""
|
||||
import sys
|
||||
import cv2 as cv
|
||||
|
||||
|
||||
def main(argv):
|
||||
## [variables]
|
||||
# First we declare the variables we are going to use
|
||||
window_name = ('Sobel Demo - Simple Edge Detector')
|
||||
scale = 1
|
||||
delta = 0
|
||||
ddepth = cv.CV_16S
|
||||
## [variables]
|
||||
|
||||
## [load]
|
||||
# As usual we load our source image (src)
|
||||
# Check number of arguments
|
||||
if len(argv) < 1:
|
||||
print ('Not enough parameters')
|
||||
print ('Usage:\nmorph_lines_detection.py < path_to_image >')
|
||||
return -1
|
||||
|
||||
# Load the image
|
||||
src = cv.imread(argv[0], cv.IMREAD_COLOR)
|
||||
|
||||
# Check if image is loaded fine
|
||||
if src is None:
|
||||
print ('Error opening image: ' + argv[0])
|
||||
return -1
|
||||
## [load]
|
||||
|
||||
## [reduce_noise]
|
||||
# Remove noise by blurring with a Gaussian filter ( kernel size = 3 )
|
||||
src = cv.GaussianBlur(src, (3, 3), 0)
|
||||
## [reduce_noise]
|
||||
|
||||
## [convert_to_gray]
|
||||
# Convert the image to grayscale
|
||||
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
|
||||
## [convert_to_gray]
|
||||
|
||||
## [sobel]
|
||||
# Gradient-X
|
||||
# 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 = 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 = cv.convertScaleAbs(grad_x)
|
||||
abs_grad_y = cv.convertScaleAbs(grad_y)
|
||||
## [convert]
|
||||
|
||||
## [blend]
|
||||
## Total Gradient (approximate)
|
||||
grad = cv.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)
|
||||
## [blend]
|
||||
|
||||
## [display]
|
||||
cv.imshow(window_name, grad)
|
||||
cv.waitKey(0)
|
||||
## [display]
|
||||
|
||||
return 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
34
3rdparty/opencv-4.5.4/samples/python/tutorial_code/ImgTrans/canny_detector/CannyDetector_Demo.py
vendored
Normal file
34
3rdparty/opencv-4.5.4/samples/python/tutorial_code/ImgTrans/canny_detector/CannyDetector_Demo.py
vendored
Normal file
@ -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='fruits.jpg')
|
||||
args = parser.parse_args()
|
||||
|
||||
src = cv.imread(cv.samples.findFile(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,139 @@
|
||||
from __future__ import print_function
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
import argparse
|
||||
import random as rng
|
||||
|
||||
rng.seed(12345)
|
||||
|
||||
## [load_image]
|
||||
# Load the image
|
||||
parser = argparse.ArgumentParser(description='Code for Image Segmentation with Distance Transform and Watershed Algorithm.\
|
||||
Sample code showing how to segment overlapping objects using Laplacian filtering, \
|
||||
in addition to Watershed and Distance Transformation')
|
||||
parser.add_argument('--input', help='Path to input image.', default='cards.png')
|
||||
args = parser.parse_args()
|
||||
|
||||
src = cv.imread(cv.samples.findFile(args.input))
|
||||
if src is None:
|
||||
print('Could not open or find the image:', args.input)
|
||||
exit(0)
|
||||
|
||||
# Show source image
|
||||
cv.imshow('Source Image', src)
|
||||
## [load_image]
|
||||
|
||||
## [black_bg]
|
||||
# Change the background from white to black, since that will help later to extract
|
||||
# better results during the use of Distance Transform
|
||||
src[np.all(src == 255, axis=2)] = 0
|
||||
|
||||
# Show output image
|
||||
cv.imshow('Black Background Image', src)
|
||||
## [black_bg]
|
||||
|
||||
## [sharp]
|
||||
# Create a kernel that we will use to sharpen our image
|
||||
# an approximation of second derivative, a quite strong kernel
|
||||
kernel = np.array([[1, 1, 1], [1, -8, 1], [1, 1, 1]], dtype=np.float32)
|
||||
|
||||
# do the laplacian filtering as it is
|
||||
# well, we need to convert everything in something more deeper then CV_8U
|
||||
# because the kernel has some negative values,
|
||||
# and we can expect in general to have a Laplacian image with negative values
|
||||
# BUT a 8bits unsigned int (the one we are working with) can contain values from 0 to 255
|
||||
# so the possible negative number will be truncated
|
||||
imgLaplacian = cv.filter2D(src, cv.CV_32F, kernel)
|
||||
sharp = np.float32(src)
|
||||
imgResult = sharp - imgLaplacian
|
||||
|
||||
# convert back to 8bits gray scale
|
||||
imgResult = np.clip(imgResult, 0, 255)
|
||||
imgResult = imgResult.astype('uint8')
|
||||
imgLaplacian = np.clip(imgLaplacian, 0, 255)
|
||||
imgLaplacian = np.uint8(imgLaplacian)
|
||||
|
||||
#cv.imshow('Laplace Filtered Image', imgLaplacian)
|
||||
cv.imshow('New Sharped Image', imgResult)
|
||||
## [sharp]
|
||||
|
||||
## [bin]
|
||||
# Create binary image from source image
|
||||
bw = cv.cvtColor(imgResult, cv.COLOR_BGR2GRAY)
|
||||
_, bw = cv.threshold(bw, 40, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
|
||||
cv.imshow('Binary Image', bw)
|
||||
## [bin]
|
||||
|
||||
## [dist]
|
||||
# Perform the distance transform algorithm
|
||||
dist = cv.distanceTransform(bw, cv.DIST_L2, 3)
|
||||
|
||||
# Normalize the distance image for range = {0.0, 1.0}
|
||||
# so we can visualize and threshold it
|
||||
cv.normalize(dist, dist, 0, 1.0, cv.NORM_MINMAX)
|
||||
cv.imshow('Distance Transform Image', dist)
|
||||
## [dist]
|
||||
|
||||
## [peaks]
|
||||
# Threshold to obtain the peaks
|
||||
# This will be the markers for the foreground objects
|
||||
_, dist = cv.threshold(dist, 0.4, 1.0, cv.THRESH_BINARY)
|
||||
|
||||
# Dilate a bit the dist image
|
||||
kernel1 = np.ones((3,3), dtype=np.uint8)
|
||||
dist = cv.dilate(dist, kernel1)
|
||||
cv.imshow('Peaks', dist)
|
||||
## [peaks]
|
||||
|
||||
## [seeds]
|
||||
# Create the CV_8U version of the distance image
|
||||
# It is needed for findContours()
|
||||
dist_8u = dist.astype('uint8')
|
||||
|
||||
# Find total markers
|
||||
contours, _ = cv.findContours(dist_8u, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
|
||||
|
||||
# Create the marker image for the watershed algorithm
|
||||
markers = np.zeros(dist.shape, dtype=np.int32)
|
||||
|
||||
# Draw the foreground markers
|
||||
for i in range(len(contours)):
|
||||
cv.drawContours(markers, contours, i, (i+1), -1)
|
||||
|
||||
# Draw the background marker
|
||||
cv.circle(markers, (5,5), 3, (255,255,255), -1)
|
||||
markers_8u = (markers * 10).astype('uint8')
|
||||
cv.imshow('Markers', markers_8u)
|
||||
## [seeds]
|
||||
|
||||
## [watershed]
|
||||
# Perform the watershed algorithm
|
||||
cv.watershed(imgResult, markers)
|
||||
|
||||
#mark = np.zeros(markers.shape, dtype=np.uint8)
|
||||
mark = markers.astype('uint8')
|
||||
mark = cv.bitwise_not(mark)
|
||||
# uncomment this if you want to see how the mark
|
||||
# image looks like at that point
|
||||
#cv.imshow('Markers_v2', mark)
|
||||
|
||||
# Generate random colors
|
||||
colors = []
|
||||
for contour in contours:
|
||||
colors.append((rng.randint(0,256), rng.randint(0,256), rng.randint(0,256)))
|
||||
|
||||
# Create the result image
|
||||
dst = np.zeros((markers.shape[0], markers.shape[1], 3), dtype=np.uint8)
|
||||
|
||||
# Fill labeled objects with random colors
|
||||
for i in range(markers.shape[0]):
|
||||
for j in range(markers.shape[1]):
|
||||
index = markers[i,j]
|
||||
if index > 0 and index <= len(contours):
|
||||
dst[i,j,:] = colors[index-1]
|
||||
|
||||
# Visualize the final image
|
||||
cv.imshow('Final Result', dst)
|
||||
## [watershed]
|
||||
|
||||
cv.waitKey()
|
65
3rdparty/opencv-4.5.4/samples/python/tutorial_code/ImgTrans/remap/Remap_Demo.py
vendored
Normal file
65
3rdparty/opencv-4.5.4/samples/python/tutorial_code/ImgTrans/remap/Remap_Demo.py
vendored
Normal file
@ -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='chicky_512.png')
|
||||
args = parser.parse_args()
|
||||
|
||||
## [Load]
|
||||
src = cv.imread(cv.samples.findFile(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 numpy as np
|
||||
import argparse
|
||||
|
||||
## [Load the image]
|
||||
parser = argparse.ArgumentParser(description='Code for Affine Transformations tutorial.')
|
||||
parser.add_argument('--input', help='Path to input image.', default='lena.jpg')
|
||||
args = parser.parse_args()
|
||||
|
||||
src = cv.imread(cv.samples.findFile(args.input))
|
||||
if src is None:
|
||||
print('Could not open or find the image:', args.input)
|
||||
exit(0)
|
||||
## [Load the image]
|
||||
|
||||
## [Set your 3 points to calculate the Affine Transform]
|
||||
srcTri = np.array( [[0, 0], [src.shape[1] - 1, 0], [0, src.shape[0] - 1]] ).astype(np.float32)
|
||||
dstTri = np.array( [[0, src.shape[1]*0.33], [src.shape[1]*0.85, src.shape[0]*0.25], [src.shape[1]*0.15, src.shape[0]*0.7]] ).astype(np.float32)
|
||||
## [Set your 3 points to calculate the Affine Transform]
|
||||
|
||||
## [Get the Affine Transform]
|
||||
warp_mat = cv.getAffineTransform(srcTri, dstTri)
|
||||
## [Get the Affine Transform]
|
||||
|
||||
## [Apply the Affine Transform just found to the src image]
|
||||
warp_dst = cv.warpAffine(src, warp_mat, (src.shape[1], src.shape[0]))
|
||||
## [Apply the Affine Transform just found to the src image]
|
||||
|
||||
# Rotating the image after Warp
|
||||
|
||||
## [Compute a rotation matrix with respect to the center of the image]
|
||||
center = (warp_dst.shape[1]//2, warp_dst.shape[0]//2)
|
||||
angle = -50
|
||||
scale = 0.6
|
||||
## [Compute a rotation matrix with respect to the center of the image]
|
||||
|
||||
## [Get the rotation matrix with the specifications above]
|
||||
rot_mat = cv.getRotationMatrix2D( center, angle, scale )
|
||||
## [Get the rotation matrix with the specifications above]
|
||||
|
||||
## [Rotate the warped image]
|
||||
warp_rotate_dst = cv.warpAffine(warp_dst, rot_mat, (warp_dst.shape[1], warp_dst.shape[0]))
|
||||
## [Rotate the warped image]
|
||||
|
||||
## [Show what you got]
|
||||
cv.imshow('Source image', src)
|
||||
cv.imshow('Warp', warp_dst)
|
||||
cv.imshow('Warp + Rotate', warp_rotate_dst)
|
||||
## [Show what you got]
|
||||
|
||||
## [Wait until user exits the program]
|
||||
cv.waitKey()
|
||||
## [Wait until user exits the program]
|
Reference in New Issue
Block a user