c++ - Sharpening an Image - Accessing neighbor pixels -
please have @ following code
#include <iostream> #include <opencv2\core\core.hpp> #include <opencv2\highgui\highgui.hpp> using namespace std; using namespace cv; void sharpen(mat &,mat); int main() { mat image,result; try { image = imread("c:/users/public/pictures/sample pictures/desert.jpg"); if(!image.data) { throw 1; } } catch(int i) { cout << "image unable reae" << endl; } sharpen(image,result); waitkey(0); } void sharpen(mat &image,mat result) { //result = image.clone(); result.create(image.size(), image.type()); //for rows except first , last for(int i=1;i<image.rows-1;i++) { const uchar *previous = image.ptr<const uchar>(i-1); const uchar *next = image.ptr<const uchar>(i+1); const uchar *current = image.ptr<const uchar>(i); uchar *output = result.ptr<uchar>(i); //for columns except first , last for(int a=1;a<image.cols-1;a++) { *output++ = cv::saturate_cast<uchar>(5*current[a]-current[a-1]-current[a+1]-previous[a]-next[a]); } } result.row(0).setto(cv::scalar(0)); result.row(result.rows-1).setto(cv::scalar(0)); result.col(0).setto(cv::scalar(0)); result.col(result.cols-1).setto(cv::scalar(0)); namedwindow("original"); imshow("original",image); namedwindow("duplicate"); imshow("duplicate",result); }
in here, trying is, trying sharpen image. following formula image sharpening.
resultpixel = 5*currentpixel-previouspixel-nextpixel-upperpixel-belowpixel
anyway, following output get
as can see, not getting expected result. doing wrong? please help!
your first issue there 3 bytes per pixel iterating on third of image. other issue cannot *output++ want skip first pixel. putting 2 things results in following code gives result looking for.
#include <iostream> #include <opencv2\core\core.hpp> #include <opencv2\highgui\highgui.hpp> using namespace std; using namespace cv; void sharpen(mat &image,mat& result) { result.create(image.size(), image.type()); //for rows except first , last for(int i=1;i<image.rows-1;i++) { const uchar *previous = image.ptr<const uchar>(i-1); const uchar *next = image.ptr<const uchar>(i+1); const uchar *current = image.ptr<const uchar>(i); uchar *output = result.ptr<uchar>(i); //for columns except first , last for(int a=3;a<(image.cols-1)*3;a++) { output[a] = cv::saturate_cast<uchar>(5*current[a]-current[a-1]-current[a+1]-previous[a]-next[a]); } } result.row(0).setto(cv::scalar(0)); result.row(result.rows-1).setto(cv::scalar(0)); result.col(0).setto(cv::scalar(0)); result.col(result.cols-1).setto(cv::scalar(0)); namedwindow("original"); imshow("original",image); namedwindow("duplicate"); imshow("duplicate",result); } int main() { mat image,result; try { image = imread("c:/users/public/pictures/sample pictures/desert.jpg"); if(!image.data) { throw 1; } } catch(int i) { cout << "image unable reae" << endl; } sharpen(image,result); waitkey(0); }
Comments
Post a Comment