OpenCV Contour count (learning)

Source: Internet
Author: User
Tags scalar

#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp>

using namespace Std;
using namespace CV;
using namespace cv::xfeatures2d;

Mat img1, Img2, IMG3, Img4, Img5, Img6, Img_result, Img_gray1, Img_gray2, Img_gray3, Img_canny1, Img_binary1, Img_binary2, Img_dist1, Img_dist2, Kernel_1, kernel_2, Img_laplance, Img_sharp;

Char win1[] = "Window1";
Char win2[] = "Window2";
Char win3[] = "WINDOW3";
Char win4[] = "WINDOW4";
Char win5[] = "WINDOW5";
Char win6[] = "WINDOW6";
Char win7[] = "WINDOW7";

int thread_value = 10;
int max_value = 255;
RNG rng1 (12345);
RNG rng2 (1235);

Double harris_min = 0;
Double Harris_max = 0;

int Demo_surf ();
void Demo_1 (int, void*);
void demo_2 (int, void*);

int Demo_surf ()
{
Namedwindow (Win1, cv_window_autosize);
Namedwindow (win2, cv_window_autosize);
Namedwindow (Win3, cv_window_autosize);

IMG1 = Imread ("D://images//71.png");
IMG1 = Imread ("d://images//77.jpg");
if (Img1.empty ())
{
cout << "Could not load image ..." << Endl;
return 0;
}
Imshow (Win1, IMG1);

Turn grayscale
Cvtcolor (IMG1, Img_gray1, Color_bgr2gray);
Binary Value
Threshold (Img_gray1, img_binary1, 0, 255, thresh_binary | Thresh_triangle);
Imshow (Win2,img_binary1);

Morphological operations
Kernel_1 = Getstructuringelement (Morph_rect, Size (5, 5), point (-1,-1));
Dilate (Img_binary1, Img_binary2, Kernel_1, point (-1,-1), 1);
Imshow (Win3, Img_binary2);

  
Background transformations
Bitwise_not (Img_binary2,img_binary2);
Distance transform
Distancetransform (Img_binary2, Img2, CV_DIST_L2, 3);
Normalization (0-1)
Normalize (Img2, Img2, 0, 1.0, Norm_minmax);
Imshow (Win4, IMG2);

Threshold two-value segmentation
Threshold (Img2, IMG2, 0.6, 1.0, thresh_binary);
Img2.convertto (IMG3, cv_8u);
Adaptivethreshold (img3,img3,255,adaptive_thresh_gaussian_c,thresh_binary,87,0.0);
kernel_2 = Getstructuringelement (Morph_rect, Size (5, 5), point (-1,-1));
Dilate (IMG3, IMG3, Kernel_2, point (-1,-1), 4);
Normalize (IMG3, IMG3, 0, 255, Norm_minmax);
Imshow (WIN5,IMG3);

Connected areas
Vector<vector<point>> vec_contours;
Findcontours (IMG3, Vec_contours, cv_retr_external, chain_approx_simple);

IMG4 = Mat::zeros (Img1.size (), CV_8UC3);

Drawing outlines
For (size_t k=0;k<vec_contours.size (); k++)
{
Scalar color_1 = scalar (rng1.uniform (0,255), Rng1.uniform (0,255), Rng1.uniform (0,255));
Drawcontours (Img4, Vec_contours, static_cast<int> (k), color_1, 2, 8,mat ());
}
cout << "Size:" <<vec_contours.size () << Endl;
Imshow (WIN6,IMG4);

return 0;
}

void Demo_1 (int, void*)
{
  
}

void demo_2 (int, void*)
{
  
}

int main ()
{
Demo_surf ();

Waitkey (0);
return 0;
}

OpenCV Contour count (learning)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.