Uploaded by abdelrazekali76

oral

advertisement
Median Filter
#include <opencv2/core/core.hpp>
//sort the window using insertion sort
void insertionSort(int window[])
{
int temp, i , j;
for(i = 0; i < 9; i++){
temp = window[i];
for(j = i-1; j >= 0 && temp < window[j]; j--){
window[j+1] = window[j];
}
window[j+1] = temp;
}
}
int main()
{
Mat src, dst;
// Load an image
src = imread("lenna.png");
if( !src.data )
{ return -1; }
//create a window of size 9
int window[9];
for(int y = 0; y < src.rows; y++)
for(int x = 0; x < src.cols; x++)
dst.at<uchar>(y,x) = 0.0;
for(int y = 1; y < src.rows - 1; y++){
for(int x = 1; x < src.cols - 1; x++){
// Pick up window element
window[0] = src.at<uchar>(y - 1 ,x - 1);
window[1] = src.at<uchar>(y, x - 1);
window[2] = src.at<uchar>(y + 1, x - 1);
window[3] = src.at<uchar>(y - 1, x);
window[4] = src.at<uchar>(y, x);
window[5] = src.at<uchar>(y + 1, x);
window[6] = src.at<uchar>(y - 1, x + 1);
window[7] = src.at<uchar>(y, x + 1);
window[8] = src.at<uchar>(y + 1, x + 1);
// sort the window to find median
insertionSort(window);
// assign the median to centered element of the matrix
dst.at<uchar>(y,x) = window[4];
}
}
namedWindow("Target image");
imshow("final", dst);
return 0;
}
Image thresholding
#include <opencv2/core/core.hpp>
Mat adaptive_threshold(Mat image)
{
Mat result(image.size(),image.type());
int sum = 0 , avg = 0, t = 0;
for(int i = 0 ; i < image.rows ; i++)
{
for(int j = 0; j < image.cols ; j++)
{
t++;
sum += image.at<uchar>(m,n);
}
avg = sum / t;
if(image.at<uchar>(i,j)>avg)
result.at<uchar>(i,j)=255;
else
result.at<uchar>(i,j)=0;
}
}
return result;
}
int main()
{
image = imread("flower.png");
binary = adaptive_threshold(image);
imshow("Binary",binary);
return 0;
}
Low pass filter (blurring)
#include <opencv2/core/core.hpp>
int main()
{
Mat src, dst;
float sum;
/// Load an image
src = imread("salt.jpg");
if( !src.data )
{ return -1; }
// define the kernel
float Kernel[3][3] = {
{1/9.0, 1/9.0, 1/9.0},
{1/9.0, 1/9.0, 1/9.0},
{1/9.0, 1/9.0, 1/9.0}
};
dst = src.clone();
//convolution operation
for(int y = 1; y < src.rows - 1; y++){
for(int x = 1; x < src.cols - 1; x++){
sum = 0.0;
for(int k = -1; k <= 1;k++){
for(int j = -1; j <=1; j++){
sum = sum + Kernel[j+1][k+1]*src.at<uchar>(y - j, x - k);
}
}
dst.at<uchar>(y,x) = sum ;
}
}
namedWindow("final");
imshow("final", dst);
namedWindow("initial");
imshow("initial", src);
waitKey();
return 0;
}
Salt & Pepper noise
#include <opencv2/core/core.hpp>
void saltAndPepperNoise(Mat & image, const double noiseProbability)
{
int imageChannels = image.channels();
random_value_generator random;
long noisePoints = noiseProbability * image.rows * image.cols * imageChannels;
for (long i = 0; i < noisePoints; i++)
{
int row = random.get_integer(image.rows);
int column = random.get_integer(image.cols);
int channel = random.get_integer(imageChannels);
unsigned char * pixelValuePtr = image.ptr(row) + (column *
imageChannels) + channel;
*pixelValuePtr = random.get_boolean() ? 255 : 0;
}
}
int main()
{
cv::Mat image;
image = cv::imread("./lenna.png");
saltAndPepperNoise (Mat image, 0.2);
}
Histogram Equalization
orginal = imread('cameraman.tif');
[rows,columns,~] = size(orginal);
finalResult = uint8(zeros(rows,columns));
pixelNumber = rows*columns;
frequncy = zeros(256,1);
pdf = zeros(256,1);
cdf = zeros(256,1);
cummlative = zeros(256,1);
outpic = zeros(256,1);
for i = 1:1:rows
for j = 1:1:columns
val = orginal(i,j);
frequncy(val+1) = frequncy(val+1)+1;
pdf(val+1) = frequncy(val+1)/pixelNumber;
end
end
sum =0 ;
intensityLevel = 255;
for i = 1:1:size(pdf)
sum =sum +frequncy(i);
cummlative(i) = sum;
cdf(i) = cummlative(i)/ pixelNumber;
outpic(i) = round(cdf(i) * intensityLevel);
end
for i = 1:1:rows
for j = 1:1:columns
finalResult(i,j) = outpic(orginal(i,j) + 1);
end
end
subplot(1,2,1),imshow(finalResult),title('after histeq');
subplot(1,2,2),imshow(histeq(orginal)),title('before histeq');
Edge Detection
I=double(imread('image1.jpg'));
In=I; mask1=[1, 0, -1;1, 0, -1;1, 0, -1];
mask2=[1, 1, 1;0, 0, 0;-1, -1, -1];
mask3=[0, -1, -1;1, 0, -1;1, 1, 0];
mask4=[1, 1, 0;1, 0, -1;0, -1, -1];
mask1=flipud(mask1);
mask1=fliplr(mask1);
mask2=flipud(mask2);
mask2=fliplr(mask2);
mask3=flipud(mask3);
mask3=fliplr(mask3);
mask4=flipud(mask4);
mask4=fliplr(mask4);
for i=2:size(I, 1)-1
for j=2:size(I, 2)-1
neighbour_matrix1=mask1.*In(i-1:i+1, j-1:j+1);
avg_value1=sum(neighbour_matrix1(:));
neighbour_matrix2=mask2.*In(i-1:i+1, j-1:j+1);
avg_value2=sum(neighbour_matrix2(:));
neighbour_matrix3=mask3.*In(i-1:i+1, j-1:j+1);
avg_value3=sum(neighbour_matrix3(:));
neighbour_matrix4=mask4.*In(i-1:i+1, j-1:j+1);
avg_value4=sum(neighbour_matrix4(:));
I(i, j)=max([avg_value1, avg_value2, avg_value3, avg_value4]);
end
end
figure, imshow(uint8(I));
Mean filter
#include <opencv2/core/core.hpp>
int main()
{
Mat src, dst;
int avg = 0, sum=0;
// Load an image
src = imread("flower.png");
if( !src.data )
{ return -1; }
//create a window of size 9
int window[9];
for(int y = 0; y < src.rows; y++)
for(int x = 0; x < src.cols; x++)
dst.at<uchar>(y,x) = 0.0;
for(int y = 1; y < src.rows - 1; y++){
for(int x = 1; x < src.cols - 1; x++){
// Pick up window element
window[0] = src.at<uchar>(y - 1 ,x - 1);
window[1] = src.at<uchar>(y, x - 1);
window[2] = src.at<uchar>(y + 1, x - 1);
window[3] = src.at<uchar>(y - 1, x);
window[4] = src.at<uchar>(y, x);
window[5] = src.at<uchar>(y + 1, x);
window[6] = src.at<uchar>(y - 1, x + 1);
window[7] = src.at<uchar>(y, x + 1);
window[8] = src.at<uchar>(y + 1, x + 1);
// calc the average window to find mean
for(int a=0 ; a< 9;a++){
sum + = window[i];
}
avg = sum /8
dst.at<uchar>(y,x) = avg;
}
}
namedWindow("Target image");
imshow("final", dst);
return 0;}
Contrast Stretching
#include <opencv2/core.hpp>
void contrast_stretching(cv::Mat& src, cv::Mat& dst, double a, double b, double c, double
d){
src.copyTo(dst);
double min = 0, max = 0;
cv::minMaxLoc(dst, &min, &max, 0, 0);
int nr = dst.rows;
int nc = dst.cols *dst.channels();
for (int i = 0; i < nr; i++){
double* ptr_dst = dst.ptr<double>(i);
for (int j = 0; j < nc; j++){
if (min <= ptr_dst[j] < a)
ptr_dst[j] = (c / a)*ptr_dst[j];
else if(a <= ptr_dst[j] < b)
ptr_dst[j] = ((d-c)/(b-a))*ptr_dst[j];
else if (b <= ptr_dst[j] < max )
ptr_dst[j] = ((max - d) / (max - b))*ptr_dst[j];
}
}
int main(){
cv::Mat src =cv::imread("girl.tif");
if (src.empty()){
return -1;
}
cv::Mat dst;
contrast_stretching(src, dst, 20, 100, 30, 200);
cv::imshow("src", src);
cv::imshow("dst", dst);
}
Log Transform
#include <opencv2/core.hpp>
void LogTransform(cv::Mat& src, cv::Mat& dst, double c){
int nr = src.rows;
int nc = src.cols*src.channels();
src.copyTo(dst);
for (int i = 0; i < nr; i++){
const uchar* srcdata = src.ptr <uchar>(i);
double* dstdata = dst.ptr <double>(i);
for (int j = 0; j < nc; j++){
dstdata[j] = c*log(double(1.0 + srcdata[j]));
}
int main(){
cv::Mat src =cv::imread("girl.tif");
if (src.empty()){
return -1;
}
cv::Mat dst;
LogTransform(src,dst,10);
cv::imshow("src", src);
cv::imshow("dst", dst);
}
Image inverse
#include <opencv2/core.hpp>
void Image_inversion(cv::Mat& src, cv::Mat& dst){
int nr = src.rows;
int nc = src.cols*src.channels();
src.copyTo(dst);
for (int i = 0; i < nr; i++){
const uchar* srcdata = src.ptr <uchar>(i);
uchar* dstdata = dst.ptr <uchar>(i);
for (int j = 0; j < nc; j++){
dstdata[j] = 255 - srcdata[j];
}
}
}
int main(){
cv::Mat src =cv::imread("girl.tif");
if (src.empty()){
return -1;
}
cv::Mat dst;
Image_inversion(src, dst);
cv::imshow("src", src);
cv::imshow("dst", dst);
}
Download