Ask Your Question
-1

gradient filter

asked 2016-07-05 11:29:03 -0600

HiHello gravatar image

updated 2016-07-05 12:54:09 -0600

Eduardo gravatar image

i am try to make gradient filter which is

intfilter[filterHeight][filterWidth]{
    -1,-1, -1,
    -1, 0,  1,
     1, 1,  1
};

and the code is like below.

image description

Mat gradient(Matdst, IplImage *img) {
Matnew_dst(256, 256, DataType<int>::type);
uchar*data;
data= (uchar*)img->imageData;

intheight, width, i, j;
height= img->height;
width= img->width;
IplImage*imgavg = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);

if(img != 0){
cvSmooth(img, imgavg, CV_BLUR, 1);//3*3의 이웃 픽셀값의 평균
cvNamedWindow("AvgFilter", CV_WINDOW_AUTOSIZE);
cvShowImage("AvgFilter", imgavg);
}


for(i = 2; i < width-2; i++) {
for(j = 2; j < height-2; j++) {
new_dst.at<int>(i,j) = {(dst.at<int>(i-1, j-1))*(filter[0][0])+(dst.at<int>(i-1,j))*(filter[0][1])+ (dst.at<int>(i-1, j+1))*(filter[0][2])
+(dst.at<int>(i , j-1))*(filter[1][0]) + (dst.at<int>(i,j))*(filter[1][1]) + (dst.at<int>(i, j+1))*(filter[1][2])
+(dst.at<int>(i+1, j-1))*(filter[2][0]) + (dst.at<int>(i+1,j))*(filter[2][1]) + (dst.at<int>(i+1, j+1))*(filter[2][2])};
}//forj (height)
}//fori (width)

IplImage*imgn;
imgn= cvCreateImage(cvSize(512, 512), IPL_DEPTH_8U, 1);
imgn->imageData= (char *)new_dst.data;
cvShowImage("Hooooooo",imgn);
returnnew_dst;
}

so i got the result like this. the result of gradient filter that i made is the first pic.

image description

i havent any idea that why the result comes out like this.

Here is my question. If you have any idea about that filter, please let me know.

Also I you can please advice me that what is wrong about my code.

Also, if there is any route that i can see the filter library which is served by opencv please let me know.

Thanks !

edit retag flag offensive close merge delete

Comments

2
  • sorry for the downvote, but all of your code is outdated, and is an "anti-pattern"
  • please stay away from anything starting wit cv*, IplImage*, etc- (that's the rotten c-api, it is dead since half a decade.)

  • please stay away from "per-pixel loops" (that's not the way we roll)

berak gravatar imageberak ( 2016-07-05 11:32:44 -0600 )edit

thanks for your advice:)

HiHello gravatar imageHiHello ( 2016-07-05 12:42:53 -0600 )edit

1 answer

Sort by » oldest newest most voted
2

answered 2016-07-05 11:37:42 -0600

berak gravatar image

updated 2016-07-05 12:24:37 -0600

in a more "modern" world, you'd do:

cv::Mat_<float> kernel(3,3);
kernel << -1, -1, -1,
          -1, 0, 1,
           1, 1, 1;
..
Mat src = ... // your input.
Mat dst;      // output. intentionally left blank.
filter2D(src, dst, -1, kernel);
edit flag offensive delete link more

Comments

thanks! also thanks for your advice.

HiHello gravatar imageHiHello ( 2016-07-05 12:40:36 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-07-05 11:29:03 -0600

Seen: 389 times

Last updated: Jul 05 '16