資源簡介
C++語言開發(fā),膠囊缺陷檢測程序,里面也包括膠囊圖片數(shù)據(jù)集,檢測準確率高.

代碼片段和文件信息
#include?“opencv2/imgproc/imgproc.hpp“
#include?“opencv2/highgui/highgui.hpp“
#include?
#include?
#include?
#include?
#include
using?namespace?cv;
using?namespace?std;
int?kernel_size?=?3;
char?print_flag?;
static?void?help()
{
cout?<“請在image窗口用鼠標分別對不同的區(qū)域(背景、體區(qū)、帽區(qū)、套合區(qū))進行標注,然后按空格鍵,以完成分水嶺算法,然后才能開始進行識別“?< cout?<“識別結果在命令窗口給出“< }
Mat?markerMask?img;
Point?prevPt(-1?-1);
static?void?onMouse(int?event?int?x?int?y?int?flags?void*)
{
if?(x?0?||?x?>=?img.cols?||?y?0?||?y?>=?img.rows)
return;
if?(event?==?CV_EVENT_LBUTTONUP?||?!(flags?&?CV_EVENT_FLAG_LBUTTON))
prevPt?=?Point(-1?-1);
else?if?(event?==?CV_EVENT_LBUTTONDOWN)
prevPt?=?Point(x?y);
else?if?(event?==?CV_EVENT_MOUSEMOVE?&&?(flags?&?CV_EVENT_FLAG_LBUTTON))
{
Point?pt(x?y);
if?(prevPt.x?0)
prevPt?=?pt;
line(markerMask?prevPt?pt?Scalar::all(255)?5?8?0);
line(img?prevPt?pt?Scalar::all(255)?5?8?0);
prevPt?=?pt;
imshow(“image“?img);
}
}
int?main(int?argc?char**?argv)
{
char*?filename?=?argc?>=?2???argv[1]?:?(char*)“D://flaw2.BMP“;
Mat?img0?=?imread(filename?1)?imgGray;
ofstream?outfile;
outfile.open(“myfile.txt“);
print_flag?=?0;
if?(img0.empty())
{
cout?<“Couldn‘g?open?image?“?< return?0;
}
help();
namedWindow(“image“?1);
namedWindow(“markerMask“?1);
//Canny
Mat?src_gray?src_canny?src_blur;
cvtColor(img0?src_gray?CV_BGR2GRAY);
blur(src_gray?src_blur?Size(3?3));
imshow(“Blur“?src_blur);
Canny(src_blur?src_canny?25?80?kernel_size);
imshow(“Canny“src_canny);
//Hough
Mat?src_hough;
char?warp_flag?=?0;
img0.copyTo(src_hough);
vector?lines;
double?angle_sum=0angle_avr=0;
// last?three: threshold minLinLength?maxLineGap
HoughLinesP(src_canny?lines?1?CV_PI?/?180?30?100?10);
if?(lines.size()?>?0)
{
vector?angle(lines.size());
for?(size_t?i?=?0;?i? {
Vec4i?l?=?lines[i];
line(src_hough?Point(l[0]?l[1])?Point(l[2]?l[3])?Scalar(0?0?255)?3?CV_AA);
angle[i]?=?atan2((l[1]?-?l[3])(?l[2]?-?l[0]));
printf(“%d?%d?%d?%d?%f\n“?l[0]l[1]l[2]l[3]angle[i]);
angle_sum?+=?angle[i];
}
angle_avr?=?angle_sum?/?lines.size();
angle_avr?=?angle_avr?/?CV_PI?*?180;
if?(angle_avr?0)
{
angle_avr?+=?180;
}
printf(“avrage?is?%f\n“?angle_avr);
if?(angle_avr==90.00)
{
printf(“no?warp?need\n“);
warp_flag?=?0;
}
else
{
warp_flag?=?1;
}
}
else
{
printf(“no?warp?need\n“);
warp_flag?=?0;
}
imshow(“Hough“src_hough);
//仿射變換
Mat?src_warp;
if?(warp_flag?==?1)
{
warp_flag?=?0;
Mat?rot_mat(2?3?CV_32FC1);
Point?center?=?Point(img0.cols?/?2?img0.rows?/?2);
double?scale?=?1;
//-表示順時針?+表示逆時針
rot_mat?=?getRotationMatrix2D(center?(90?-?angle_avr)?scale);
warpAffine(img0?src_warp?rot_mat?src_warp.size
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-07-28?12:51??Capsule-defect-detection-master\
?????文件????????4527??2017-07-03?10:26??Capsule-defect-detection-master\README.md
?????文件????????6313??2017-07-03?10:26??Capsule-defect-detection-master\mask.jpg
?????文件???????17635??2017-07-03?10:26??Capsule-defect-detection-master\main.cpp
?????文件???????18046??2017-07-03?10:26??Capsule-defect-detection-master\LICENSE
?????文件??????308278??2017-07-03?10:26??Capsule-defect-detection-master\good.bmp
?????文件??????308278??2017-07-03?10:26??Capsule-defect-detection-master\flaw9.bmp
?????文件??????308278??2017-07-03?10:26??Capsule-defect-detection-master\flaw8.bmp
?????文件??????308278??2017-07-03?10:26??Capsule-defect-detection-master\flaw7.bmp
?????文件??????308278??2017-07-03?10:26??Capsule-defect-detection-master\flaw6.bmp
?????文件??????308278??2017-07-03?10:26??Capsule-defect-detection-master\flaw5.bmp
?????文件??????308278??2017-07-03?10:26??Capsule-defect-detection-master\flaw4.bmp
?????文件??????308278??2017-07-03?10:26??Capsule-defect-detection-master\flaw3.bmp
?????文件??????308278??2017-07-03?10:26??Capsule-defect-detection-master\flaw2.bmp
?????文件??????308278??2017-07-03?10:26??Capsule-defect-detection-master\flaw10.bmp
?????文件??????308278??2017-07-03?10:26??Capsule-defect-detection-master\flaw1.bmp
- 上一篇:rich dad and poor dad
- 下一篇:單表替換密碼算法破解工具
評論
共有 條評論