void CrelaxMyFriendDlg::OnBnClickedOk()

{

hdc = this->GetDC()->GetSafeHdc();

// TODO: 在此添加控件通知处理程序代码

string imAddr = "c:/Users/actorsun/Pictures/";

string imAddr1 = imAddr+"rice.png";

Mat relax1, positive;

relax1 = imread(imAddr1);

Mat relax;

cvtColor(relax1, relax, COLOR_BGR2GRAY);

threshold(relax, positive, 50, 255, THRESH_BINARY);

imshow(positive);

RNG rng(101);

Mat a, b;

int cout = connectedComponentsWithStats(positive, positive,a,b,8,CV_16U);

//其中b是中心点的坐标,本例中是一个7行,2列的东西,每一行代表一个中心点坐标值

imshow("1", positive);

waitKey();

vector colors;

for (int i = 0; i < cout; i++)

{

Vec3b vec(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256));

colors.push_back(vec);

}

Mat result(positive.rows,positive.cols,relax1.type());

for (int row = 0; row < relax1.rows; row++)

{

for (int col = 0; col < relax1.cols; col++)

{

int temp = positive.at(row,col);

if (temp == 0)

continue;

result.at(row, col) = colors[temp];

}

}

imshow("标记后的图", result);

waitKey();

return;

}

/** @overload

@param image the 8-bit single-channel image to be labeled

@param labels destination labeled image

@param stats statistics output for each label, including the background label.

Statistics are accessed via stats(label, COLUMN) where COLUMN is one of

#ConnectedComponentsTypes, selecting the statistic. The data type is CV_32S.

@param centroids centroid output for each label, including the background label. Centroids are

accessed via centroids(label, 0) for x and centroids(label, 1) for y. The data type CV_64F.

@param connectivity 8 or 4 for 8-way or 4-way connectivity respectively

@param ltype output image label type. Currently CV_32S and CV_16U are supported.

*/

CV_EXPORTS_W int connectedComponentsWithStats(InputArray image, OutputArray labels,

OutputArray stats, OutputArray centroids,

int connectivity = 8, int ltype = CV_32S);

/** @brief Random Number Generator

Random number generator. It encapsulates the state (currently, a 64-bit

integer) and has methods to return scalar random values and to fill

arrays with random values. Currently it supports uniform and Gaussian

(normal) distributions. The generator uses Multiply-With-Carry

algorithm, introduced by G. Marsaglia (

).

Gaussian-distribution random numbers are generated using the Ziggurat

algorithm ( ),

introduced by G. Marsaglia and W. W. Tsang.

*/

class CV_EXPORTS RNG

{

。。。

}

上面这个代码是MFC程序里面的一个函数,不能够直接运行,但是如果学过MFC相信没有问题完全能够看得懂。

public static final int

CC_STAT_LEFT = 0,

CC_STAT_TOP = 1,

CC_STAT_WIDTH = 2,

CC_STAT_HEIGHT = 3,

CC_STAT_AREA = 4,

CC_STAT_MAX = 5;

上例当中,count=7(也许可能是count,我写成了cout,也就不改了).说明检测到7个连通域。

精彩文章

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。