mirror of
https://github.com/opencv/opencv.git
synced 2026-07-30 07:43:03 +04:00
Merge remote-tracking branch 'upstream/3.4' into merge-3.4
This commit is contained in:
@@ -25,6 +25,7 @@ int const max_kernel_size = 21;
|
||||
void Erosion( int, void* );
|
||||
void Dilation( int, void* );
|
||||
|
||||
//![main]
|
||||
/**
|
||||
* @function main
|
||||
*/
|
||||
@@ -70,6 +71,7 @@ int main( int argc, char** argv )
|
||||
waitKey(0);
|
||||
return 0;
|
||||
}
|
||||
//![main]
|
||||
|
||||
//![erosion]
|
||||
/**
|
||||
|
||||
@@ -34,6 +34,7 @@ public class MorphologyDemo1 {
|
||||
private JFrame frame;
|
||||
private JLabel imgLabel;
|
||||
|
||||
//! [constructor]
|
||||
public MorphologyDemo1(String[] args) {
|
||||
String imagePath = args.length > 0 ? args[0] : "../data/LinuxLogo.jpg";
|
||||
matImgSrc = Imgcodecs.imread(imagePath);
|
||||
@@ -54,7 +55,9 @@ public class MorphologyDemo1 {
|
||||
frame.pack();
|
||||
frame.setVisible(true);
|
||||
}
|
||||
//! [constructor]
|
||||
|
||||
//! [components]
|
||||
private void addComponentsToPane(Container pane, Image img) {
|
||||
if (!(pane.getLayout() instanceof BorderLayout)) {
|
||||
pane.add(new JLabel("Container doesn't use BorderLayout!"));
|
||||
@@ -114,21 +117,31 @@ public class MorphologyDemo1 {
|
||||
imgLabel = new JLabel(new ImageIcon(img));
|
||||
pane.add(imgLabel, BorderLayout.CENTER);
|
||||
}
|
||||
//! [components]
|
||||
|
||||
//! [update]
|
||||
private void update() {
|
||||
//! [kernel]
|
||||
Mat element = Imgproc.getStructuringElement(elementType, new Size(2 * kernelSize + 1, 2 * kernelSize + 1),
|
||||
new Point(kernelSize, kernelSize));
|
||||
//! [kernel]
|
||||
|
||||
if (doErosion) {
|
||||
//! [erosion]
|
||||
Imgproc.erode(matImgSrc, matImgDst, element);
|
||||
//! [erosion]
|
||||
} else {
|
||||
//! [dilation]
|
||||
Imgproc.dilate(matImgSrc, matImgDst, element);
|
||||
//! [dilation]
|
||||
}
|
||||
Image img = HighGui.toBufferedImage(matImgDst);
|
||||
imgLabel.setIcon(new ImageIcon(img));
|
||||
frame.repaint();
|
||||
}
|
||||
//! [update]
|
||||
|
||||
//! [main]
|
||||
public static void main(String[] args) {
|
||||
// Load the native OpenCV library
|
||||
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
||||
@@ -142,4 +155,5 @@ public class MorphologyDemo1 {
|
||||
}
|
||||
});
|
||||
}
|
||||
//! [main]
|
||||
}
|
||||
|
||||
@@ -3,61 +3,76 @@ import cv2 as cv
|
||||
import numpy as np
|
||||
import argparse
|
||||
|
||||
src = None
|
||||
erosion_size = 0
|
||||
max_elem = 2
|
||||
max_kernel_size = 21
|
||||
title_trackbar_element_type = 'Element:\n 0: Rect \n 1: Cross \n 2: Ellipse'
|
||||
title_trackbar_element_shape = 'Element:\n 0: Rect \n 1: Cross \n 2: Ellipse'
|
||||
title_trackbar_kernel_size = 'Kernel size:\n 2n +1'
|
||||
title_erosion_window = 'Erosion Demo'
|
||||
title_dilatation_window = 'Dilation Demo'
|
||||
title_dilation_window = 'Dilation Demo'
|
||||
|
||||
|
||||
## [main]
|
||||
def main(image):
|
||||
global src
|
||||
src = cv.imread(cv.samples.findFile(image))
|
||||
if src is None:
|
||||
print('Could not open or find the image: ', image)
|
||||
exit(0)
|
||||
|
||||
cv.namedWindow(title_erosion_window)
|
||||
cv.createTrackbar(title_trackbar_element_shape, title_erosion_window, 0, max_elem, erosion)
|
||||
cv.createTrackbar(title_trackbar_kernel_size, title_erosion_window, 0, max_kernel_size, erosion)
|
||||
|
||||
cv.namedWindow(title_dilation_window)
|
||||
cv.createTrackbar(title_trackbar_element_shape, title_dilation_window, 0, max_elem, dilatation)
|
||||
cv.createTrackbar(title_trackbar_kernel_size, title_dilation_window, 0, max_kernel_size, dilatation)
|
||||
|
||||
erosion(0)
|
||||
dilatation(0)
|
||||
cv.waitKey()
|
||||
## [main]
|
||||
|
||||
# optional mapping of values with morphological shapes
|
||||
def morph_shape(val):
|
||||
if val == 0:
|
||||
return cv.MORPH_RECT
|
||||
elif val == 1:
|
||||
return cv.MORPH_CROSS
|
||||
elif val == 2:
|
||||
return cv.MORPH_ELLIPSE
|
||||
|
||||
|
||||
## [erosion]
|
||||
def erosion(val):
|
||||
erosion_size = cv.getTrackbarPos(title_trackbar_kernel_size, title_erosion_window)
|
||||
erosion_type = 0
|
||||
val_type = cv.getTrackbarPos(title_trackbar_element_type, title_erosion_window)
|
||||
if val_type == 0:
|
||||
erosion_type = cv.MORPH_RECT
|
||||
elif val_type == 1:
|
||||
erosion_type = cv.MORPH_CROSS
|
||||
elif val_type == 2:
|
||||
erosion_type = cv.MORPH_ELLIPSE
|
||||
erosion_shape = morph_shape(cv.getTrackbarPos(title_trackbar_element_shape, title_erosion_window))
|
||||
|
||||
element = cv.getStructuringElement(erosion_type, (2*erosion_size + 1, 2*erosion_size+1), (erosion_size, erosion_size))
|
||||
## [kernel]
|
||||
element = cv.getStructuringElement(erosion_shape, (2 * erosion_size + 1, 2 * erosion_size + 1),
|
||||
(erosion_size, erosion_size))
|
||||
## [kernel]
|
||||
erosion_dst = cv.erode(src, element)
|
||||
cv.imshow(title_erosion_window, erosion_dst)
|
||||
## [erosion]
|
||||
|
||||
|
||||
## [dilation]
|
||||
def dilatation(val):
|
||||
dilatation_size = cv.getTrackbarPos(title_trackbar_kernel_size, title_dilatation_window)
|
||||
dilatation_type = 0
|
||||
val_type = cv.getTrackbarPos(title_trackbar_element_type, title_dilatation_window)
|
||||
if val_type == 0:
|
||||
dilatation_type = cv.MORPH_RECT
|
||||
elif val_type == 1:
|
||||
dilatation_type = cv.MORPH_CROSS
|
||||
elif val_type == 2:
|
||||
dilatation_type = cv.MORPH_ELLIPSE
|
||||
dilatation_size = cv.getTrackbarPos(title_trackbar_kernel_size, title_dilation_window)
|
||||
dilation_shape = morph_shape(cv.getTrackbarPos(title_trackbar_element_shape, title_dilation_window))
|
||||
|
||||
element = cv.getStructuringElement(dilatation_type, (2*dilatation_size + 1, 2*dilatation_size+1), (dilatation_size, dilatation_size))
|
||||
element = cv.getStructuringElement(dilation_shape, (2 * dilatation_size + 1, 2 * dilatation_size + 1),
|
||||
(dilatation_size, dilatation_size))
|
||||
dilatation_dst = cv.dilate(src, element)
|
||||
cv.imshow(title_dilatation_window, dilatation_dst)
|
||||
cv.imshow(title_dilation_window, dilatation_dst)
|
||||
## [dilation]
|
||||
|
||||
parser = argparse.ArgumentParser(description='Code for Eroding and Dilating tutorial.')
|
||||
parser.add_argument('--input', help='Path to input image.', default='LinuxLogo.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)
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description='Code for Eroding and Dilating tutorial.')
|
||||
parser.add_argument('--input', help='Path to input image.', default='LinuxLogo.jpg')
|
||||
args = parser.parse_args()
|
||||
|
||||
cv.namedWindow(title_erosion_window)
|
||||
cv.createTrackbar(title_trackbar_element_type, title_erosion_window , 0, max_elem, erosion)
|
||||
cv.createTrackbar(title_trackbar_kernel_size, title_erosion_window , 0, max_kernel_size, erosion)
|
||||
|
||||
cv.namedWindow(title_dilatation_window)
|
||||
cv.createTrackbar(title_trackbar_element_type, title_dilatation_window , 0, max_elem, dilatation)
|
||||
cv.createTrackbar(title_trackbar_kernel_size, title_dilatation_window , 0, max_kernel_size, dilatation)
|
||||
|
||||
erosion(0)
|
||||
dilatation(0)
|
||||
cv.waitKey()
|
||||
main(args.input)
|
||||
|
||||
Reference in New Issue
Block a user