Reputation: 23025
I am working on the 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 is unable to reae" << endl;
}
Sharpen(image,result);
waitKey(0);
}
void Sharpen(Mat &image,Mat result)
{
//result = image.clone();
result.create(image.size(), image.type());
//For all rows except first and 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 all columns except first and 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, what I an trying to do is, trying to sharpen an image. Following is the formula for image sharpening.
resultPixel = 5*currentPixel-previousPixel-nextPixel-upperPixel-belowPixel
Anyway, following is the output I get
As you can see, I am not getting the expected result. What am doing wrong?
Upvotes: 2
Views: 4092
Reputation: 2985
Your first issue is that there are 3 bytes per pixel so you are only iterating over a third of your image. Your other issue is that you cannot do *output++ as you want to skip the first pixel. Putting those two things together results in the following code which gives the result you are 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 all rows except first and 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 all columns except first and 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 is unable to reae" << endl;
}
Sharpen(image,result);
waitKey(0);
}
Upvotes: 3