資源簡介
用計算機視覺的方法處理米粒圖像。功能包括:中值濾波、直方圖顯示、大津法和經驗閾值法的閾值分割、Susan算子檢測邊緣、米粒計數、尋找最大米粒。
代碼片段和文件信息
#include???
#include?
#include?
#include???
#include???
#include“opencv2/imgproc/imgproc.hpp“
using?namespace?std;
using?namespace?cv;
//?得到圖像的直方圖??
MatND?getHistogram(Mat?&image)
{
MatND?hist;
int?channels[]?=?{?0?};
int?dims?=?1;
int?histSize[]?=?{?256?};
float?granges[]?=?{?0?255?};
const?float?*ranges[]?=?{?granges?};
calcHist(&image?1?channels?Mat()?hist?dims?histSize?ranges);
return?hist;
}
//??將圖像直方圖展示出來????
Mat?getHistogramImage(Mat?&image)
{
MatND?hist?=?getHistogram(image);
Mat?showImage(256?256?CV_8U?Scalar(0));
int?i;
double?maxValue?=?0;
minMaxLoc(hist?0?&maxValue?0?0);
for?(i?=?0;?i?256;?i++)
{
float?value?=?hist.at(i);
int?intensity?=?saturate_cast(256?-?256?*?(value?/?maxValue));
rectangle(showImage?Point(i?256?-?1)?Point((i?+?1)?-?1?intensity)?Scalar(255));
}
return?showImage;
}
Mat?SusanFun(Mat?img)
{
//susan模板
int?OffSetX[37]?=?{?-1?0?1
-2-1?0?1?2
-3-2-1?0?1?2?3
-3-2-1?0?1?2?3
-3-2-1?0?1?2?3
-2-1?0?1?2
-1?0?1?};
int?OffSetY[37]?=?{?-3-3-3
-2-2-2-2-2
-1-1-1-1-1-1-1
0?0?0?0?0?0?0
1?1?1?1?1?1?1
2?2?2?2?2
3?3?3?};
int?mHeight?=?img.rows;
int?mWidth?=?img.cols;
unsigned?char?*ucDataImg?=?(unsigned?char?*)img.data;
Mat?nImg(img.rows?img.cols?CV_8UC1);
unsigned?char?*ucDatanImg?=?(unsigned?char?*)nImg.data;
int?k?pixelSum?sameNum;
float?thresh;
for?(int?i?=?3;?i {
for?(int?j?=?3;?j {
pixelSum?=?0;
sameNum?=?0;
for?(k?=?0;?k<37;?k++)
{
pixelSum?+=?ucDataImg[i+?OffSetY[k]?*?mWidth/50+?(j?+?OffSetX[k])];
thresh?=?(float)pixelSum?/?37;//數量
if?(fabs((float)ucDataImg[(i?+?OffSetY[k])*mWidth?+?(j?+?OffSetX[k])]?-?ucDataImg[i*mWidth?+?j])?<=?thresh)
{
sameNum++;
}
}
if?(sameNum<22)//質量
{
ucDatanImg[i*mWidth?+?j]?=?255;
}
else
{
ucDatanImg[i*mWidth?+?j]?=?0;
}
}
}
return?nImg;
}
//利用直方圖選擇閾值
int?SelectThreshold(Mat?image)?{
int?i;
float?max?=?0?min?=?0;
MatND?hist;
hist?=?getHistogram(image);
for?(i?=?1;?i?<=?255;?i++)
{
if?(hist.at(i-1)?(i)&&?hist.at(i)>hist.at(i?+?1)&&?hist.at(i)>110)
{
break;
}
}
for?(i;?i?<=?255;?i++)
{
if?(hist.at(i)?<100)
break;
}
return?i;
}
int?main()?{
uint?i;
CvMemStorage*?stor?=?cvCreateMemStorage(0);
vector?>?contours;
vector?hierarchy;
int?thresh1;
double?thresh2;
Mat?ImgbinImgsubImg1?subImg2ground?grayGroundotsuImgmaxrice;
Mat?grayImgmedImgsusanImg;
//打開圖像
Img?=?imread(“..\\rice.jpg“1);
ground?=?imread(“..\\米粒圖二值圖.jpg“?1);
imshow(“groundtruth“ground);
//轉換為灰度圖像
cvtColor(Img?grayImg?CV_BGR2GRA
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????184??2018-04-08?10:31??exp1\exp1\Debug\exp1.log
?????文件??????????2??2018-04-08?10:31??exp1\exp1\Debug\exp1.tlog\CL.command.1.tlog
?????文件????????225??2018-04-08?10:31??exp1\exp1\Debug\exp1.tlog\exp1.lastbuildstate
?????文件??????????0??2018-04-08?10:31??exp1\exp1\Debug\exp1.tlog\unsuccessfulbuild
?????文件??????19456??2018-04-08?10:31??exp1\exp1\Debug\vc141.idb
?????文件?????135168??2018-04-08?10:31??exp1\exp1\Debug\vc141.pdb
?????文件???????1308??2018-04-08?10:22??exp1\exp1\exp1.aps
?????文件???????4857??2018-05-04?22:11??exp1\exp1\exp1.cpp
?????文件???????6084??2018-04-08?10:24??exp1\exp1\exp1.vcxproj
?????文件???????1190??2018-04-08?10:24??exp1\exp1\exp1.vcxproj.filters
?????文件????????165??2018-04-08?10:18??exp1\exp1\exp1.vcxproj.user
?????文件????????383??2018-04-08?10:22??exp1\exp1\resource.h
?????文件?????134687??2018-05-08?13:31??exp1\exp1\Susan輪廓圖.jpg
?????文件????????997??2018-05-03?17:10??exp1\exp1\x64\Debug\exp1.Build.CppClean.log
?????文件????????255??2018-05-04?22:11??exp1\exp1\x64\Debug\exp1.log
?????文件?????546642??2018-05-04?22:11??exp1\exp1\x64\Debug\exp1.obj
?????文件????????568??2018-05-04?22:11??exp1\exp1\x64\Debug\exp1.tlog\CL.command.1.tlog
?????文件??????24546??2018-05-04?22:11??exp1\exp1\x64\Debug\exp1.tlog\CL.read.1.tlog
?????文件????????380??2018-05-04?22:11??exp1\exp1\x64\Debug\exp1.tlog\CL.write.1.tlog
?????文件????????223??2018-05-04?22:11??exp1\exp1\x64\Debug\exp1.tlog\exp1.lastbuildstate
?????文件???????1088??2018-05-04?22:11??exp1\exp1\x64\Debug\exp1.tlog\li
?????文件???????3360??2018-05-04?22:11??exp1\exp1\x64\Debug\exp1.tlog\li
?????文件????????366??2018-05-04?22:11??exp1\exp1\x64\Debug\exp1.tlog\li
?????文件?????240640??2018-05-04?22:11??exp1\exp1\x64\Debug\vc141.idb
?????文件?????880640??2018-05-04?22:11??exp1\exp1\x64\Debug\vc141.pdb
?????文件?????181353??2018-05-08?13:31??exp1\exp1\中值濾波后的圖.jpg
?????文件??????84018??2018-05-08?13:31??exp1\exp1\大津法與groundtruth做減法.jpg
?????文件?????100958??2018-05-08?13:31??exp1\exp1\大津法二值圖.jpg
?????文件?????134571??2018-05-08?13:31??exp1\exp1\最大米粒.jpg
?????文件?????268742??2018-05-08?13:31??exp1\exp1\灰度圖.jpg
............此處省略23個文件信息
- 上一篇:STM32F4 EMWIN開發手冊_V3.1.pdf
- 下一篇:GL3520方案
評論
共有 條評論