017-Opencv笔记-Sobel算子图像边缘提取

Sobel算子
Scharr->Sobel的改进版

cv::Sobel (
InputArray Src // 输入图像
OutputArray dst// 输出图像,大小与输入图像一致
int depth // 输出图像深度.
Int dx. // X方向,几阶导数
int dy // Y方向,几阶导数.
int ksize, SOBEL算子kernel大小,必须是1、3、5、7、
double scale = 1
double delta = 0
int borderType = BORDER_DEFAULT
)

cv::Scharr (
InputArray Src // 输入图像
OutputArray dst// 输出图像,大小与输入图像一致
int depth // 输出图像深度.
Int dx. // X方向,几阶导数
int dy // Y方向,几阶导数.
double scale = 1
double delta = 0
int borderType = BORDER_DEFAULT
)

GaussianBlur( src, dst, Size(3,3), 0, 0, BORDER_DEFAULT );
cvtColor( src, gray, COLOR_RGB2GRAY );
addWeighted( A, 0.5,B, 0.5, 0, AB);
convertScaleAbs(A, B)// 计算图像A的像素绝对值,输出到图像B

#include "pch.h"
#include 
#include 
#include 

using namespace cv;
int main(int argc, char** argv) {
    Mat src, dst, xygrad;
    src = imread("D:/girl.jpg");
    //resize(src, src, Size(src.cols / 4, src.rows / 4));
    if (!src.data) {
        printf("could not load image...\n");
        return -1;
    }

    char INPUT_TITLE[] = "input image";
    char OUTPUT_TITLE[] = "sobel-demo";
    namedWindow(INPUT_TITLE, 0);
    namedWindow(OUTPUT_TITLE, 0);
    imshow(INPUT_TITLE, src);

    Mat gray_src;
    GaussianBlur(src, dst, Size(3, 3), 0, 0);
    cvtColor(dst, gray_src, CV_BGR2GRAY);
    //imshow("gray image", gray_src);

    Mat xgrad, ygrad;
    Scharr(gray_src, xgrad, CV_16S, 1, 0);
    Scharr(gray_src, ygrad, CV_16S, 0, 1);

    // Sobel(gray_src, xgrad, CV_16S, 1, 0, 3);
    // Sobel(gray_src, ygrad, CV_16S, 0, 1, 3);
    convertScaleAbs(xgrad, xgrad);
    convertScaleAbs(ygrad, ygrad);
    //imshow("xgrad", xgrad);
    //imshow("ygrad", ygrad);

    //模拟addWeighted方法
    //Mat xygrad = Mat(xgrad.size(), xgrad.type());
    //printf("type : %d\n", xgrad.type());
    //int width = xgrad.cols;
    //int height = ygrad.rows;
    //for (int row = 0; row < height; row++) {
    //  for (int col = 0; col < width; col++) {
    //      int xg = xgrad.at(row, col);
    //      int yg = ygrad.at(row, col);
    //      int xy = xg + yg;
    //      xygrad.at(row, col) = saturate_cast(xy);
    //  }
    //}
    addWeighted(xgrad, 0.5, ygrad, 0.5, 0, xygrad);
    imshow(OUTPUT_TITLE, xygrad);

    waitKey(0);
    return 0;
}
#include "pch.h"
#include 
#include 
#include 

using namespace cv;
int main(int argc, char** argv) {
    Mat src, dest, xygrad;
    src = imread("D:/girl.jpg");
    if (!src.data) {
        printf("could not load image...\n");
        return -1;
    }

    char INPUT_TITLE[] = "input image";
    char OUTPUT_TITLE[] = "sobel-demo";
    namedWindow(INPUT_TITLE, 0);
    namedWindow(OUTPUT_TITLE, 0);
    imshow(INPUT_TITLE, src);

    GaussianBlur(src, src, Size(3, 3), 0, 0);
    cvtColor(src, src, CV_BGR2GRAY);

    Mat xgrad, ygrad;
     Sobel(src, xgrad, CV_16S, 1, 0, 3);
     Sobel(src, ygrad, CV_16S, 0, 1, 3);
    convertScaleAbs(xgrad, xgrad);
    convertScaleAbs(ygrad, ygrad);
    addWeighted(xgrad, 0.5, ygrad, 0.5, 0, dest);
    imshow(OUTPUT_TITLE, dest);

    waitKey(0);
    return 0;
}

你可能感兴趣的:(017-Opencv笔记-Sobel算子图像边缘提取)