影醉阏轩窗

衣带渐宽终不悔,为伊消得人憔悴。
扩大
缩小

《图像处理实例》 之 物体计数

目标:

灰度化--->>>阈值化--->>>形态学:

因为玉米都连在一起了,没办法通过形态学分割,所以进行距离变换:

阈值操作,这点需要进行可变的阈值进行观察,找到一个合适的阈值:

轮廓查找计数:

 

遇到的问题:

A.  

    1.用下面这个自适应阈值必须进行数据的转换,不能直接传入数据!不然程序一直崩溃,找了很久才发现。

    2.最后一个参数给个0就可以了,倒数第二个参数为大于1的数。

  3.倒数第二个参数一定为基数,当为偶数的时候一直奔溃!!!

    Middle_image.convertTo(show_image, CV_8U);

    adaptiveThreshold(show_image, output_image, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 79, 0);

B.

  1.下面这个距离变换第三个参数:名义上是那么多参数,我试了一下可以使用的是前四个,后面使用之后就崩溃,具体原因我也不知道。

 1 enum DistanceTypes {
 2 DIST_USER = -1, //!< User defined distance
 3 DIST_L1 = 1, //!< distance = |x1-x2| + |y1-y2|
 4 DIST_L2 = 2, //!< the simple euclidean distance
 5 DIST_C = 3, //!< distance = max(|x1-x2|,|y1-y2|)
 6 DIST_L12 = 4, //!< L1-L2 metric: distance = 2(sqrt(1+x*x/2) - 1))
 7 DIST_FAIR = 5, //!< distance = c^2(|x|/c-log(1+|x|/c)), c = 1.3998
 8 DIST_WELSCH = 6, //!< distance = c^2/2(1-exp(-(x/c)^2)), c = 2.9846
 9 DIST_HUBER = 7 //!< distance = |x|<c ? x^2/2 : c(|x|-c/2), c=1.345
10 };

    2.输出参数要求是8-bit or 32-bit floating-point,就是CV_8FU1、CV_32FU1,其实直接定义个Mat,然后传入数据就行了。

   distanceTransform(Middle_image, Middle_image, DIST_L2, DIST_MASK_3, 5);

代码:

C.

  定义数组长度的时候千万多比实际使用多一点,不然就一直崩溃!!!

     char countText[50];//
     sprintf(countText,"The Corn count is : %d", static_cast<int>(i));

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<iostream>
 3 #include <opencv2/opencv.hpp>
 4 #include <math.h>
 5 using namespace cv;
 6 using namespace std;
 7 
 8 RNG rng(12345);
 9 
10 Mat input_image, threshold_image, output_image, Middle_image, show_image;
11 int g_value = 68;
12 
13 void AdjustThresholdValue(int, void*);
14 
15 int main(int argc, char**argv)
16 {
17     input_image = imread("1.jpg");
18     if (input_image.data == NULL) {
19         return -1; cout << "can't open image.../";
20     }
21     imshow("Sourse Image", input_image);
22     cvtColor(input_image, Middle_image, COLOR_RGB2GRAY);
23     //imshow("Gray Image", Middle_image);
24     threshold(Middle_image, Middle_image,0,255, THRESH_BINARY| THRESH_TRIANGLE);
25     //imshow("Gray Image", Middle_image);
26     Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5), Point(-1, -1));
27     morphologyEx(Middle_image, Middle_image, MORPH_CLOSE,kernel);
28     //imshow("morphology Image", Middle_image);
29     morphologyEx(Middle_image, Middle_image, MORPH_DILATE, kernel);
30     imshow("dilate Image", Middle_image);
31     bitwise_not(Middle_image, Middle_image);
32     imshow("distance Image", Middle_image);
33     distanceTransform(Middle_image, Middle_image, DIST_L2, DIST_MASK_3, 5);
34     normalize(Middle_image, Middle_image, 0, 1, NORM_MINMAX);
35     imshow("distance Image", Middle_image);
36     
37     Middle_image.convertTo(show_image, CV_8U);
38     namedWindow("threshold Image",1);
39     createTrackbar("value","threshold Image",&g_value,100, AdjustThresholdValue);
40     AdjustThresholdValue(0,0);
41     waitKey(0);
42     return 0;
43 }
44 
45 void AdjustThresholdValue(int, void*)
46 {
47     int k = (g_value * 2 + 1);
48     adaptiveThreshold(show_image, output_image, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, k, 0);
49     Mat kernel = getStructuringElement(MORPH_RECT, Size(9, 9), Point(-1, -1));
50     morphologyEx(output_image, output_image, MORPH_OPEN, kernel);
51     imshow("threshold Image", output_image);
52     Mat temp;
53     output_image.copyTo(temp);
54     vector<vector<Point>> contours;
55     vector<Vec4i> hierarchy;
56     findContours(output_image,contours,hierarchy, RETR_TREE, CHAIN_APPROX_NONE,Point(-1,-1));
57     size_t i;
58     for (i = 0; i < contours.size(); i++)
59     {
60         drawContours(output_image, contours, static_cast<int>(i), Scalar(255, 0, 0), 2);
61     }
62     char countText[50];//定义数组长度的时候千万多比实际使用多一点,不然就一直崩溃!!!
63     sprintf(countText,"The Corn count is : %d", static_cast<int>(i));
64     putText(output_image, countText, Point(30,30), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(255, 255, 255), 2, 16);
65     imshow("Contours Image", output_image);
66 }

 

 

参考:贾老师opencv系列

 

posted on 2017-04-28 12:55  影醉阏轩窗  阅读(...)  评论(...编辑  收藏

导航

/* 线条鼠标集合 */ /* 鼠标点击求赞文字特效 */ //带头像评论