#include
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include"math.h"
#include
int main(int argc, const char * argv[]) {
cv::Mat image = cv::imread("C:\\Users\\User\\Desktop\\untitled.png");
//cv::Mat image=cv::imread("C:\Users\User\Desktop"
if (!image.data) {
std::cout << "Image file not found\n";
return 1;
}
//Prepare the image for findContours
cv::cvtColor(image, image, CV_BGR2GRAY);
cv::threshold(image, image, 150, 255, CV_THRESH_BINARY);
//Find the contours. Use the contourOutput Mat so the original image doesn't get overwritten
std::vector> contours;
cv::Mat contourOutput = image.clone();
cv::findContours(contourOutput, contours, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);
//Draw the contours
cv::Mat contourImage(image.size(), CV_8UC3, cv::Scalar(0, 0, 0));
cv::Scalar colors[3];
colors[0] = cv::Scalar(255, 0, 0);
colors[1] = cv::Scalar(0, 255, 0);
colors[2] = cv::Scalar(0, 0, 255);
for (size_t idx = 0; idx < contours.size(); idx++) {
int thickness =2 ;
//size_t idx = 2;
cv::drawContours(contourImage,contours,idx,colors[idx % 1],thickness);
//cv::drawContours(contourImage, contours, idx, colors[idx % 1], thickness);
}
for (int i = 0; i < contours.size(); i++)
std::cout << "Area " << i << ": " << contourArea(contours[i]) << std::endl;
↧