#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);

  //转灰度
  cvtColor(img1, img_gray1, COLOR_BGR2GRAY);
  //二值化
  threshold(img_gray1, img_binary1, 0, 255, THRESH_BINARY | THRESH_TRIANGLE);
  imshow(win2,img_binary1);

  //形态学操作
  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);

  
  //背景变换
  bitwise_not(img_binary2,img_binary2);
  //距离变换
  distanceTransform(img_binary2, img2, CV_DIST_L2, 3);
  //归一化(0-1)
  normalize(img2, img2, 0, 1.0, NORM_MINMAX);
  imshow(win4, img2);

  //阈值化二值分割
  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);

  //连通区域
  vector<vector<Point>> vec_contours;
  findContours(img3, vec_contours, CV_RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

  img4 = Mat::zeros(img1.size(),CV_8UC3);

  //绘制轮廓
  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轮廓计数(学习)的更多相关文章

  1. OpenCV之Python学习笔记

    OpenCV之Python学习笔记 直都在用Python+OpenCV做一些算法的原型.本来想留下发布一些文章的,可是整理一下就有点无奈了,都是写零散不成系统的小片段.现在看 到一本国外的新书< ...

  2. OpenCV 轮廓基本特征

     http://blog.csdn.net/tiemaxiaosu/article/details/51360499 OpenCV 轮廓基本特征 2016-05-10 10:26 556人阅读 评论( ...

  3. OpenCV轮廓vectorvector

    OpenCV轮廓vectorvector,vector,vector,vector https://blog.csdn.net/Ahuuua/article/details/80593388   轮廓 ...

  4. Opencv调用深度学习模型

    https://blog.csdn.net/lovelyaiq/article/details/79929393 https://blog.csdn.net/qq_29462849/article/d ...

  5. 语义分割:基于openCV和深度学习(二)

    语义分割:基于openCV和深度学习(二) Semantic segmentation in images with OpenCV 开始吧-打开segment.py归档并插入以下代码: Semanti ...

  6. 语义分割:基于openCV和深度学习(一)

    语义分割:基于openCV和深度学习(一) Semantic segmentation with OpenCV and deep learning 介绍如何使用OpenCV.深度学习和ENet架构执行 ...

  7. opencv——对象计数

     思路: 1.通过形态学操作.阈值处理.距离变换等方法,使得各个轮廓分开 2.计算轮廓数量 #include <opencv2/opencv.hpp> #include <iostr ...

  8. opencv安装及学习资料

    第一次装时win7+VS2010+opencv3.0,结果不成功,原因解压出来的没有vc10,可能新版本不在支持vc的旧版本了.所以换了VS2013+opencv3.0,比较经典的安装时VS2010+ ...

  9. 【转载】openCV轮廓操作

    声明:非原创,转载自互联网,有问题联系博主 1.轮廓的提取 从图片中将目标提取出来,常常用到的是提取目标的轮廓. OpenCV里提取目标轮廓的函数是findContours(), 它的输入图像是一幅二 ...

随机推荐

  1. oracle之 RAC Interconnect之HAIP

    0. 背景 Oracle 从11.2.0.2开始引入了一个新特性叫做Redundant Interconnect,简称HAIP.HAIP的目的用来代替操作系统级别的网卡绑定以实现Active-Acti ...

  2. caffemodel的读取与修改

    直接撸代码~ import caffe import numpy as np caffe.set_mode_cpu() net = caffe.Net('myprototxt.prototxt', ' ...

  3. 爬虫高性能相关(协程效率最高,IO密集型)

    一背景常识 爬虫的本质就是一个socket客户端与服务端的通信过程,如果我们有多个url待爬取,采用串行的方式执行,只能等待爬取一个结束后才能继续下一个,效率会非常低. 需要强调的是:串行并不意味着低 ...

  4. C# List泛型转换,int,string 转字符,转数组

    List转字符串 List<string> List = new List<string>(); string strArray = string.Join(",&q ...

  5. 大白话系列之C#委托与事件讲解大结局

    声明:本系列非原创,因为太精彩才转载,如有侵权请通知删除,原文:http://www.cnblogs.com/wudiwushen/archive/2010/04/20/1698795.html 今天 ...

  6. Windows网络编程基础知识

    1.WinSock的初始化 #include<iostream> #include<WinSock2.h> #include<MSWSock.h> #pragma ...

  7. MyBatis单个参数的动态语句引用

    参考:http://blog.csdn.net/viviju1989/article/details/17071909 是当我们的参数为String时,在sql语句中#{name} 会去我们传进来的参 ...

  8. IDA Pro 权威指南学习笔记(十一) - 名称与命名

    多数情况下,要修改一个名称,只需单击想要修改的名称(使其突出显示),并使用快捷键 N 打开更名对话框 右击需要修改的名称,并在出现的上下文菜单中选择 Rename 选项,也可以更改名称 参数和局部变量 ...

  9. ceph中用sgdisk工具进行分区--------固定uuid

    ceph中两种类型分区的type code: type type code journal 45b0969e-9b03-4f30-b4c6-b4b80ceff106 osd 4fbd7e29-9d25 ...

  10. 编码转换(UTF8->GBK)

    WCHAR woutstr[]; ]; , value, -, NULL, ); MultiByteToWideChar(CP_UTF8, , value, -, woutstr, len); len ...