TIL Day 23

Jason Jongyoub Lee·2023년 10월 26일
0

OpenCV

목록 보기
5/11

2023.10.20

Today's agenda

  1. Image filtering
  2. Image bluring / shanpning
  3. Noise filter

1. Image filtering

Frequency domain filtering

Using Fourier transform, transform image to frequency area and filter image

Spatial domain filtering

Filter Using pixel value of the image(Certain point and around pixel value)
Mostly mask operation is used(mask ~~ kernel)

  • Changed filter operation by mask's shape and value(cross, horizontal, grid, vertical)
void filter2D(InputArray src, OutputArray dst, int ddepth, InputArray kernel,
				Point anchor = Point(-1, 1), double delta = 0,
                int borderType = BORDER_DEFAULT);

parameters

  • src : source image
  • dst : destination image
  • ddepth : set depth of the destination image. if -1 uses depth of src
  • kernel : filter mask array. 1 channel floating value
  • anchor : anchor point of the mask. if Point(-1, 1) use center pixel to the anchor
  • delta : additional value of filter
  • borderType : edge processing method

2. Image bluring / sharpning

Image bluring

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

  • src : source image
  • dst : destination image
  • ksize : average filter size(Size(3, 3) means 3x3 matrix mask)
  • anchor : anchor point of the mask. if Point(-1, 1) use center pixel to the anchor
  • borderType : edge processing method
void GaussianBlur(InputArray src, OutputArray dst, Size ksize, double sigmaX,
					double sigmaY = 0, int borderType = BORDER_DEFAULT);

parameters

  • src : source image. gaussian blur function runs each channels
  • dst : destination image. must be same size and same type with source image
  • ksize : gaussian kernel size. if give Size() give a auto value by sigma value
  • sigmaX : sigma for x
  • sigmaY : sigma for y
  • borderType : edge processing method

Gaussian blur method used to solve above disadvantage of blur function. Using gaussian function destribution make gaussian filter and do blur

Image sharpning

Unsharp mask filtering

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

3. Noise filter

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

  • dst : normalized dstribution matrix. need to be defined and can have 1~4 channel
  • mean : mean value
  • stddev : standard deviation
#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

  • src : source image. CV_8UC3 or CV_32F1 or CV_32F3
  • dst : destination image. must be same size and same type with source image
  • d : radius from center pixel. if -1 declared by sigmaSpace
  • sigmaColor : normalized distribution of color space
  • sigmaSpace : normalized distribution of point space
  • borderType : edge processing method

0개의 댓글