|
楼主 |
发表于 2022-12-20 10:05:50
|
显示全部楼层
Bot:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat srcImage = imread("egg.jpg",1);
if(srcImage.empty())
{
cout<<"图片加载失败!"<<endl;
return -1;
}
imshow("原始图:", srcImage);
Mat midImage,dstImage;//临时变量和目标图的定义
cvtColor(srcImage, midImage, COLOR_BGR2GRAY);//转化边缘检测后的图为灰度图
blur(midImage, midImage, Size(3,3));//中值滤波
Canny(midImage, dstImage, 50, 200, 3);//运行canny算子
vector<Vec2f> lines;
HoughLines(dstImage, lines, 1, CV_PI/180, 100, 0, 0 );
int count = 0;
for( size_t i = 0; i < lines.size(); i++ )
{
float rho = lines[i][0], theta = lines[i][1];
Point pt1, pt2;
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
pt1.x = cvRound(x0 + 1000*(-b));
pt1.y = cvRound(y0 + 1000*(a));
pt2.x = cvRound(x0 - 1000*(-b));
pt2.y = cvRound(y0 - 1000*(a));
line( srcImage, pt1, pt2, Scalar(55,100,195), 1, LINE_AA);
count++;
}
cout<<"共有"<<count<<"个鸡蛋"<<endl;
imshow("结果图:", srcImage);
waitKey(0);
return 0;
} |
|