编写搜索边函数 参数设定找边的起始方向从左到右、从右到左、从上到下、从下到上、从白或黑边起 FindSubPixelEdge(Mat image, int direction)
时间: 2023-07-10 08:04:26 浏览: 44
以下是一个可能的搜索边函数的实现, 你可以参考:
```c++
enum Direction {LEFT_TO_RIGHT, RIGHT_TO_LEFT, UP_TO_DOWN, DOWN_TO_UP, WHITE_EDGE, BLACK_EDGE};
bool isValidPixel(int row, int col, Mat img) {
return (row >= 0 && row < img.rows && col >= 0 && col < img.cols);
}
Point findLeftToRightEdge(Mat img, int row) {
int col = 0;
while (col < img.cols - 1 && img.at<uchar>(row, col) <= img.at<uchar>(row, col + 1)) {
col++;
}
return Point(col, row);
}
Point findRightToLeftEdge(Mat img, int row) {
int col = img.cols - 1;
while (col > 0 && img.at<uchar>(row, col) <= img.at<uchar>(row, col - 1)) {
col--;
}
return Point(col, row);
}
Point findUpToDownEdge(Mat img, int col) {
int row = 0;
while (row < img.rows - 1 && img.at<uchar>(row, col) <= img.at<uchar>(row + 1, col)) {
row++;
}
return Point(col, row);
}
Point findDownToUpEdge(Mat img, int col) {
int row = img.rows - 1;
while (row > 0 && img.at<uchar>(row, col) <= img.at<uchar>(row - 1, col)) {
row--;
}
return Point(col, row);
}
Point findWhiteEdge(Mat img) {
for (int row = 0; row < img.rows; row++) {
for (int col = 0; col < img.cols; col++) {
if (img.at<uchar>(row, col) == 255) {
return Point(col, row);
}
}
}
return Point(-1, -1);
}
Point findBlackEdge(Mat img) {
for (int row = 0; row < img.rows; row++) {
for (int col = 0; col < img.cols; col++) {
if (img.at<uchar>(row, col) == 0) {
return Point(col, row);
}
}
}
return Point(-1, -1);
}
Point FindSubPixelEdge(Mat image, int direction) {
Mat img;
cvtColor(image, img, COLOR_BGR2GRAY);
Point edge(-1, -1);
switch (direction) {
case LEFT_TO_RIGHT:
edge = findLeftToRightEdge(img, img.rows / 2);
break;
case RIGHT_TO_LEFT:
edge = findRightToLeftEdge(img, img.rows / 2);
break;
case UP_TO_DOWN:
edge = findUpToDownEdge(img, img.cols / 2);
break;
case DOWN_TO_UP:
edge = findDownToUpEdge(img, img.cols / 2);
break;
case WHITE_EDGE:
edge = findWhiteEdge(img);
break;
case BLACK_EDGE:
edge = findBlackEdge(img);
break;
}
return edge;
}
```
这个函数接受一个图像和一个方向作为输入参数,然后返回在该方向上找到的边缘的坐标。这个函数内部实际上是根据不同的方向分别进行了搜索。在搜索过程中,我们使用了一些辅助函数,例如 `isValidPixel` 用于检查像素坐标是否在图像内; `findLeftToRightEdge` 用于在从左到右的方向上查找边缘;`findWhiteEdge`和 `findBlackEdge` 用于在白色或黑色边缘上查找边缘等等。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.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)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)