OpenCV——遍历文件夹图片,获取分割轮廓每一列的像素个数

#include 
#include 
#include 
#include 

using namespace std;
using namespace cv;

int Process(cv::Mat image_in)
{
	int number_max = 0;
	for (size_t i = 0; i < image_in.cols; i++)
	{
		int number = 0;
		for (size_t j = 0; j < image_in.rows; j++)
		{
			if (image_in.ptr(j)[i][0] == 255)
			{
				number ++;
			}
		}
		if (number > number_max)
		{
			number_max = number;
		}
	}
	return number_max;
}

int main()
{
	clock_t start_time, end_time;
	std::string dir_path = "C:/Users/     /Desktop/Process_side/*.bmp";
	std::vector images;
	std::vector fn;
	cv::Mat image_thred;
	cv::glob(dir_path, fn, false);
	size_t count = fn.size();
	std::cout << "count: " << count << std::endl;
	for (int i = 0; i < count; i++)
	{
		images.push_back(cv::imread(fn[i]));
		cv::threshold(images[i], image_thred, 100, 255, THRESH_BINARY_INV);
		std::string image_write = "./" + std::to_string(i) + ".bmp";
		start_time = clock();
		cv::imwrite(image_write, image_thred);
		int pix_number = Process(image_thred);
		end_time = clock();
		std::cout << "image_rows: " << image_thred.rows  << "   thres_rows: " << pix_number << std::endl;
		std::cout << "time: " << (double)(end_time - start_time) / CLOCKS_PER_SEC << std::endl;
	}
	system("pause");
}

你可能感兴趣的:(OpenCV)