19,468
社区成员
发帖
与我相关
我的任务
分享
if(dst.channels() == 1)//dst是Mat类型的对象,里面放着就是图像
{
for(int i=0; i<dst.rows; i++)
{
sum[i] = 0;
for(int j=0; j<dst.cols; j++)
{
if(dst.at<uchar>(i,j)){.....}//这里就是遍历每个像素点的数值了。修改同样直接赋值就可以!
}
}
}
/*------------------------------------------------------------------------------------------*\
This file contains material supporting chapter 7 of the cookbook:
Computer Vision Programming using the OpenCV Library.
by Robert Laganiere, Packt Publishing, 2011.
This program is free software; permission is hereby granted to use, copy, modify,
and distribute this source code, or portions thereof, for any purpose, without fee,
subject to the restriction that the copyright notice may not be removed
or altered from any source or altered source distribution.
The software is released on an as-is basis and without any warranties of any kind.
In particular, the software is not guaranteed to be fault-tolerant or free from failure.
The author disclaims all warranties with regard to this software, any use,
and any consequent failure, is purely the responsibility of the user.
Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
\*------------------------------------------------------------------------------------------*/
#include "StdAfx.h"
#include <iostream>
#include <vector>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
int main()
{
// Read input binary image
cv::Mat image= cv::imread("test.png",0);
if (!image.data)
return 0;
cv::namedWindow("Binary Image");
cv::imshow("Binary Image",image);
// Get the contours of the connected components
std::vector<std::vector<cv::Point>> contours;
cv::findContours(image,
contours, // a vector of contours
CV_RETR_EXTERNAL, // retrieve the external contours
CV_CHAIN_APPROX_NONE); // retrieve all pixels of each contours
// Print contours' length
std::cout << "Contours: " << contours.size() << std::endl;
std::vector<std::vector<cv::Point>>::const_iterator itContours= contours.begin();
for ( ; itContours!=contours.end(); ++itContours)
{
std::cout << "Size: " << itContours->size() << std::endl;
}
// draw black contours on white image
cv::Mat result(image.size(),CV_8U,cv::Scalar(255));
cv::drawContours(result,contours,
-1, // draw all contours
cv::Scalar(0), // in black
2); // with a thickness of 2
cv::namedWindow("Contours");
cv::imshow("Contours",result);
// draw contours on the original image
cv::Mat original= cv::imread("test.png");
cv::drawContours(original,contours,
-1, // draw all contours
cv::Scalar(255,255,255), // in white
-1); // with a thickness of 2
cv::namedWindow("Contours on Animals");
cv::imshow("Contours on Animals",original);
// Let's now draw black contours on white image
result.setTo(cv::Scalar(255));
cv::drawContours(result,contours,
-1, // draw all contours
cv::Scalar(0), // in black
-1); // with a thickness of 1
image= cv::imread("test.png",0);
cv::waitKey();
return 0;
}