Output two distinct windows

asked 2017-11-01 05:02:56 -0500

TheHives gravatar image

Hi, I want to use the Sobel Derivatives with two pictures (https://docs.opencv.org/2.4/doc/tutor...). I tried the following code but it just output the first arguments and the second windows never shows up. In this code, I just call twice a function from the quoted tutoriel twice with the two argv arguments.

What can I do ? Thanks you

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>


using namespace cv;

void Showedges( char** argv, int index);

/** @function main */
int main(int argc, char** argv)
{
Showedges(argv, 1);
Showedges(argv, 2);
return 0;
}

void Showedges(char** argv, int index)

{

Mat src, src_gray;
Mat grad;
char* window_name = argv[index];
int scale = 1;
int delta = 0;
int ddepth = CV_16S;

int c;

/// Load an image
src = imread(argv[index]);

if (!src.data)
{
    return;
}

GaussianBlur(src, src, Size(3, 3), 0, 0, BORDER_DEFAULT);

/// Convert it to gray
cvtColor(src, src_gray, CV_BGR2GRAY);

/// Create window
namedWindow(window_name, CV_WINDOW_AUTOSIZE);

/// Generate grad_x and grad_y
Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;

/// Gradient X
//Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
Sobel(src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT);
convertScaleAbs(grad_x, abs_grad_x);

/// Gradient Y
//Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
Sobel(src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT);
convertScaleAbs(grad_y, abs_grad_y);

/// Total Gradient (approximate)
addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad);




imshow(window_name, grad);

waitKey(0);

return;

}

edit retag flag offensive close merge delete

Comments

It seems that I have to create the windows before calling showedges. I do not undertand why because I do it within the function showedges...

int main(int argc, char** argv)
{
    namedWindow(argv[1]);
    namedWindow(argv[2]);


    Showedges(argv, 1);
    Showedges(argv, 2);

    printf("%d", argc);
    waitKey(0);
    return 0;
}
TheHives gravatar imageTheHives ( 2017-11-01 05:32:15 -0500 )edit

You have specified a waitkey(0) at the end of the showedge method which waits infinite time period, Once you press some key over the first image window, the second call of showedge will take place and the other window will get displayed.

John_OpenCVdev gravatar imageJohn_OpenCVdev ( 2017-11-01 06:48:41 -0500 )edit

Get it, thank you a lot

TheHives gravatar imageTheHives ( 2017-11-01 08:31:28 -0500 )edit