区域分割(OpenCV)

区域分割

区域生长

分裂合并

水域分割

Hough变换

Hough变换原理

广义Hough变换

#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace std;
using namespace cv;

Mat g_srcImage, g_dstImage, g_midImage;//原始图、中间图和效果图
vector<Vec4i> g_lines;//定义一个矢量结构g_lines用于存放得到的线段矢量集合
//变量接收的TrackBar位置参数
int g_nthreshold = 100;

static void on_HoughLines(int, void*);//回调函数
static void ShowHelpText();

int main()
{
    ShowHelpText();
    Mat g_srcImage = imread("1.jpg");  //工程目录下应该有一张名为1.jpg的素材图
    imshow("【原始图】", g_srcImage);

    namedWindow("【效果图】", 1);
    createTrackbar("", "【效果图】", &g_nthreshold, 200, on_HoughLines);

    Canny(g_srcImage, g_midImage, 50, 200, 3);//进行一次canny边缘检测
    cvtColor(g_midImage, g_dstImage, COLOR_GRAY2BGR);//转化边缘检测后的图为灰度图

    on_HoughLines(g_nthreshold, 0);
    HoughLinesP(g_midImage, g_lines, 1, CV_PI / 180, 80, 50, 10);

    imshow("【效果图】", g_dstImage);
    waitKey(0);
    return 0;
}

static void on_HoughLines(int, void*)
{
    Mat dstImage = g_dstImage.clone();
    Mat midImage = g_midImage.clone();

    vector<Vec4i> mylines;
    HoughLinesP(midImage, mylines, 1, CV_PI / 180, g_nthreshold + 1, 50, 10);
    for (size_t i = 0; i < mylines.size(); i++)
    {
        Vec4i l = mylines[i];
        line(dstImage, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(23, 180, 55), 1, LINE_AA);
    }
    imshow("【效果图】", dstImage);
}

static void ShowHelpText()
{
    printf("当前使用的OpenCV版本为:" CV_VERSION);
    printf("\n请调整滚动条观察图像效果!");


}
Hough

 

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

using namespace std;
using namespace cv;

#define WINDOW_NAME "【程序窗口】"        //为窗口标题定义的宏 

Mat g_srcImage, g_dstImage;
Mat g_map_x, g_map_y;

int update_map(int key);
static void ShowHelpText();//输出帮助文字

int main(int argc, char** argv)
{
    
    ShowHelpText();

    g_srcImage = imread("1.jpg", 1);
    if (!g_srcImage.data) { printf("读取图片错误,请确定目录下是否有imread函数指定的图片存在~! \n"); return false; }
    imshow("原始图", g_srcImage);

    g_dstImage.create(g_srcImage.size(), g_srcImage.type());
    g_map_x.create(g_srcImage.size(), CV_32FC1);
    g_map_y.create(g_srcImage.size(), CV_32FC1);

    namedWindow(WINDOW_NAME, WINDOW_AUTOSIZE);
    imshow(WINDOW_NAME, g_srcImage);

    while (1)
    {
        //获取键盘按键  
        int key = waitKey(0);
        //判断ESC是否按下,若按下便退出  
        if ((key & 255) == 27)
        {
            cout << "程序退出...........\n";
            break;
        }

        //根据按下的键盘按键来更新 map_x & map_y的值. 然后调用remap( )进行重映射
        update_map(key);
        //此句代码的OpenCV2版为:
        //remap( g_srcImage, g_dstImage, g_map_x, g_map_y, CV_INTER_LINEAR, BORDER_CONSTANT, Scalar(0,0, 0) );
        //此句代码的OpenCV3版为:
        remap(g_srcImage, g_dstImage, g_map_x, g_map_y, INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0));

        //显示效果图
        imshow(WINDOW_NAME, g_dstImage);
    }
    return 0;
}

int update_map(int key)
{
    //双层循环,遍历每一个像素点
    for (int j = 0; j < g_srcImage.rows; j++)
    {
        for (int i = 0; i < g_srcImage.cols; i++)
        {
            switch (key)
            {
            case '1': // 键盘【1】键按下,进行第一种重映射操作
                if (i > g_srcImage.cols * 0.25 && i < g_srcImage.cols * 0.75 && j > g_srcImage.rows * 0.25 && j < g_srcImage.rows * 0.75)
                {
                    g_map_x.at<float>(j, i) = static_cast<float>(2 * (i - g_srcImage.cols * 0.25) + 0.5);
                    g_map_y.at<float>(j, i) = static_cast<float>(2 * (j - g_srcImage.rows * 0.25) + 0.5);
                }
                else
                {
                    g_map_x.at<float>(j, i) = 0;
                    g_map_y.at<float>(j, i) = 0;
                }
                break;
            case '2':// 键盘【2】键按下,进行第二种重映射操作
                g_map_x.at<float>(j, i) = static_cast<float>(i);
                g_map_y.at<float>(j, i) = static_cast<float>(g_srcImage.rows - j);
                break;
            case '3':// 键盘【3】键按下,进行第三种重映射操作
                g_map_x.at<float>(j, i) = static_cast<float>(g_srcImage.cols - i);
                g_map_y.at<float>(j, i) = static_cast<float>(j);
                break;
            case '4':// 键盘【4】键按下,进行第四种重映射操作
                g_map_x.at<float>(j, i) = static_cast<float>(g_srcImage.cols - i);
                g_map_y.at<float>(j, i) = static_cast<float>(g_srcImage.rows - j);
                break;
            }
        }
    }
    return 1;
}

static void ShowHelpText()
{

    printf("当前使用的OpenCV版本为:" CV_VERSION);
    printf("\n欢迎来到重映射示例程序:");
    printf("\n按键操作说明: \n"
        "键盘按键【ESC】- 退出程序\n"
        "键盘按键【1】-  第一种映射方式\n"
        "键盘按键【2】- 第二种映射方式\n"
        "键盘按键【3】- 第三种映射方式\n"
        "键盘按键【4】- 第四种映射方式\n");
}
remap

 

#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;

#define WINDOW_NAME1 "【原始图窗口】"                    //为窗口标题定义的宏 
#define WINDOW_NAME2 "【经过Warp后的图像】"        //为窗口标题定义的宏 
#define WINDOW_NAME3 "【经过Warp和Rotate后的图像】"        //为窗口标题定义的宏 

static void ShowHelpText();

int main()
{
    ShowHelpText();

    //定义两组点,代表两个三角形
    Point2f srcTriangle[3];
    Point2f dstTriangle[3];
    //定义一些Mat变量
    Mat rotMat(2, 3, CV_32FC1);
    Mat warpMat(2, 3, CV_32FC1);
    Mat srcImage, dstImage_warp, dstImage_warp_rotate;

    srcImage = imread("1.jpg", 1);
    if (!srcImage.data) { printf("读取图片错误,请确定目录下是否有imread函数指定的图片存在~! \n"); return false; }
    // 设置目标图像的大小和类型与源图像一致
    dstImage_warp = Mat::zeros(srcImage.rows, srcImage.cols, srcImage.type());

    srcTriangle[0] = Point2f(0, 0);
    srcTriangle[1] = Point2f(static_cast<float>(srcImage.cols - 1), 0);
    srcTriangle[2] = Point2f(0, static_cast<float>(srcImage.rows - 1));

    dstTriangle[0] = Point2f(static_cast<float>(srcImage.cols * 0.0), static_cast<float>(srcImage.rows * 0.33));
    dstTriangle[1] = Point2f(static_cast<float>(srcImage.cols * 0.65), static_cast<float>(srcImage.rows * 0.35));
    dstTriangle[2] = Point2f(static_cast<float>(srcImage.cols * 0.15), static_cast<float>(srcImage.rows * 0.6));

    warpMat = getAffineTransform(srcTriangle, dstTriangle);

    warpAffine(srcImage, dstImage_warp, warpMat, dstImage_warp.size());

    // 计算绕图像中点顺时针旋转50度缩放因子为0.6的旋转矩阵
    Point center = Point(dstImage_warp.cols / 2, dstImage_warp.rows / 2);
    double angle = -50.0;
    double scale = 0.6;

    rotMat = getRotationMatrix2D(center, angle, scale);
    warpAffine(dstImage_warp, dstImage_warp_rotate, rotMat, dstImage_warp.size());

    imshow(WINDOW_NAME1, srcImage);
    imshow(WINDOW_NAME2, dstImage_warp);
    imshow(WINDOW_NAME3, dstImage_warp_rotate);

    waitKey(0);
    return 0;
}

static void ShowHelpText()
{
    printf("当前使用的OpenCV版本为:" CV_VERSION);
    printf("\n欢迎来到仿射变换综合示例程序!");
}
Affine_Transform

 -----------------------------------continue----------------------------------

posted @ 2020-10-30 18:12  望星草  阅读(981)  评论(0编辑  收藏  举报