Opencv系列9_opencv区域增长算法分割JPG图像并显示保存

 实例9:opencv区域增长算法分割JPG图像并显示保存

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include <iostream>
#include "math.h"

using namespace cv;
using namespace std;

Point recent_Point, recent_Point1;

Mat RegionGrow(Mat src, Point2i pt, int th)
{
    Point2i ptGrowing;                        //待生长点位置
    int nGrowLable = 0;                                //标记是否生长过
    int nSrcValue = 0;                                //生长起点灰度值
    int nCurValue = 0;                                //当前生长点灰度值
    Mat matDst = Mat::zeros(src.size(), CV_8UC1);    //创建一个空白区域,填充为黑色
    //生长方向顺序数据
    int DIR[8][2] = { { -1, -1 }, { 0, -1 }, { 1, -1 }, { 1, 0 }, { 1, 1 }, { 0, 1 }, { -1, 1 }, { -1, 0 } };
    vector<Point2i> vcGrowPt;                        //生长点栈
    vcGrowPt.push_back(pt);                            //将生长点压入栈中
    matDst.at<uchar>(pt.y, pt.x) = 255;                //标记生长点
    nSrcValue = src.at<uchar>(pt.y, pt.x);            //记录生长点的灰度值

    while (!vcGrowPt.empty())                        //生长栈不为空则生长
    {
        pt = vcGrowPt.back();                        //取出一个生长点
        vcGrowPt.pop_back();

        //分别对八个方向上的点进行生长
        for (int i = 0; i < 9; ++i)
        {
            ptGrowing.x = pt.x + DIR[i][0];
            ptGrowing.y = pt.y + DIR[i][1];
            //检查是否是边缘点
            if (ptGrowing.x < 0 || ptGrowing.y < 0 || ptGrowing.x >(src.cols - 1) || (ptGrowing.y > src.rows - 1))
                continue;

            nGrowLable = matDst.at<uchar>(ptGrowing.y, ptGrowing.x);        //当前待生长点的灰度值

            if (nGrowLable == 0)                    //如果标记点还没有被生长
            {
                nCurValue = src.at<uchar>(ptGrowing.y, ptGrowing.x);
                if (abs(nSrcValue - nCurValue) < th)                    //在阈值范围内则生长
                {
                    matDst.at<uchar>(ptGrowing.y, ptGrowing.x) = 255;        //标记为白色
                    vcGrowPt.push_back(ptGrowing);                    //将下一个生长点压入栈中
                }
            }
        }
    }
    return matDst.clone();
}

void On_mouse(int event, int x, int y, int flags, void*)//每次点击左键,在相应位置画红点
{
    if (event == EVENT_LBUTTONDOWN)
    {
        recent_Point = Point(x, y);
        cout << "img_x" << " " << recent_Point.x << " " << "img_y" << " " << recent_Point.y << endl;
        //circle(srcimg, recent_Point, 2, Scalar(0, 0, 255), -1);
        //imshow("srcImg", srcimg);
    }
}

int main() //区域生长
{
    Mat binaryimg, greyimg;
    Mat regiongrow, regiongrow1, regiongrow2;
    Mat dst;
    int th = 10;
    Mat src = imread("1.jpg");
    cvtColor(src, greyimg, COLOR_BGR2GRAY);   //转化为灰度图
    Mat temp_regiongrow = Mat::zeros(src.size(), CV_8UC1);    //创建一个空白区域,填充为黑色
    //转化为二值图
    threshold(greyimg, binaryimg, 200, 255, THRESH_BINARY);
    namedWindow("srcImg", 0);
    imshow("srcImg", src);

    namedWindow("binaryImg", 0);
    imshow("binaryImg", binaryimg);
    cout << "select one point in binaryImg" << endl;
    setMouseCallback("binaryImg", On_mouse);

    for (int i = 0;i < 1;i++) {
        char c = (char)waitKey(0);
        cout << "select one point in binaryImg" << endl;
        setMouseCallback("binaryImg", On_mouse);
        if (c == 'b') {
            regiongrow1 = RegionGrow(binaryimg, recent_Point, th);
            bitwise_or(regiongrow1, temp_regiongrow, regiongrow);    //和前一个分割的图做或运算
            temp_regiongrow = regiongrow1.clone();    //保存前一个分割图
        }
        bitwise_and(greyimg, regiongrow, dst);   //与原图做与运算
        namedWindow("dstimg", 0);
        imshow("dstimg", dst);
        imwrite("region_growing.jpg", dst);
    }
    waitKey(0);
    return 0;
}

在binaryImag图像中选择区域增长的初始种子点(鼠标左键一下红色区域选取):

     

出现位置坐标后,键盘输入字母‘b'进行区域增长算法运行,dstimg为区域增长分割后图像:

  

本例程配套素材见源码整理文章下载(点击进入)

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亦我飞也

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值