2023.10.20
Today's agenda
Using Fourier transform, transform image to frequency area and filter image
Filter Using pixel value of the image(Certain point and around pixel value)
Mostly mask operation is used(mask ~~ kernel)
void filter2D(InputArray src, OutputArray dst, int ddepth, InputArray kernel, Point anchor = Point(-1, 1), double delta = 0, int borderType = BORDER_DEFAULT);
parameters
Get all average pixel value of around such of pixels and set to the pixel value
Edge become dull and reduce noise. If mask is bigger result smother
But if mask bigger, far pixels effect bigger
void blur(InputArray src, OutputArray dst, Size ksize, Point anchor = Point(-1, 1), int borderType = BORDER_DEFAULT);
parameters
void GaussianBlur(InputArray src, OutputArray dst, Size ksize, double sigmaX, double sigmaY = 0, int borderType = BORDER_DEFAULT);
parameters
Gaussian blur method used to solve above disadvantage of blur function. Using gaussian function destribution make gaussian filter and do blur
Create image use blured image
It can be used to blur function. but to avoid to much effect from far pixels, use gaussian blur function
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
void sharpen_mean();
void sharpen_gaussian();
int main(void)
{
sharpen_mean();
sharpen_gaussian();
}
void sharpen_mean()
{
Mat src = imread("rose.bmp", IMREAD_GRAYSCALE);
if (src.empty()) {
cerr << "Image load failed!" << endl;
return;
}
float sharpen[] = {
-1 / 9.f, -1 / 9.f, -1 / 9.f,
-1 / 9.f, 17 / 9.f, -1 / 9.f,
-1 / 9.f, -1 / 9.f, -1 / 9.f
};
Mat kernel(3, 3, CV_32F, sharpen);
Mat dst;
filter2D(src, dst, -1, kernel);
imshow("src", src);
imshow("dst", dst);
waitKey();
destroyAllWindows();
}
void sharpen_gaussian()
{
Mat src = imread("rose.bmp", IMREAD_GRAYSCALE);
if (src.empty()) {
cerr << "Image load failed!" << endl;
return;
}
imshow("src", src);
Mat srcf;
src.convertTo(srcf, CV_32FC1);
for (int sigma = 1; sigma <= 5; sigma++) {
Mat blr;
GaussianBlur(srcf, blr, Size(), sigma);
float alpha = 1.0f;
Mat dst = (1.f + alpha) * srcf - alpha * blr;
dst.convertTo(dst, CV_8UC1);
String desc = format("sigma: %d", sigma);
putText(dst, desc, Point(10, 30), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(255), 1, LINE_AA);
imshow("dst", dst);
waitKey();
}
destroyAllWindows();
}
Writing camera to the image through image sensor noise can generate
Converting analog signal to electical value can generate noise and every sensor have a noise
void randn(InputOutputArray dst, InpuArray mean, InputArray stddev);
parameters
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
void on_trackbar(int, void*);
Mat src, dst, profile;
int row = 0;
int main(void)
{
src = imread("lenna.bmp", IMREAD_GRAYSCALE);
if (src.empty()) {
cerr << "Image load failed!" << endl;
return -1;
}
// GaussianBlur(src, src, Size(), 2);
namedWindow("dst");
namedWindow("profile");
profile.create(256, src.cols, CV_8UC1);
createTrackbar("Profile", "dst", &row, src.rows - 1, on_trackbar);
on_trackbar(0, 0);
waitKey();
}
void on_trackbar(int, void*)
{
src.copyTo(dst);
profile.setTo(255);
uchar* pSrc = (uchar*)src.ptr<uchar>(row);
uchar* pDst = (uchar*)dst.ptr<uchar>(row);
for (int i = 1; i < src.cols; i++) {
line(profile, Point(i - 1, 255 - pSrc[i - 1]), Point(i, 255 - pSrc[i]), 0);
pDst[i] = saturate_cast<uchar>(pSrc[i] + 50);
}
imshow("dst", dst);
imshow("profile", profile);
}
void bilateralFilter(InputArray src, OutputArray dst, int d double sigmaColor, double sigmaSpace, int borderType = BORDER_DEFAULT);
parameters