๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ก EE's DEV/์˜์ƒ์ฒ˜๋ฆฌ

[์˜์ƒ์ฒ˜๋ฆฌ] OpenCV2 ํ”ฝ์…€ ์—ฐ์‚ฐ – ํ‰๊ท , ๋ง์…ˆ, ๋บ„์ƒ˜, ํ‰๊ท  ๊ฐ’, ๊ฐ€์ค‘ ํ‰๊ท  ๊ฐ’, ๊ฐ€์šฐ์‹œ์•ˆ ํ•„ํ„ฐ

by Danna 2017. 2. 27.
728x90
728x90

๋‘ ์ด๋ฏธ์ง€๊ฐ„ ํ”ฝ์…€์˜ ํ‰๊ท  ๊ฐ’ ๊ตฌํ•˜๊ธฐ

void avgImage(Mat &img1, Mat &img2, Mat &out)
{
    for (int y = 0; y < out.size().height; y++)
        {
            for (int x = 0; x < out.size().width; x++)
                out.at<uchar>(y, x) = (img1.at<uchar>(y, x) + img2.at<uchar>(y, x)) / 2;
        }
}


* img1, img2 ๋Š” ์‚ฌ์ด์ฆˆ๊ฐ€ ๋˜‘๊ฐ™์€๊ฒƒ์œผ๋กœ ํ•˜๊ธฐ!

ํ‰๊ท  ์—ฐ์‚ฐ ๋œ ์ด๋ฏธ์ง€ ๊ฒฐ๊ณผ > 





๋‘ ์ด๋ฏธ์ง€๊ฐ„ ํ”ฝ์…€์˜ ๋”ํ•œ ๊ฐ’ ๊ตฌํ•˜๊ธฐ

#include <opencv2/core/core.hpp>

#include <opencv2/imgproc/imgproc.hpp>

#include <opencv2/highgui/highgui.hpp>

using namespace cv;

void addImage(Mat &img1, Mat &img2, Mat &out)
{
    for (int y = 0; y < out.size().height; y++)
    {
        for (int x = 0; x < out.size().width; x++)
        {
            if (img1.at<uchar>(y, x) + img2.at<uchar>(y, x) > 255)
                out.at<uchar>(y, x) = 255;
            else
                out.at<uchar>(y, x) = img1.at<uchar>(y, x) + img2.at<uchar>(y, x);
        }
    }
}

int main()
{
    Mat img1 = imread("1.jpg", 0);           
    Mat img2 = imread("heart.png", 0);

    int width = img1.size().width;
    int height = img1.size().height;

    Mat addOut(height, width, CV_8UC1);

    addImage(img1, img2, addOut);

    imshow("original", img1);
    imshow("heart", img2);
    imshow("add Image", addOut);

    imwrite("addImage.jpg", addOut);

    waitKey();
}

 Mat addOut(height, width, CV_8UC1);
    • Mat ๊ฐ์ฒด ์ดˆ๊ธฐํ™”
    • height, width ์ˆœ์„œ (ํฌ๊ธฐ๊ฐ€ ๋‹ค๋ฅผ ๊ฒฝ์šฐ ์ฃผ์˜)
    • CV_8UC1 : 1์ฑ„๋„, ๊ทธ๋ ˆ์ด
imwrite("addImage.jpg", addOut);
    • "addImage.jpg" : ๋งŒ๋“ค ํŒŒ์ผ๋ช…
    • addOut : ํŒŒ์ผ๋กœ ๋งŒ๋“ค Mat ๊ฐ์ฒด

add Image ๊ฒฐ๊ณผ >



๋‘ ์ด๋ฏธ์ง€๊ฐ„ ํ”ฝ์…€์˜ ๋บ€ ๊ฐ’ ๊ตฌํ•˜๊ธฐ

#include <opencv2/core/core.hpp>

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;

int main()
{
    Mat img1 = imread("img1.png", 0);
    Mat img2 = imread("img2.png", 0);

    imshow("img1", img1);
    imshow("img2", img2);

    int width = img1.size().width;
    int height = img1.size().height;

    Mat output(height, width, CV_8UC1);

    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            output.at<uchar>(y, x) = abs(img1.at<uchar>(y, x) - img2.at<uchar>(y, x));
        }
    }

    imshow("sub Image", output);

    waitKey();
}
           
output.at<uchar>(y, x) = abs(img1.at<uchar>(y, x) - img2.at<uchar>(y, x));
    • abs ํ•จ์ˆ˜ : ์ ˆ๋Œ“๊ฐ’ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜
    • math ํ—ค๋”์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ํ•จ์ˆ˜์ง€๋งŒ
    • opencv์—๋„ ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ๋ณ„๋„๋กœ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š์•„๋„ ๋จ

sub Image ๊ฒฐ๊ณผ >





Mean Filter, ํ‰๊ท  ๊ฐ’ ํ•„ํ„ฐ

์žฅ์ 

  • ๋‚ ์นด๋กœ์šด ์—์ง€๊ฐ€ ๋ฌด๋ŽŒ์ง„๋‹ค.
  • ์žก์Œ์˜ ์˜ํ–ฅ์ด ์‚ฌ๋ผ์ง„๋‹ค.

๋‹จ์ 

  • ๊ณผ๋„ํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์ดˆ์ ์ด ๋งž์ง€ ์•Š์€ ์‚ฌ์ง„์ฒ˜๋Ÿผ ๋ณ€ํ•œ๋‹ค.

1/9 ์˜ ์˜๋ฏธ

  • ๊ฐ€์ค‘์น˜๋Š” ์ „์ฒด์˜ ํ•ฉ์„ 1๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.
  • 1๋ณด๋‹ค ์ปค์งˆ ๊ฒฝ์šฐ, ๊ฒฐ๊ณผ ์˜์ƒ์€ ์ „๋ฐ˜์ ์œผ๋กœ ๋ฐ๊ธฐ ๊ฐ’์ด ์ฆ๊ฐ€๋˜๊ณ 
  • 1๋ณด๋‹ค ์ž‘์„ ๊ฒฝ์šฐ, ๋ฐ๊ธฐ ๊ฐ’์ด ๊ฐ์†Œ๋œ๋‹ค.



Weighted mean filter, ๊ฐ€์ค‘ ํ‰๊ท  ๊ฐ’ ํ•„ํ„ฐ


ํ‰๊ท  ๊ฐ’ ํ•„ํ„ฐ์˜ ๋‹จ์ 

  • ์ฃผ๋ณ€ ํ”ฝ์…€์˜ ๊ฐ’์„ ๋งŽ์ด ์ฐธ์กฐํ•˜์—ฌ ์›๋ž˜ ์œ„์น˜์˜ ํ”ฝ์…€ ์ •๋ณด๊ฐ€ ์ƒ๋Œ€์ ์œผ๋กœ ๊ฐ์†Œ๋œ๋‹ค.

๊ฐ€์ค‘ ํ‰๊ท  ๊ฐ’ ํ•„ํ„ฐ

  • ํ‰๊ท  ๊ฐ’ ํ•„ํ„ฐ์˜ ๋‹จ์ ์„ ๋ณด์™„ํ•œ ๊ฒƒ
  • ํ•„ํ„ฐ ๋งˆ์Šคํฌ์˜ ๊ฐ€์šด๋ฐ ์œ„์น˜ํ•œ ํ”ฝ์…€์— ๊ฐ€์ค‘์น˜๋ฅผ ๋” ์ฃผ์–ด ์›๋ž˜ ์œ„์น˜์˜ ํ”ฝ์…€ ์ •๋ณด๋ฅผ ๋œ ๊ฐ์†Œ์‹œํ‚จ๋‹ค. 





Gaussian filter, ๊ฐ€์šฐ์‹œ์•ˆ ํ•„ํ„ฐ 

๊ฐ€์šฐ์‹œ์•ˆ ๋ถ„ํฌ(์ •๊ทœ ๋ถ„ํฌ)๋Š” ์ž์—ฐ ํ˜„์ƒ์„ ๊ฐ€์žฅ ์ž˜ ํ‘œํ˜„ํ•˜๋Š” ์ด์ƒ์ ์ธ ํ™•๋ฅ  ๋ชจํ˜•์ด๋‹ค.

ํ‰๊ท ์ด 0์ด๊ณ , ํ‘œ์ค€ ํŽธ์ฐจ๊ฐ€ ฯƒ์ผ ๋•Œ, ํ•จ์ˆซ๊ฐ’๋“ค์€ -4ฯƒ โ‰ค x โ‰ค +4ฯƒ ์‚ฌ์ด์—์„œ ์ „์ฒด ๋ถ„ํฌ์˜ 99.99%๊ฐ€ ์กด์žฌํ•œ๋‹ค.

๊ฐ€์šฐ์‹œ์•ˆ ํ•จ์ˆ˜๋Š” ์—ฐ์† ํ•จ์ˆ˜์ด์ง€๋งŒ, ์ด์‚ฐํ˜•์˜ ๋งˆ์Šคํฌ๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ x, y ๊ฐ’์ด ์ •์ˆ˜์ผ ๋•Œ๋งŒ ์ถ”์ถœํ•˜์—ฌ ๋งˆ์Šคํฌ๋ฅผ ๋งŒ๋“ ๋‹ค.

2์ฐจ์› ๊ฐ€์šฐ์‹œ์•ˆ ํ•จ์ˆ˜๋Š” 1์ฐจ์› ํ•จ์ˆ˜์˜ ๊ณฑ์œผ๋กœ ๋ถ„ํ• ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, x, y ๋ฐฉํ–ฅ์œผ๋กœ 1์ฐจ์› ๋งˆ์Šคํฌ ์—ฐ์‚ฐ์„ ๊ฐ๊ฐ ์ˆ˜ํ–‰ํ•˜์—ฌ ๋น ๋ฅด๊ฒŒ ๊ฒฐ๊ณผ ์˜์ƒ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.


void gaussianFilter(Mat &image, Mat &out)

{

    if (image.channels() != 1)

        return;

    int mask[5][5] = {

        2, 4, 5, 4, 2,

        4, 9, 12, 9, 4,

        5, 12, 15, 12, 5,

        4, 9, 12, 9, 4,

        2, 4, 5, 4, 2

    };


    int sum = 0;

    out = image.clone();

    for (int y = 2; y < image.size().height - 2; y++)

    {

        for (int x = 2; x < image.size().width - 2; x++)

        {

            sum = 0;


            for (int m = -2; m <= 2; m++)

            {

                for (int n = -2; n <= 2; n++)

                    sum += image.at<uchar>(y + m, x + n) 

    * mask[m + 2][n + 2];

                out.at<uchar>(y, x) = sum / 159.f;

            }

        }

    }

}





  • 2020.12.07 ํ•„ํ„ฐ์— ๋Œ€ํ•œ ๋‚ด์šฉ ์ถ”๊ฐ€ (2018 ๋™์•„๋ฆฌ ์„ธ๋ฏธ๋‚˜ ์ง„ํ–‰์ž๋ฃŒ)


728x90
728x90