2013-04-20 2 views
2

Пожалуйста, посмотрите на следующий кодрезкости изображения - Доступ к соседним пикселам

#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); 
} 

В здесь, что я пытается сделать это, пытается заострить изображение. Ниже приведена формула для заточки изображения.

resultPixel = 5*currentPixel-previousPixel-nextPixel-upperPixel-belowPixel 

Во всяком случае, после это выход я получаю

enter image description here

Как вы можете видеть, я не получаю ожидаемого результата. Что я делаю неправильно? Пожалуйста помоги!

ответ

3

Ваша первая проблема заключается в том, что есть 3 байта на пиксель, поэтому вы только итерируете более трети вашего изображения. Другая проблема заключается в том, что вы не можете делать * output ++, поскольку хотите пропустить первый пиксель. Объединение этих двух элементов приводит к следующему коду, который дает результат, который вы ищете.

#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); 
} 
+0

Удивительный! Спасибо! Я предполагаю, что мой способ подходит только для изображений с одним каналом (Gray Scale), не так ли? –

Смежные вопросы