I have implemented the following code to perform filter2D on a matrix. After I compiled the program it returns Segmentation fault error. In this program, I want to assign an input array in the program (don't want to load images). then perform a filter which is assigned inside the program to measure the time of the filter2D function for different matrices and kernels at runtime.
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace std;
using namespace cv;
int main(){
//Picture size for input and output are the same
int Pxsize = 128;
int Pysize = Pxsize;
//Kernel size
int Kxsize = 3;
int Kysize = Kxsize;
//filter arguments
Point anchor;
double delta;
int ddepth;
//name for out put
char window_name[32] = "filter2D Demo";
Mat input[128][128];
Mat output[128][128];
Mat kernel[3][3];
// Initialize arguments for the filter
anchor = Point( -1, -1 );
delta = 0;
ddepth = -1;
int i,j;
//assign data between 0 and 255 to the input matrix
for (i=0; i<Pxsize; i++)
for (j=0; j<Pysize; j++)
input[i][j]=(i*j)%255;
//assign data to the kernel
//assign data between 0 and 255 to the input matrix
for (i=0; i<Kxsize; i++)
for (j=0; j<Kysize; j++)
kernel[i][j]=1;
//the problem is here:
filter2D((InputArray) input, (OutputArray) output, ddepth , ( InputArray) kernel, anchor, delta, BORDER_DEFAULT );
namedWindow( window_name, CV_WINDOW_AUTOSIZE );
imshow( window_name, (OutputArray) output );
return 0;
}
the output is:
Segmentation fault
------------------
(program exited with code: 139)
Press return to continue
I used this command for gcc 6.2.0
compiler in Linux mint
g++ -Wall -Wextra -Werror -pedantic -I/usr/local/include/opencv -I/usr/local/include/opencv2 -L/usr/local/lib/ -g -o "opencv" "opencv.cpp" -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_stitching
Compilation finished successfully
Thanks in advance