1.opencv库配置
1.opencv官网(https://opencv.org/)下载3.40版本opencv库,直接解压后放在非中文路径下;
2.配置电脑环境:右键点开桌面此电脑属性--高级系统设置--环境变量--Path--编辑,把之前解压好的OpenCV路径下的bin路径添加到Path系统环境变量中。重启一下
3.最后操作,在VS2017中创建一个新项目(debug,x64)。
打开属性管理器,右键属性,找到vc++目录——包含目录——右键选择编辑,添加opencv build目录下的include,以及include目录下的opencv,opencv2。
找到 vc++目录——库目录——右键选择编辑,添加build中的x64/vs14/lib;
找到链接器——输入——附加依赖项,添加opencv_world340d.lib。
4.配置完毕 开始使用。
测试一下
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
Mat image = imread(""); //存放自己图像的路径
imshow("显示图像", image);
waitKey(0);
return 0;
}
2.分块处理方法
void Seed_Filling(const cv::Mat& binImg, cv::Mat& lableImg)
{
if (binImg.empty() ||
binImg.type() != CV_8UC1)
{
return;
}
binImg.type() != CV_8UC1)
{
return;
}
lableImg.release();
binImg.convertTo(lableImg, CV_32SC1);
binImg.convertTo(lableImg, CV_32SC1);
int label = 1;
int rows = binImg.rows - 1;
int cols = binImg.cols - 1;
for (int i = 1; i < rows - 1; i++)
{
int* data = lableImg.ptr<int>(i);
for (int j = 1; j < cols - 1; j++)
{
if (data[j] == 1)
{
stack<pair<int, int>> neighborPixels;
neighborPixels.push(pair<int, int>(i, j));
++label;
while (!neighborPixels.empty())
{
pair<int, int> curPixel = neighborPixels.top();
int curX = curPixel.first;
int curY = curPixel.second;
lableImg.at<int>(curX, curY) = label;
int cols = binImg.cols - 1;
for (int i = 1; i < rows - 1; i++)
{
int* data = lableImg.ptr<int>(i);
for (int j = 1; j < cols - 1; j++)
{
if (data[j] == 1)
{
stack<pair<int, int>> neighborPixels;
neighborPixels.push(pair<int, int>(i, j));
++label;
while (!neighborPixels.empty())
{
pair<int, int> curPixel = neighborPixels.top();
int curX = curPixel.first;
int curY = curPixel.second;
lableImg.at<int>(curX, curY) = label;
neighborPixels.pop();
if (lableImg.at<int>(curX, curY - 1) == 1)
}
neighborPixels.push(pair<int, int>(curX, curY - 1));
}
if (lableImg.at<int>(curX, curY + 1) == 1)
{
neighborPixels.push(pair<int, int>(curX, curY + 1));
}
if (lableImg.at<int>(curX - 1, curY) == 1)
{
neighborPixels.push(pair<int, int>(curX - 1, curY));
}
if (lableImg.at<int>(curX + 1, curY) == 1)
{
neighborPixels.push(pair<int, int>(curX + 1, curY));
}
}
}
}
}
cout << label << endl;
}
}
neighborPixels.push(pair<int, int>(curX, curY - 1));
}
if (lableImg.at<int>(curX, curY + 1) == 1)
{
neighborPixels.push(pair<int, int>(curX, curY + 1));
}
if (lableImg.at<int>(curX - 1, curY) == 1)
{
neighborPixels.push(pair<int, int>(curX - 1, curY));
}
if (lableImg.at<int>(curX + 1, curY) == 1)
{
neighborPixels.push(pair<int, int>(curX + 1, curY));
}
}
}
}
}
cout << label << endl;
}