for (int i = 1; i < inputImg.rows - 1; i++) for (int j = 1; j < inputImg.cols - 1; j++) { if (inputImg.at<uchar>(i, j) == 255) { cv::Point currentPoint(j, i); std::vector<cv::Point> neighborPoints; neighborPoints.push_back(cv::Point(j - 1, i - 1)); neighborPoints.push_back(cv::Point(j, i - 1)); neighborPoints.push_back(cv::Point(j + 1, i - 1)); neighborPoints.push_back(cv::Point(j - 1, i)); int minLabel = 255; for (std::vector<cv::Point>::iterator it = neighborPoints.begin(); it != neighborPoints.end(); it++) { int neighborLabel = labelImg.at<int>(it->y, it->x); if (neighborLabel > 0 && neighborLabel < minLabel) minLabel = neighborLabel; } if (minLabel == 255) { labelImg.at<int>(i, j) = label; label++; } else { labelImg.at<int>(i, j) = minLabel; for (std::vector<cv::Point>::iterator it = neighborPoints.begin(); it != neighborPoints.end(); it++) if (labelImg.at<int>(it->y, it->x) > 0 && labelImg.at<int>(it->y, it->x) != minLabel) { int tempLabel = labelImg.at<int>(it->y, it->x); cv::Mat mask = (labelImg == tempLabel); labelImg.setTo(minLabel, mask); } } } }
时间: 2023-06-20 12:05:48 浏览: 61
这段代码是进行图像分割的算法,具体是连通区域标记算法。其主要作用是将图像中相邻像素值相同的像素点分成一个区域,并为每个区域赋予一个标号。其中,inputImg是输入的图像,labelImg是输出的标记图像,uchar和int是OpenCV中的数据类型。
具体的算法流程如下:
1. 对于每个像素点,如果像素值为255,则进行下一步处理。
2. 找到该像素点的相邻像素点(左、上、右、下、左上、右上、左下、右下),并将这些相邻像素点放入一个容器中。
3. 如果相邻像素点中存在标记值,将标记值最小的标记赋给当前像素点的标记值,否则将当前像素点标记为新的标记值,并将标记值加1。
4. 对于相邻像素点中标记值不为0且不是当前像素点的标记值的像素点,将标记值设为最小标记值,并将这些像素点连通。
这个算法的时间复杂度为O(N),其中N为像素数量,适用于小尺寸图像的分割。
相关问题
int label = 1; for (int i = 1; i < inputImg.rows - 1; i++) for (int j = 1; j < inputImg.cols - 1; j++) { if (inputImg.at<uchar>(i, j) == 255) { cv::Point currentPoint(j, i); std::vector<cv::Point> neighborPoints; neighborPoints.push_back(cv::Point(j - 1, i - 1)); neighborPoints.push_back(cv::Point(j, i - 1)); neighborPoints.push_back(cv::Point(j + 1, i - 1)); neighborPoints.push_back(cv::Point(j - 1, i)); int minLabel = 255; for (std::vector<cv::Point>::iterator it = neighborPoints.begin(); it != neighborPoints.end(); it++) { int neighborLabel = labelImg.at<int>(it->y, it->x); if (neighborLabel > 0 && neighborLabel < minLabel) minLabel = neighborLabel; } if (minLabel == 255) { labelImg.at<int>(i, j) = label; label++; } else { labelImg.at<int>(i, j) = minLabel; for (std::vector<cv::Point>::iterator it = neighborPoints.begin(); it != neighborPoints.end(); it++) if (labelImg.at<int>(it->y, it->x) > 0 && labelImg.at<int>(it->y, it->x) != minLabel) { int tempLabel = labelImg.at<int>(it->y, it->x); cv::Mat mask = (labelImg == tempLabel); labelImg.setTo(minLabel, mask); } } } }如何让标记的不同区域数值不一样,不再是每个区域都是1,而是不同区域可以有1 2 3之类的
可以在代码中增加一个变量来记录当前已经标记的区域数,初始值为1,每当发现一个新的区域时,将该变量的值加1并赋给该区域的标记值。例如:
```cpp
int label = 1;
int numRegions = 1; // 记录当前已经标记的区域数
for (int i = 1; i < inputImg.rows - 1; i++)
for (int j = 1; j < inputImg.cols - 1; j++)
{
if (inputImg.at<uchar>(i, j) == 255)
{
cv::Point currentPoint(j, i);
std::vector<cv::Point> neighborPoints;
neighborPoints.push_back(cv::Point(j - 1, i - 1));
neighborPoints.push_back(cv::Point(j, i - 1));
neighborPoints.push_back(cv::Point(j + 1, i - 1));
neighborPoints.push_back(cv::Point(j - 1, i));
int minLabel = 255;
for (std::vector<cv::Point>::iterator it = neighborPoints.begin(); it != neighborPoints.end(); it++)
{
int neighborLabel = labelImg.at<int>(it->y, it->x);
if (neighborLabel > 0 && neighborLabel < minLabel)
minLabel = neighborLabel;
}
if (minLabel == 255)
{
labelImg.at<int>(i, j) = numRegions;
numRegions++; // 新区域的标记值为当前已经标记的区域数
}
else
{
labelImg.at<int>(i, j) = minLabel;
for (std::vector<cv::Point>::iterator it = neighborPoints.begin(); it != neighborPoints.end(); it++)
if (labelImg.at<int>(it->y, it->x) > 0 && labelImg.at<int>(it->y, it->x) != minLabel)
{
int tempLabel = labelImg.at<int>(it->y, it->x);
cv::Mat mask = (labelImg == tempLabel);
labelImg.setTo(minLabel, mask);
}
}
}
}
```
这样就可以实现不同区域的标记值不同。
优化一下这段代码else if(directionFlag == DIRECTION_FLAG1::RIGHT) { for (int var = p0.x; var < p1.x; var++) { float x = var; float y =0; if(var<0) { x = 0; } else if(var>inputImg.rows) { x = inputImg.rows; } if(k!=0) { y = k * x + b; } else if(k==0) { y = p0.y; } int data = inputImg.ptr<uchar>(int (y))[int(x)]; PointX.push_back(x); PointY.push_back(y); Gray.push_back(data); } } else if(directionFlag == DIRECTION_FLAG1::LEFT) { for (int var = p1.x; var < p0.x; var++) { float x = var; float y =0; if(var<0) { x = 0; } else if(var>inputImg.rows) { x = inputImg.rows; } if(k!=0) { y = k * x + b; } else if(k==0) { y = p0.y; } int data = inputImg.ptr<uchar>(int (y))[int(x)]; PointX.push_back(x); PointY.push_back(y); Gray.push_back(data); } }
这段代码可以进行如下优化:
1. 在判断 x 是否越界时,应该使用 inputImg.cols 而不是 inputImg.rows。
2. 在循环中,var 的增量应该为 1,而不是空着不写。
3. 在计算 y 坐标时,可以将 k * x 和 b 分别计算出来,然后再相加,这样可以减少重复计算。
4. 可以将两个 if(k!=0) 和 if(k==0) 合并成一个 if-else 语句。
优化后的代码如下:
else if(directionFlag == DIRECTION_FLAG1::RIGHT) { for (int var = p0.x; var < p1.x; var++) { float x = var; float y = 0; if(x < 0) { x = 0; } else if(x > inputImg.cols) { x = inputImg.cols; } y = k * x + b; int data = inputImg.ptr<uchar>(int (y))[int(x)]; PointX.push_back(x); PointY.push_back(y); Gray.push_back(data); } } else if(directionFlag == DIRECTION_FLAG1::LEFT) { for (int var = p1.x; var < p0.x; var++) { float x = var; float y = 0; if(x < 0) { x = 0; } else if(x > inputImg.cols) { x = inputImg.cols; } y = k * x + b; int data = inputImg.ptr<uchar>(int (y))[int(x)]; PointX.push_back(x); PointY.push_back(y); Gray.push_back(data); } }
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)