TIL Day 27

Jason Jongyoub Lee·2023년 10월 31일
0

OpenCV

목록 보기
9/11

2023.10.26

Today's agenda
1. image binarization
2. mopology
3. labling and outer line detection

1. image binarization

binarization on image

Make pixels on image make to only 0 or 255 value(true or false)
it can be seperate background and object
Make a image into ROI(Reason Of Interest) and Non-ROI space

double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type);

parameters

  • src : source image
  • dst : destination image(same size, type channels from src)
  • thresh : user defined threshold value
  • maxval : max value if use THRESH_BINARY or THRESH_BINARY_INV
  • type : threshold function type

return value

  • threshold value
#include <iostream>
#include "opencv2/opencv.hpp"

using namespace std;
using namespace cv;

int t_value = 128;
void on_trackbar_threshold(int, void*);
Mat src, dst;

int main(int argc, char* argv[])
{
	String filename = "neutrophils.png";

	if (argc > 1) {
		filename = argv[1];
	}

	src = imread(filename, IMREAD_GRAYSCALE);

	if (src.empty()) {
		cerr << "Image load failed!" << endl;
		return -1;
	}

	namedWindow("src");
	imshow("src", src);

	namedWindow("dst");
	createTrackbar("Threshold", "dst", &t_value, 255, on_trackbar_threshold);
	on_trackbar_threshold(0, 0); // Call the function to initialize

	waitKey();
}

void on_trackbar_threshold(int, void*)
{
	threshold(src, dst, t_value, 255, THRESH_BINARY | THRESH_OTSU);
	imshow("dst", dst);
}

global binarization / global threshold

problem of local binarization
week of different brightness image

For global binarazation, tear a part from a image like a 16 or 25 parts and do each part and local binarization

void adaptiveThreshold(InputArray src, OutputArray dst, double maxValue, int adaptiveMethod,
						int thresholeType, int blockSize, double C);

parameters

  • src : input image
  • dst : destination image
  • maxValue : maximum threshold value
  • adaptiveMethod : each block's getting threshold method
    - ADAPTIVE_THRESH_MEAN_C : average from arithmetic
    • ADAPTIVE_THRESH_GAUSSIAN_C : gaussian average
  • thresholdType : THRESH_BINARY or THRESH_BINARY_INV
  • blockSIze : block size(odd value over 3)
  • C : value for minus value for threshold

3. mopology

detection of object in shape space that pre/post processing

erosion

dilation

void erode(InputArray src, OutputArray dst, InputArray kernel, Point anchor = Point(-1, 1),
			int iterations = 1, int borderType = BORDER_CONSTANT,
            const Scalar& borderValue = morphologyDefaultBorderValue());

paramerers

  • src : source image
  • dst : destination image(same size and type from src)
  • kernel : mask element if set Mat() for 3x3 element
  • anchor : anchor point if Point(-1, 1) uses central point of image
  • iterations : iteration numbers
void dialate(InputArray src, OutputArray dst, InputArray kernel, Point anchor = Point(-1, 1),
			int iterations = 1, int borderType = BORDER_CONSTANT,
            const Scalar& borderValue = morphologyDefaultBorderValue());

paramerers

  • src : source image
  • dst : destination image(same size and type from src)
  • kernel : mask element if set Mat() for 3x3 element
  • anchor : anchor point if Point(-1, 1) uses central point of image
  • iterations : iteration numbers
Mat getStructuringElement(int shape, Size ksize, Point anchor = Point(-1, 1));
``

paramerters

  • shape : shape of mask element
    - MORPH_RECT : rectangle
    • MORPH_CROSS : cross
    • MORPH_ELLIPSE : circle that is included in one rectangle
  • ksize : size of mask
  • anchor : ahcnor point if Point(-1, 1) uses central point of image

return value

  • CV_8UC1 type of matrix

opening/closing procedure or binarization

opening = erosion -> dilation
closing = dilation -> erosion

void morphologyEx(InputArray src, OutputArray dst, int op, InputArray kernel,
					Point anchor = Point(-1, 1), int iterations = 1,
                    int borderType = BORDER_CONSTANT,
                    const Scalar& borderValue = morphologyDefaultBorderValue());

parameters

  • src, dst : source and destination image
  • op : morphology constant value
  • kernel : kernel element
  • anchor : anchor point
  • iterations : iteration value

0개의 댓글