Szumy

Szum typu Gaussowskiego 

Dodajmy do obrazu szum typu Gaussowskiego za pomocą OpenCV. Będą nam przydatne trzy funkcje:

  1. randn(noise_image, Scalar::all(average), Scalar::all(standard_deviation)); 

    Fills the array with normally distributed random numbers.

    C++: void randn(InputOutputArray dst, InputArray mean, InputArray stddev)
    Parameters:
    dst
     – output array of random numbers; the array must be pre-allocated and have 1 to 4 channel
    mean – mean value (expectation) of the generated random numbers.
    stddev – standard deviation of the generated random numbers; it can be either a vector (in which case a diagonal standard deviation matrix is assumed) or a square matrix

    The function randn fills the matrix dst with normally distributed random numbers with the specified mean vector and the standard deviation matrix. The generated random numbers are clipped to fit the value range of the output array data type

  2. image.convertTo(temp_image,CV_16SC3); 

    Converts an array to another data type with optional scaling.

    C++: void Mat::convertTo(OutputArray m, int rtype, double alpha=1, double beta=0 ) const
    Parameters:
    m
     – output matrix; if it does not have a proper size or type before the operation, it is reallocated.
    rtype – desired output matrix type or, rather, the depth since the number of channels are the same as the input has; if rtype is negative, the output matrix will have the same type as the input.
    alpha – optional scale factor.
    beta – optional delta added to the scaled values.

  3. addWeighted(temp_image, 1.0, noise_image, 1.0, 0.0, temp_image); 

    Calculates the weighted sum of two arrays.

    C++: void addWeighted(InputArray src1, double alpha, InputArray src2, double beta, double gamma, OutputArray dst, intdtype=-1)
    Parameters:
    src1
     – first input array.
    alpha – weight of the first array elements.
    src2 – second input array of the same size and channel number as src1.
    beta – weight of the second array elements.
    dst – output array that has the same size and number of channels as the input arrays.
    gamma – scalar added to each sum.
    dtype – optional depth of the output array; when both input arrays have the same depth, dtype can be set to -1, which will be equivalent to src1.depth().

Więcej informacji o powyższych funkcjach można znaleźć w dokumentacji biblioteki OpenCV.

#include <cstdlib>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;

int main(){
  Mat img = imread("./LENA_512.jpg", 1 );
  double average=0.0;
  double standard_deviation=100.0;
  Mat noise_img(img.size(), CV_16SC3);    
  randn(noise_img, Scalar::all(average), Scalar::all(standard_deviation));
  Mat temp_img;
  img.convertTo(temp_img,CV_16SC3);
  addWeighted(temp_img, 1.0, noise_img, 1.0, 0.0, temp_img);
  temp_img.convertTo(img,img.type());   
  namedWindow( "Noise", WINDOW_AUTOSIZE );
  imshow( "Noise", img );
  waitKey(); 
  return 0; 
} 

Szum jednostajny 

Do wygenerowania szumu jednostajnego w OpenCV można użyć funkcji :

  • randu(noise_img, Scalar::all(low), Scalar::all(high)); -
    Generates a single uniformly-distributed random number or an array of random numbers.
    C++: void randu(InputOutputArray dst, InputArray low, InputArray high)
    Parameters:
    dst – output array of random numbers; the array must be pre-allocated.
    low – inclusive lower boundary of the generated random numbers.
    high – exclusive upper boundary of the generated random numbers.

    The template functions randu generate and return the next uniformly-distributed random value of the specified type. randu<int>() is an equivalent to (int)theRNG(); , and so on. 

#include <cstdlib>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;
int main(){
  Mat img = imread("./LENA_512.jpg", 1 );
  double low=0.0;
  double high=100.0;
  Mat noise_img(img.size(), CV_16SC3);    
  randu(noise_img, Scalar::all(low), Scalar::all(high));
  Mat temp_img;
  img.convertTo(temp_img,CV_16SC3);
  addWeighted(temp_img, 1.0, noise_img, 1.0, 0.0, temp_img);
  temp_img.convertTo(img,img.type());   
  namedWindow( "Noise", WINDOW_AUTOSIZE );
  imshow( "Noise", img );
  waitKey(); 
  return 0; 
}     

Szum typu Pieprz i Sól 

#include <cstdlib>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;

int main(){
  Mat img = imread("./LENA_512.jpg", 1 );
//cvtColor(img, img, CV_BGR2GRAY);
  double noise_percentage = 10.0;
  int img_rows = img.rows;
  int img_columns = img.cols;
  int img_channels = img.channels();
  int noise_points = (int) (((double) img_rows* img_columns*img_channels)*noise_percentage/100.0);
  
  for (int count = 0; count < noise_points; count++){
    int row = rand() % img_rows;
    int column = rand() % img_columns;
    int channel = rand() % img_channels;
    uchar* pixel = img.ptr(row) + (column*img_channels) + channel;
    *pixel = (rand()%2 == 1) ? 255 : 0;
  }
    
  namedWindow( "Noise", WINDOW_AUTOSIZE );
  imshow( "Noise", img );  
  waitKey(); 
  return 0; 
}      

Lokalne rozmycie 

#include <cstdlib>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;

int main(){
  Mat img = imread("/home/przemek/Pulpit/openCVExamples/LENA_512.jpg", 1 );
  // cvtColor(img, img, CV_BGR2GRAY);
  for(int i = 1 ; i < 10; i ++){
    Mat smoothed_img = img.clone();
    blur(img,smoothed_img,Size(3*i,3*i));
  }
  waitKey(0);
  return 0;
}

Lokalne rozmycie 

#include <cstdlib>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;

int main(){
  Mat img = imread("./LENA_gaussian.jpg", 1 );
  // cvtColor(img, img, CV_BGR2GRAY);
  Mat smoothed_img = img.clone();
  blur(img,smoothed_img,Size(5,5));
  namedWindow( "Gaussian", WINDOW_AUTOSIZE );
  imshow( "Gaussian", smoothed_img );
  
  img = imread("./LENA_uniform.jpg", 1 );
  smoothed_img = img.clone();
  blur(img,smoothed_img,Size(5,5));
  namedWindow( "Uniform", WINDOW_AUTOSIZE );
  imshow( "Uniform", smoothed_img );
  
  img = imread("./LENA_RGB_salt_and_pepper.jpg", 1 );
  smoothed_img = img.clone();
  blur(img,smoothed_img,Size(5,5));
  namedWindow( "Salt and Pepper", WINDOW_AUTOSIZE );
  imshow( "Salt and Pepper", smoothed_img );
  
  waitKey(0);
  return 0;
}

Lokalne rozmycie 

#include <cstdlib>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;

/// Global Variables
Mat img; 
int bins = 1;
/// Function Headers
void blur(int, void* );
 
int main(){
  /// Read the image
  img = imread( "./LENA_512.jpg", 1 );
  // cvtColor(img, img, CV_BGR2GRAY);
 
  /// Create Trackbar to enter the number of bins
  char* window_image = "Source image";
  namedWindow( window_image, CV_WINDOW_AUTOSIZE );
  createTrackbar("* Blur: ", window_image, &bins, 100, blur );
  blur(1, 0);
 
  /// Show the image
  imshow( window_image, img );
 
  /// Wait until user exits the program
  waitKey(0);
  return 0;
}
 
void blur(int, void* ){
  Mat smoothed_img;
  blur(img,smoothed_img,Size(2*bins+1,2*bins+1));
  imshow( "Blur", smoothed_img );
}

Filtr medianowy 

#include <cstdlib>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;

int main(){
  Mat img = imread("/home/przemek/Pulpit/openCVExamples/LENA_512.jpg", 1 );
  // cvtColor(img, img, CV_BGR2GRAY);
  for(int i = 1 ; i < 10; i ++){
    Mat smoothed_img = img.clone();
    medianBlur(img,smoothed_img,2*i+1);
  }
  waitKey(0);
  return 0;
}

Filtr medianowy 

#include <cstdlib>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;

int main(){
  Mat img = imread("./LENA_gaussian.jpg", 1 );
  // cvtColor(img, img, CV_BGR2GRAY);
  Mat smoothed_img = img.clone();
  medianBlur(img,smoothed_img,5);
  namedWindow( "Gaussian", WINDOW_AUTOSIZE );
  imshow( "Gaussian", smoothed_img );
  
  img = imread("./LENA_uniform.jpg", 1 );
  smoothed_img = img.clone();
  medianBlur(img,smoothed_img,5);
  namedWindow( "Uniform", WINDOW_AUTOSIZE );
  imshow( "Uniform", smoothed_img );
  
  img = imread("./LENA_RGB_salt_and_pepper.jpg", 1 );
  smoothed_img = img.clone();
  medianBlur(img,smoothed_img,5);
  namedWindow( "Salt and Pepper", WINDOW_AUTOSIZE );
  imshow( "Salt and Pepper", smoothed_img );
  
  waitKey(0);
  return 0;
}

Filtr medianowy 

#include <cstdlib>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

/// Global Variables
Mat img; 
int bins = 1;
/// Function Headers
void median(int, void* );
 
int main(){
  /// Read the image
  img = imread( "/LENA_512.jpg", 1 );
  // cvtColor(img, img, CV_BGR2GRAY);
 
  /// Create Trackbar to enter the number of bins
  char* window_image = "Source image";
  namedWindow( window_image, CV_WINDOW_AUTOSIZE );
  createTrackbar("* Median filter: ", window_image, &bins, 100, median );
  median(1, 0);
 
  /// Show the image
  imshow( window_image, img );
 
  /// Wait until user exits the program
  waitKey(0);
  return 0;
}
 
void median(int, void* ){
  Mat smoothed_img;
  medianBlur(img,smoothed_img,2*bins+1);  
  imshow( "Median filter", smoothed_img );
}

Filtr Gaussowski 

#include <cstdlib>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;

int main(){
  Mat img = imread("/home/przemek/Pulpit/openCVExamples/LENA_512.jpg", 1 );
  // cvtColor(img, img, CV_BGR2GRAY);
  for(int i = 1 ; i < 10; i ++){
    Mat smoothed_img = img.clone();
    GaussianBlur(img,smoothed_img,Size(2*i+1,2*i+1),1.5);
  }
  waitKey(0);
  return 0;
}

Filtr Gaussowski 

#include <cstdlib>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;

int main(){
  Mat img = imread("./LENA_gaussian.jpg", 1 );
  // cvtColor(img, img, CV_BGR2GRAY);
  Mat smoothed_img = img.clone();
  GaussianBlur(img,smoothed_img,Size(5,5),1.5);
  namedWindow( "Gaussian", WINDOW_AUTOSIZE );
  imshow( "Gaussian", smoothed_img );
  
  img = imread("./LENA_uniform.jpg", 1 );
  smoothed_img = img.clone();
  GaussianBlur(img,smoothed_img,Size(5,5),1.5);
  namedWindow( "Uniform", WINDOW_AUTOSIZE );
  imshow( "Uniform", smoothed_img );
  
  img = imread("./LENA_RGB_salt_and_pepper.jpg", 1 );
  smoothed_img = img.clone();
  GaussianBlur(img,smoothed_img,Size(5,5),1.5);
  namedWindow( "Salt and Pepper", WINDOW_AUTOSIZE );
  imshow( "Salt and Pepper", smoothed_img );
  
  waitKey(0);
  return 0;
}

Filtr Gaussowski 

#include <cstdlib>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;
 
/// Global Variables
Mat img; 
int bins = 1;
int sigma = 1;
/// Function Headers
void gaussian(int, void* );
 
int main(){
  /// Read the image
  img = imread( "/home/przemek/Pulpit/openCVExamples/LENA_512.jpg", 1 );
  // cvtColor(img, img, CV_BGR2GRAY);
 
  /// Create Trackbar to enter the number of bins
  char* window_image = "Source image";
  namedWindow( window_image, CV_WINDOW_AUTOSIZE );
  createTrackbar("* Gaussian filter: ", window_image, &sigma, 100, gaussian);
  gaussian(1, 0);
 
  /// Show the image
  imshow( window_image, img );
 
  /// Wait until user exits the program
  waitKey(0);
  return 0;
}
 
void gaussian(int, void* ){
  Mat smoothed_img;
  GaussianBlur(img,smoothed_img,Size(2*sigma+1,2*sigma+1),sigma); 
  imshow( "Gaussian filter", smoothed_img );
}

Wyostrzanie 

#include <cstdlib>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

int main(){
  Mat img = imread("./LENA_512.jpg", 1 );
	Mat kernel(3,3,CV_32F,cv::Scalar(0));
	// assigns kernel values
	kernel.at(1,1)= 5.0;
	kernel.at(0,1)= -1.0;
	kernel.at(2,1)= -1.0;
	kernel.at(1,0)= -1.0;
	kernel.at(1,2)= -1.0;

	//filter the image
	cv::filter2D(img,img,img.depth(),kernel);

	namedWindow( "Sharpen", WINDOW_AUTOSIZE );
  imshow( "Sharpen", img );
  waitKey(0);
  return 0;  
}