c/c++ 二值图像 边界提取

//二值图像的边界提取可以采用四领域或八领域方法:

//当前像素值为1,且周围四个(或8个)像素的值都为1,则此行列对应的结果图像的的像素为0

//否则保持不变

//本代码在vs2019调试通光。

 #include <iostream>

using namespace std;
typedef unsigned char  Byte;

void Edge4(Byte f[6][6], int h, int w, Byte mb, Byte g[6][6])
{   for (int y = 1; y < h-1; y++)
    {    for (int x= 1; x < w-1; x++)
        {  if (f[y][x] == mb && f[y + 1][x] == mb && f[y - 1][x] == mb
                && f[y][x+1] == mb && f[y][x-1] == mb
                )//
            g[y][x] = 1 - f[y][x];
            else
            g[y][x] = f[y][x];
        }
        cout << endl;
    }
}
Byte  a[6][6] ;
Byte  b[6][6];
void initA()
{  for (int i = 0; i < 6; i++)
    { for (int j = 0; j < 6; j++)
        a[i][j] = 1;
        
    }
}
void printAStar()
{   for (int i = 0; i < 6; i++)
    {   for (int j = 0; j < 6; j++)
        {  if (a[i][j]) cout << "*";
            else cout << " ";
        }
        cout << endl;
    }
}
void printBStar()
{  for (int i = 0; i < 6; i++)
    {  for (int j = 0; j < 6; j++)
        {  if (b[i][j]) cout << "*";
            else cout << " ";
        }
        cout << endl;
    }
}
int main()
{   initA();
    memcpy(b, a, 36);
    printAStar();
    Edge4(a, 6, 6, 1, b);
    printBStar();
    system("pause");
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenCV提供了在Java和C++中进行轮廓提取的方法。 在Java中,可以使用Imgproc.findContours()方法来提取像中的轮廓。该方法需要一个二进制像,其中前景为白色,背景为黑色。轮廓将存储在List<MatOfPoint>对象中。 以下是一个简单的Java示例,演示如何提取轮廓: ```java Mat image = Imgcodecs.imread("input.jpg"); Mat grayImage = new Mat(); Imgproc.cvtColor(image, grayImage, Imgproc.COLOR_BGR2GRAY); Mat binaryImage = new Mat(); Imgproc.threshold(grayImage, binaryImage, 128, 255, Imgproc.THRESH_BINARY); List<MatOfPoint> contours = new ArrayList<>(); Mat hierarchy = new Mat(); Imgproc.findContours(binaryImage, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE); ``` 在C++中,可以使用cv::findContours()函数来提取像中的轮廓。该函数需要一个二进制像,其中前景为白色,背景为黑色。轮廓将存储在std::vector<std::vector<cv::Point>>对象中。 以下是一个简单的C++示例,演示如何提取轮廓: ```cpp cv::Mat image = cv::imread("input.jpg"); cv::Mat grayImage; cv::cvtColor(image, grayImage, cv::COLOR_BGR2GRAY); cv::Mat binaryImage; cv::threshold(grayImage, binaryImage, 128, 255, cv::THRESH_BINARY); std::vector<std::vector<cv::Point>> contours; cv::findContours(binaryImage, contours, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE); ``` 对于轮廓的筛选,可以使用cv::contourArea()函数计算轮廓的面积,并使用cv::boundingRect()函数计算轮廓的边界框。还可以使用cv::approxPolyDP()函数对轮廓进行多边形逼近,以减少顶点数。 以下是一个简单的C++示例,演示如何筛选轮廓: ```cpp std::vector<std::vector<cv::Point>> filteredContours; for (const auto& contour : contours) { double area = cv::contourArea(contour); if (area > 100 && area < 1000) { cv::Rect boundingRect = cv::boundingRect(contour); if (boundingRect.width / boundingRect.height > 0.5 && boundingRect.width / boundingRect.height < 2) { std::vector<cv::Point> approx; cv::approxPolyDP(contour, approx, 5, true); if (approx.size() == 4) { filteredContours.push_back(approx); } } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值