資源簡(jiǎn)介
對(duì)米粒圖像處理的基礎(chǔ)實(shí)驗(yàn)源代碼+實(shí)驗(yàn)報(bào)告,包括米粒圖像的讀取,變換灰度圖,Suan邊緣檢測(cè),根據(jù)直方圖進(jìn)行閾值分割;根據(jù)分割結(jié)果,求米粒位置,重心,長(zhǎng)度,面積等信息。
建議配置運(yùn)行環(huán)境:OPENCV2.4.10+VS2010(其他版本也可以,需要自己調(diào)整)
代碼片段和文件信息
#include
#include
using?namespace?cv;
using?namespace?std;
#include???//ofstream類的頭文件
#define?BYTE?unsigned?char
//函數(shù)聲明
void?ReadImageRgbImageToGrayImage(Mat?&inputImg?Mat?&grayImg);
void?VisitContinueImgByPointer(Mat?&inputImg?Mat?&dstImg);
Mat?SusanFun(Mat?img);
void?DrowCenter(Mat?inputImage);
//讀取圖像并變成灰度圖
void?ReadImageRgbImageToGrayImage(Mat?&inputImg?Mat?&grayImg)
{
inputImg?=?cvLoadImage(“米粒4.png“);
if?(inputImg.empty())//讀取失敗時(shí)
{
cout?<“Could?not?open?or?find?the?image“?< }
//namedWindow(“原圖“?WINDOW_AUTOSIZE);?
//resize(inputImg?inputImg?Size()0.250.25);//長(zhǎng)寬都變?yōu)樵瓉?lái)的0.5倍
????imshow(“原圖“?inputImg);????
cvtColor(inputImg?grayImg?CV_RGB2GRAY);//把圖片轉(zhuǎn)化為灰度圖
}
//訪問(wèn)像素
void?VisitContinueImgByPointer(Mat?&inputImg?Mat?&dstImg)
{
????dstImg?=?inputImg.clone();
????int?rows?=?dstImg.rows;
????int?cols?=?dstImg.cols;
????int?channels?=?dstImg.channels();
????if(dstImg.isContinuous())
????{
????????cols?*=?rows;
????????rows?=?1;
????????//cout?<“is?continuous?“?<????}
????for(int?i?=?0;?i?????{
????????uchar*?data?=?dstImg.ptr(i);
????????for(int?j?=?0;?j?????????{
//ofstream?mycout(“Q1.txt“);
????//mycout<????????????data[j]?=?0;??//處理每一個(gè)像素
????????????//add?code
????????}
????}?
????//若存儲(chǔ)連續(xù),等效于以下代碼
????//uchar*?data?=?dstImg.data;
????//for(int?i?=?0;?i?????//????data[i]?=?155;????//處理每一個(gè)像素
}
//susan算子
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;//數(shù)量?自適應(yīng)閾值?????(不是全局閾值)
thresh=10;
if?(fabs((float)ucDataImg[(i?+?OffSetY[k])*mWidth?+?(j?+?OffSetX[k])]?-?ucDataImg[i*mWidth?+?j])?<=?thresh)
{
sameNum++;
}
}
if?(sameNum<22)//質(zhì)量?第二個(gè)閾值選擇
{
ucDatanImg[i*mWidth?+?j]?=?255;
}
else
{
ucDatanImg[i*mWidth?+?j]?=?0;
}
}
}
return?nImg;
}
//圖像的形態(tài)學(xué)處理:膨脹與腐蝕(主要是為了解決susan算子邊緣檢測(cè)結(jié)果出現(xiàn)斷裂的問(wèn)題)
Mat?DilateAndErode(Mat?&inputImg)
{
Mat?out;
????//獲取自定義核
Mat?element?=?getStructuringElement(MORPH_RECT?Size(3?3));?//第一個(gè)參數(shù)MORPH_RECT表示矩形的卷積核,當(dāng)然還可以選擇橢圓形的、交叉型的?
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2019-01-11?09:09??OPENCVTEST\
?????目錄???????????0??2018-12-26?20:51??OPENCVTEST\Debug\
?????文件??????127488??2018-12-26?20:51??OPENCVTEST\Debug\OPENCVTEST.exe
?????文件??????725764??2018-12-26?20:51??OPENCVTEST\Debug\OPENCVTEST.ilk
?????文件?????1641472??2018-12-26?20:51??OPENCVTEST\Debug\OPENCVTEST.pdb
?????目錄???????????0??2019-01-11?08:52??OPENCVTEST\ipch\
?????目錄???????????0??2019-01-11?08:52??OPENCVTEST\ipch\opencvtest-e1fb7881\
?????文件????57868288??2019-01-11?08:52??OPENCVTEST\ipch\opencvtest-e1fb7881\opencvtest-296a8017.ipch
?????目錄???????????0??2018-12-19?11:23??OPENCVTEST\OPENCVTEST\
?????文件???????29720??2018-12-12?21:32??OPENCVTEST\OPENCVTEST\Center.tif
?????目錄???????????0??2018-12-26?20:51??OPENCVTEST\OPENCVTEST\Debug\
?????文件?????????548??2018-12-26?11:03??OPENCVTEST\OPENCVTEST\Debug\cl.command.1.tlog
?????文件???????10654??2018-12-26?11:03??OPENCVTEST\OPENCVTEST\Debug\CL.read.1.tlog
?????文件?????????318??2018-12-26?11:03??OPENCVTEST\OPENCVTEST\Debug\CL.write.1.tlog
?????文件????????3468??2018-12-26?20:51??OPENCVTEST\OPENCVTEST\Debug\li
?????文件????????5638??2018-12-26?20:51??OPENCVTEST\OPENCVTEST\Debug\li
?????文件?????????570??2018-12-26?20:51??OPENCVTEST\OPENCVTEST\Debug\li
?????文件??????502608??2018-12-26?11:03??OPENCVTEST\OPENCVTEST\Debug\main.obj
?????文件?????????672??2018-12-26?20:51??OPENCVTEST\OPENCVTEST\Debug\mt.command.1.tlog
?????文件?????????946??2018-12-26?20:51??OPENCVTEST\OPENCVTEST\Debug\mt.read.1.tlog
?????文件?????????376??2018-12-26?20:51??OPENCVTEST\OPENCVTEST\Debug\mt.write.1.tlog
?????文件????????1020??2018-12-19?11:25??OPENCVTEST\OPENCVTEST\Debug\OPENCVTEST.Build.CppClean.log
?????文件?????????381??2018-12-26?20:51??OPENCVTEST\OPENCVTEST\Debug\OPENCVTEST.exe.intermediate.manifest
?????文件??????????54??2018-12-26?20:51??OPENCVTEST\OPENCVTEST\Debug\OPENCVTEST.lastbuildstate
?????文件????????2983??2018-12-26?20:51??OPENCVTEST\OPENCVTEST\Debug\OPENCVTEST.log
?????文件?????????713??2018-12-19?11:55??OPENCVTEST\OPENCVTEST\Debug\OPENCVTEST.vcxprojResolveAssemblyReference.cache
?????文件???????????0??2018-12-19?11:25??OPENCVTEST\OPENCVTEST\Debug\OPENCVTEST.write.1.tlog
?????文件??????478208??2018-12-26?11:03??OPENCVTEST\OPENCVTEST\Debug\vc100.idb
?????文件?????1036288??2018-12-26?11:03??OPENCVTEST\OPENCVTEST\Debug\vc100.pdb
?????文件???????12194??2018-12-26?11:03??OPENCVTEST\OPENCVTEST\main.cpp
?????文件????????8949??2018-12-19?20:09??OPENCVTEST\OPENCVTEST\OPENCVTEST.vcxproj
............此處省略63個(gè)文件信息
評(píng)論
共有 條評(píng)論