资源简介
使用opencv提取图片中光斑的质心达到了亚像素级别
代码片段和文件信息
#include
#include
#include
using namespace cv;
using namespace std;
//-----------------------------------【main( )函数】------------------------------------------
// 描述:控制台应用程序的入口函数,我们的程序从这里开始
//-----------------------------------------------------------------------------------------------
int g_nMedianBlurValue = 2;
Mat element = getStructuringElement(MORPH_RECT Size(3 3));
vector point_seq;
Mat selectChannel(Mat src int channel);
bool objectDetection(Mat src int threshold_vlaue int areasize int channel)
{/*
@param[out] success or fail.
@param[in] threshold threshold for segmentation.
@param[in] areasize threshold for selecting large-enough object.
@param[in] channel 1(B) 2(G) 3(R) 4(H) 5(S) 6(V) other(GRAY)
*/
int i;
//cvCopy(srcdisplayImageNULL);
Mat displayImage = src.clone();
//cvClearSeq(point_seq);
//cvClearSeq(contour);
//cvClearMemStorage(storage);
Mat imageSC = selectChannel(src channel);
//smooth(imageSCimageSCCV_MEDIAN);//图像中值滤波
medianBlur(imageSC imageSC g_nMedianBlurValue * 2 + 1);//中值滤波
//cvAdaptiveThreshold( gray gray 255 CV_ADAPTIVE_THRESH_MEAN_CCV_THRESH_BINARY 7 0);
threshold(imageSC imageSC threshold_vlaue 255 CV_THRESH_BINARY);
if (1)
threshold(imageSC imageSC threshold_vlaue 255 CV_THRESH_BINARY_INV); //cvNot(imageSCimageSC);//把元素的每一位取反
//imageSC->origin = 0;
dilate(imageSC imageSC element);//膨胀
//CvScalar color = CV_RGB( 155 155155 );//灰度图像
Scalar color = Scalar(155 155 155);
vector> Contours;
vector Hierarchy;
findContours(imageSC Contours Hierarchy CV_RETR_TREE CV_CHAIN_APPROX_SIMPLE Point(0 0));
vector mu(Contours.size());
vector mc(Contours.size());
Mat drawing = Mat::zeros(src.size() CV_8UC3);
for (int i = 0; i< Contours.size(); i++) {
mu[i] = moments(Contours[i] false);
}
for (int i = 0; i< Contours.size(); i++) {
mc[i] = Point2d(mu[i].m10 / mu[i].m00 mu[i].m01 / mu[i].m00);
}
for (int i = 0; i< Contours.size(); i++) {
double tmparea = fabs(contourArea(Contours[i]));
if (tmparea>areasize) {
drawContours(displayImage Contours i color 2 8 Hierarchy 0 Point());//you can change 1 to CV_FILLED
if (1) {
//Mat region=Contour[i];
//CvMoments moments;
//cvMoments( region &moments0 );
//cvMoments( &contour &moments0 );
// cvDrawContours( cnt_img _contours CV_RGB(25500) CV_RGB(02550) _levels 3 CV_AA cvPoint(00) ); CV_FILLED
////////////////////////////////////////////////
/*float xdyd;
int xcyc;
float m00 m10 m01 inv_m00;
Point point;
m00 = moments.m00;
m10 = moments.m10;
m01 = moments.m01;
inv_m00 = 1. / m00;
xd =m10 * inv_m00;//一阶矩
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-11-21 20:26 opencv提取质心\
目录 0 2017-11-21 20:25 opencv提取质心\.vs\
目录 0 2017-11-21 20:25 opencv提取质心\.vs\opencv提取质心\
目录 0 2017-11-21 20:25 opencv提取质心\.vs\opencv提取质心\v14\
文件 23040 2017-11-21 21:01 opencv提取质心\.vs\opencv提取质心\v14\.suo
目录 0 2017-11-21 20:29 opencv提取质心\opencv提取质心\
文件 5855 2017-11-21 20:29 opencv提取质心\opencv提取质心\main.cpp
文件 7388 2017-11-21 20:26 opencv提取质心\opencv提取质心\opencv提取质心.vcxproj
文件 945 2017-11-21 20:26 opencv提取质心\opencv提取质心\opencv提取质心.vcxproj.filters
目录 0 2017-11-21 20:26 opencv提取质心\opencv提取质心\x64\
目录 0 2017-11-21 20:29 opencv提取质心\opencv提取质心\x64\Debug\
文件 694139 2017-11-21 20:29 opencv提取质心\opencv提取质心\x64\Debug\main.obj
文件 1307 2017-11-21 20:29 opencv提取质心\opencv提取质心\x64\Debug\opencv提取质心.log
目录 0 2017-11-21 20:29 opencv提取质心\opencv提取质心\x64\Debug\opencv提取质心.tlog\
文件 574 2017-11-21 20:29 opencv提取质心\opencv提取质心\x64\Debug\opencv提取质心.tlog\CL.command.1.tlog
文件 24010 2017-11-21 20:29 opencv提取质心\opencv提取质心\x64\Debug\opencv提取质心.tlog\CL.read.1.tlog
文件 404 2017-11-21 20:29 opencv提取质心\opencv提取质心\x64\Debug\opencv提取质心.tlog\CL.write.1.tlog
文件 1172 2017-11-21 20:29 opencv提取质心\opencv提取质心\x64\Debug\opencv提取质心.tlog\li
文件 3140 2017-11-21 20:29 opencv提取质心\opencv提取质心\x64\Debug\opencv提取质心.tlog\li
文件 390 2017-11-21 20:29 opencv提取质心\opencv提取质心\x64\Debug\opencv提取质心.tlog\li
文件 193 2017-11-21 20:29 opencv提取质心\opencv提取质心\x64\Debug\opencv提取质心.tlog\opencv提取质心.lastbuildstate
文件 674816 2017-11-21 20:29 opencv提取质心\opencv提取质心\x64\Debug\vc140.idb
文件 1150976 2017-11-21 20:29 opencv提取质心\opencv提取质心\x64\Debug\vc140.pdb
文件 13266944 2017-11-21 21:01 opencv提取质心\opencv提取质心.VC.db
文件 1336 2017-11-21 20:25 opencv提取质心\opencv提取质心.sln
目录 0 2017-11-21 20:26 opencv提取质心\x64\
目录 0 2017-11-21 20:26 opencv提取质心\x64\Debug\
文件 174080 2017-11-21 20:29 opencv提取质心\x64\Debug\opencv提取质心.exe
文件 862456 2017-11-21 20:29 opencv提取质心\x64\Debug\opencv提取质心.ilk
文件 2125824 2017-11-21 20:29 opencv提取质心\x64\Debug\opencv提取质心.pdb
- 上一篇:EC20_发英文短信例程.zip
- 下一篇:文本对比工具
评论
共有 条评论