Edge_style

#include "opencv2/opencv.hpp"  
#include<iostream>  
#include<stdint.h>
using namespace cv;
using namespace std;


void conv2D(InputArray src, InputArray kernel, OutputArray dst, int ddepth, Point anchor = Point(-1, -1), int borderType = BORDER_DEFAULT)
{
    Mat kernelFlip;
    flip(kernel, kernelFlip, -1);
    filter2D(src, dst, ddepth, kernelFlip, anchor, 0.0, borderType);
}


void roberts(InputArray src, OutputArray dst, int ddepth, int x = 1, int y = 0, int borderType = BORDER_DEFAULT)
{
    CV_Assert(!(x == 0 && y == 0));
    Mat roberts_1 = (Mat_<float>(2, 2) << 1, 0, 0, -1);
    Mat roberts_2 = (Mat_<float>(2, 2) << 0, 1, -1, 0);


    if (x != 0 && y == 0)
    {
        conv2D(src, roberts_1, dst, ddepth, Point(0, 0), borderType);
    }
    if (y != 0 && x == 0)
    {
        conv2D(src, roberts_2, dst, ddepth, Point(1, 0), borderType);
    }
}


Mat processframe(Mat& frame)
{
    Mat img_roberts_1;
    roberts(frame, img_roberts_1, CV_32FC1, 1, 0);
    Mat img_roberts_2;
    roberts(frame, img_roberts_2, CV_32FC1, 0, 1);


    Mat abs_img_roberts_1, abs_img_roberts_2;
    convertScaleAbs(img_roberts_1, abs_img_roberts_1, 1, 0);
    convertScaleAbs(img_roberts_2, abs_img_roberts_2, 1, 0);


    Mat img_roberts_1_2, img_roberts_2_2, edge;
    pow(img_roberts_1, 2.0, img_roberts_1_2);
    pow(img_roberts_2, 2.0, img_roberts_2_2);
    sqrt(img_roberts_1_2 + img_roberts_2_2, edge);
    edge.convertTo(edge, CV_8UC1);
    return edge;
}


bool covert_to_edge_style(string src, string dst)
{
    VideoCapture cap(src);// open the default camera  


    if (!cap.isOpened()) // check if we succeeded  
        return -1;


    double rate = cap.get(CV_CAP_PROP_FPS);


    int width = cap.get(CV_CAP_PROP_FRAME_WIDTH);
    int height = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
    Size videoSize(width, height);
    VideoWriter writer(dst, CV_FOURCC('M', 'J', 'P', 'G'), rate, videoSize);


    //namedWindow("src");


    for (; ; )
    {
        Mat frame;
        cap >> frame; // get a new frame from camera 


        if (frame.empty())
            break;


        Mat edge = processframe(frame);
        writer << edge;


        //namedWindow("edge intensity", CV_WINDOW_AUTOSIZE);
        //imshow("edge intensity", edge);


        //imshow("src", frame);
        //if (waitKey(10) >= 0)
        //    break;
    }


    return 0;
}


int main(void)
{
    while (1)
    {
        printf("--------------------------------------------------------------\n");
        printf("please input your video path to be coverted:\n");
        char src[1024];
        fgets(src, 100, stdin);
        if (src[strlen(src) - 1] == '\n')
            src[strlen(src) - 1] = '\0';


        printf("please input output video path:\n");
        char dst[1024];
        fgets(dst, 100, stdin);
        if (dst[strlen(dst) - 1] == '\n')
            dst[strlen(dst) - 1] = '\0';


        covert_to_edge_style(src, dst);


        printf("covertion finished!! \n");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值