@@ -550,6 +550,86 @@ void morphologyEx(InputArray _src, OutputArray _dst, int op,
550
550
InputArray _kernel, Point anchor, int iterations,
551
551
int borderType, const Scalar& borderValue)
552
552
{
553
+ CV_INSTRUMENT_REGION ();
554
+
555
+ CV_Assert (!_src.empty ());
556
+
557
+ Mat kernel = _kernel.getMat ();
558
+ if (kernel.empty ())
559
+ {
560
+ kernel = getStructuringElement (MORPH_RECT, Size (3 , 3 ), Point (1 , 1 ));
561
+ }
562
+
563
+ Mat src = _src.getMat (), temp;
564
+ _dst.create (src.size (), src.type ());
565
+ Mat dst = _dst.getMat ();
566
+
567
+ switch (op)
568
+ {
569
+ case MORPH_ERODE:
570
+ stMorph::erode (src, dst, kernel, anchor, iterations, borderType, borderValue);
571
+ break ;
572
+ case MORPH_DILATE:
573
+ stMorph::dilate (src, dst, kernel, anchor, iterations, borderType, borderValue);
574
+ break ;
575
+ case MORPH_OPEN:
576
+ stMorph::erode (src, dst, kernel, anchor, iterations, borderType, borderValue);
577
+ stMorph::dilate (dst, dst, kernel, anchor, iterations, borderType, borderValue);
578
+ break ;
579
+ case MORPH_CLOSE:
580
+ stMorph::dilate (src, dst, kernel, anchor, iterations, borderType, borderValue);
581
+ stMorph::erode (dst, dst, kernel, anchor, iterations, borderType, borderValue);
582
+ break ;
583
+ case MORPH_GRADIENT:
584
+ stMorph::erode (src, temp, kernel, anchor, iterations, borderType, borderValue);
585
+ stMorph::dilate (src, dst, kernel, anchor, iterations, borderType, borderValue);
586
+ dst -= temp;
587
+ break ;
588
+ case MORPH_TOPHAT:
589
+ if (src.data != dst.data )
590
+ temp = dst;
591
+ stMorph::erode (src, temp, kernel, anchor, iterations, borderType, borderValue);
592
+ stMorph::dilate (temp, temp, kernel, anchor, iterations, borderType, borderValue);
593
+ dst = src - temp;
594
+ break ;
595
+ case MORPH_BLACKHAT:
596
+ if (src.data != dst.data )
597
+ temp = dst;
598
+ stMorph::dilate (src, temp, kernel, anchor, iterations, borderType, borderValue);
599
+ stMorph::erode (temp, temp, kernel, anchor, iterations, borderType, borderValue);
600
+ dst = temp - src;
601
+ break ;
602
+ case MORPH_HITMISS:
603
+ CV_Assert (src.type () == CV_8UC1);
604
+ if (countNonZero (kernel) <= 0 )
605
+ {
606
+ src.copyTo (dst);
607
+ break ;
608
+ }
609
+ {
610
+ Mat k1, k2, e1 , e2 ;
611
+ k1 = (kernel == 1 );
612
+ k2 = (kernel == -1 );
613
+
614
+ if (countNonZero (k1) <= 0 )
615
+ e1 = Mat (src.size (), src.type (), Scalar (255 ));
616
+ else
617
+ stMorph::erode (src, e1 , k1, anchor, iterations, borderType, borderValue);
618
+
619
+ if (countNonZero (k2) <= 0 )
620
+ e2 = Mat (src.size (), src.type (), Scalar (255 ));
621
+ else
622
+ {
623
+ Mat src_complement;
624
+ bitwise_not (src, src_complement);
625
+ stMorph::erode (src_complement, e2 , k2, anchor, iterations, borderType, borderValue);
626
+ }
627
+ dst = e1 & e2 ;
628
+ }
629
+ break ;
630
+ default :
631
+ CV_Error (cv::Error::StsBadArg, " unknown morphological operation" );
632
+ }
553
633
}
554
634
555
635
}} // cv::st::
0 commit comments