MerveMeriç
MerveMeriç

Reputation: 73

Convolution in OpenCV C++

I want to take convolution in opencv with cpp my code gives the error that is below

error C2040: 'H' : 'int [1][2]' differs in levels of indirection from 'cv::Mat'

I want to take conv between H= [1,-1] and image || V=[1;-1] and image

#include <iostream>
#include <opencv2\opencv.hpp>

using namespace std;
using namespace cv;

void main(){

    Mat image =imread("C:\\Users\\merve\\Desktop\\images3.jpg",CV_LOAD_IMAGE_COLOR);

    int h,w;



    Size s=image.size();
    h = s.height;
    w = s.width;

    int i,j;
    Mat H_gradient;
    
    int H [1][2] = {1,-1};
    int V [2][1] = {{1},{-1}};


    filter2D( image, H_gradient, -1 , H, Point( -1, -1 ), 0, BORDER_DEFAULT );

    namedWindow( "filter2D Demo", CV_WINDOW_AUTOSIZE );
    imshow( "filter2D Demo", H_gradient );

        
    

    
    waitKey(0);





}

Upvotes: 0

Views: 13591

Answers (1)

marol
marol

Reputation: 4074

I assume you would like to compute horizontal gradient, right? So instead of using two dimensional array of ints as a kernel, use cv::Mat object (from docs: a single-channel floating point matrix) :

cv::Mat kernelH(1, 3, CV_32F);
kernelH.at<float>(0,0) = 1.0f;
kernelH.at<float>(0,1) = 0.0f;
kernelH.at<float>(0,2) = -1.0f;

and the use it instead H variable in your code. Similar, to compute vertical gradient, try following snippet:

cv::Mat kernelH(3, 1, CV_32F);
kernelH.at<float>(0,0) = 1.0f;
kernelH.at<float>(1,0) = 0.0f;
kernelH.at<float>(2,0) = -1.0f;

Also, remember about converting color image to grayscale before applying filter2d.

Upvotes: 3

Related Questions