我是想要自己实现一下Laplacian的功能,但是代码中的105行一直显示指针越界,检查好久没有发现原因。。
1 #include <iostream> 2 #include <highgui/highgui.hpp> 3 #include <imgproc/imgproc.hpp> 4 #include <core/core.hpp> 5 6 using namespace std; 7 using namespace cv; 8 9 void maskoperation(int* table,int* arr,int l); 10 11 //******************************// 12 //function: Sharpening Spatial Filter 13 //German_Iris@outlook.com 14 //******************************// 15 int main() 16 { 17 //load the Original Image and get some informations 18 Mat src = imread("012.jpg",0); 19 namedWindow("OriginalImage"); 20 imshow("OriginalImage",src); 21 CV_Assert(src.depth() == CV_8U); 41 42 //Own Laplacian Operation 43 //get some informations of original image 44 int nr = src.rows; 45 int nc = src.cols; 46 int n = nr*nc; 47 int arr[9] = {0}; 48 49 //scan the whole pixels of original image 50 int* table = new int[n]; 51 int l; 52 for (int i=0;i<n;i++) 53 { 54 table[i] = 0; 55 } 56 for (int i=1;i<nr-1;i++) 57 { 58 const uchar* previous = src.ptr<uchar>(i-1); 59 const uchar* current = src.ptr<uchar>(i); 60 const uchar* next = src.ptr<uchar>(i+1); 61 for (int j=1;j<nc-1;j++) 62 { 63 for (int k=0;k<3;k++) 64 { 65 arr[k] = previous[j+k-1]; 66 arr[k+3] = current[j+k-1]; 67 arr[k+6] = next[j+k-1]; 68 } 69 l = nr*(i-1)+j; //calculate the location in the table of current pixel 70 maskoperation(table,arr,l); 71 } 72 } 73 //pixels scale 74 int min = table[0]; 75 int max = table[0]; 76 uchar* table_scale = new uchar[n]; 77 for (int i=0;i<n;i++) 78 { 79 if(min>table[i]) 80 { 81 min = table[i]; 82 } 83 if(max<table[i]) 84 { 85 max = table[i]; 86 } 87 } 88 for (int i=0;i<n;i++) 89 { 90 table_scale[i] = (uchar)((table[i]-min)/(max-min)); 91 //table_scale[i] = (table[i]-min)/(max-min); 92 } 93 94 //padding values 95 Mat LaResult_own; 96 LaResult_own.create(src.size(),src.type()); 97 uchar* p = NULL; 98 for (int i=0;i<nr;i++) 99 { 100 p = LaResult_own.ptr<uchar>(i); 101 for (int j=0;j<nc;j++) 102 { 103 l = nr*(i-1)+j; 104 //???? 105 p[j] = table_scale[l]; 106 } 107 } 108 109 //show results 110 namedWindow("LaResult_own"); 111 imshow("LaResult_own",LaResult_own); 112 113 waitKey(); 114 } 115 116 //mask operation 117 void maskoperation(int* table,int* arr,int l) 118 { 119 int tmp[9] = {-1,-1,-1,-1,8,-1,-1,-1,-1}; 120 for (int i=0;i<9;i++) 121 { 122 table[l] = table[l] + tmp[i]*arr[i]; 123 } 124 }
l = nr*(i-1)+j; //把i=0,j < nr的情况代入进去算一算
越界不仅仅可以后向,也可以前向
多谢,正如你说的那样的,就是单纯的指针越界,我已经找到原因了,怪我粗心大意了