opencv中使用findContours、contourArea的问题。 10
尝试使用findContours、contourArea分别找到最外围的轮廓和最外围轮廓的面积,发现最外围轮廓不是图形的整个轮廓,同时,将原图旋转90度,参数保持不变,再...
尝试使用findContours、contourArea分别找到最外围的轮廓和最外围轮廓的面积,发现最外围轮廓不是图形的整个轮廓,同时,将原图旋转90度,参数保持不变,再运行程序发现面积发生了变化。请问怎么样才能得到整个轮廓的面积?附上程序:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat img=imread("leaf2.jpg");
if(!img.data)
{
return -1;
}
imshow("source",img);
Mat imgGray,cannyOutput;
imgGray.create(img.size(),CV_8UC1);
cvtColor(img,imgGray,CV_BGR2GRAY);
blur(imgGray,imgGray,Size(3,3));
//imshow("gray",imgGray);
Canny(imgGray,cannyOutput,0,50);
imshow("cannyOutput",cannyOutput);
vector< vector<Point> > contours;
findContours(cannyOutput,contours,RETR_EXTERNAL,CHAIN_APPROX_NONE);
cout<<"size of contours:"<<contours.size()<<endl;
Mat area(1,contours.size(),CV_32FC1);
float maxArea=area.at<float>(0);
for(int i=0;i<(int)contours.size();i++)//将有符号的contours.size()强制转换位非符号的;
{
area.at<float>(i)=contourArea(contours[i]);
//cout<<"area "<<i<<" ="<<area.at<float>(i)<<endl;
if(maxArea<area.at<float>(i))
{
maxArea=area.at<float>(i);
}
}
cout<<"The number of pixels is "<<maxArea<<endl;
Mat result(img.size(),CV_8U,Scalar(255));
drawContours(result,contours,-1,Scalar(0),2);
imshow("result",result);
waitKey();
return 0;
} 展开
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat img=imread("leaf2.jpg");
if(!img.data)
{
return -1;
}
imshow("source",img);
Mat imgGray,cannyOutput;
imgGray.create(img.size(),CV_8UC1);
cvtColor(img,imgGray,CV_BGR2GRAY);
blur(imgGray,imgGray,Size(3,3));
//imshow("gray",imgGray);
Canny(imgGray,cannyOutput,0,50);
imshow("cannyOutput",cannyOutput);
vector< vector<Point> > contours;
findContours(cannyOutput,contours,RETR_EXTERNAL,CHAIN_APPROX_NONE);
cout<<"size of contours:"<<contours.size()<<endl;
Mat area(1,contours.size(),CV_32FC1);
float maxArea=area.at<float>(0);
for(int i=0;i<(int)contours.size();i++)//将有符号的contours.size()强制转换位非符号的;
{
area.at<float>(i)=contourArea(contours[i]);
//cout<<"area "<<i<<" ="<<area.at<float>(i)<<endl;
if(maxArea<area.at<float>(i))
{
maxArea=area.at<float>(i);
}
}
cout<<"The number of pixels is "<<maxArea<<endl;
Mat result(img.size(),CV_8U,Scalar(255));
drawContours(result,contours,-1,Scalar(0),2);
imshow("result",result);
waitKey();
return 0;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询