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

[์˜์ƒ์ฒ˜๋ฆฌ] openCV2 ์ด๋ฏธ์ง€ ๊ธฐ๋ณธ ์—ฐ์‚ฐ, ์ƒ‰์ƒ ๋ชจ๋ธ

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

์žก์Œ ์ƒ์„ฑ

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

using namespace std;
using namespace cv;

void salt(Mat &image, int snum)
{
    for (int n = 0; n < snum; n++)
    {
        int x = rand() % image.cols;
        int y = rand() % image.rows;

        if (image.channels() == 1)      // Gray
        {
            image.at<uchar>(y, x) = 0;  // ํฐ์ƒ‰ ์žก์Œ
        }
        else if (image.channels() == 3) // RGB
        {
            image.at<Vec3b>(y, x)[0] = 255;
            image.at<Vec3b>(y, x)[1] = 255;
            image.at<Vec3b>(y, x)[2] = 255;
        }
    }
}

int main()
{
    Mat image = imread("test.png");

    salt(image, 10000);
    imshow("salt", image);

    waitKey(0);
}


Mat &image 
    • main์—์„œ ์“ฐ๊ณ  ์žˆ๋Š” Mat ๊ฐ์ฒด๋ฅผ salt ํ•จ์ˆ˜์—์„œ ์ฐธ์กฐ, ์ฐธ์กฐํ•˜์ง€ ์•Š์œผ๋ฉด main์—์„œ ๋ฐ˜์˜ x
for ๋ฌธ snum  
    • ์  ์ฐ๋Š” ํšŸ์ˆ˜
if(image.channels() == 1)  
    • channel1 = GraySclae 
    • channel3 = RGB
image.at<uchar>(y, x) = 255;
    • unsinged char - 1byte, 0 ~ 255 ํ‘œํ˜„
    • y๋งŒํผ ๋‚ด๋ ค๊ฐ€๊ณ  x๋งŒํผ ์ด๋™ํ•ด์„œ ๊ทธ ์ขŒํ‘œ์— 255 
image.at<Vec3b>(y, x)[0] = 255;     // B
image.at<Vec3b>(y, x)[1] = 255;     // G

image.at<Vec3b>(y, x)[2] = 255;     // R
  • RGB 3์ฑ„๋„ ์ด๋ฏธ์ง€์ธ ๊ฒฝ์šฐ, ๊ฐ๊ฐ์˜ ์ƒ‰์ƒ๊ฐ’์— ์žก์Œ์„ ์ƒ์„ฑํ•ด์ฃผ์–ด์•ผํ•จ.
  • 0, 1, 2 = B, G, R ์ˆœ์„œ๋Œ€๋กœ.


๋นจ๊ฐ„์ƒ‰๋งŒ ๋‚˜ํƒ€๋‚ด๊ธฐ
  • ๋งŒ์•ฝ grayScale ์ด๋ผ๋ฉด? return
  • Mat.at<Vec3b>(y, x)[index] ํ•จ์ˆ˜ ์ด์šฉํ•ด์„œ ๋นจ๊ฐ„์ƒ‰๋งŒ ์ถ”์ถœํ•˜๊ธฐ
    • ๋นจ๊ฐ„์ƒ‰์„ ์ถ”์ถœํ•˜๊ธฐ ์œ„ํ•ด, ํŒŒ๋ž‘(B), ์ดˆ๋ก(G) ์„ 0์œผ๋กœ ๋งŒ๋“ค๊ธฐ.
void getRed(Mat &image)
{
    if (image.channels() == 1)
        return;

    for (int x=0; x < image.cols; x++)
    {
        for (int y=0; y < image.rows; y++)
        {
            image.at<Vec3b>(y, x)[0] = 0;
            image.at<Vec3b>(y, x)[1] = 0;
        }
    }
}


void main()
{
Mat image = imread(โ€œtest.pngโ€);
getRed(image);

imshow(โ€œgetGreenโ€, image);
waitKey(0);
}




CYAN๋งŒ ๋‚˜ํƒ€๋‚ด๊ธฐ (CYAN = GREEN + BLUE)

void getCyan(Mat &image)
{
    if (image.channels() == 1)
        return;

    for (int x = 0; x < image.cols; x++)
    {
        for (int y = 0; y < image.rows; y++)
            image.at<Vec3b>(y, x)[2] = 0;
    }
}



ํ‘๋ฐฑ ๋ฐ˜์ „

  • at ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์ตœ๋Œ€๊ฐ’์—์„œ ํ˜„์žฌ ํ”ฝ์…€๊ฐ’์„ ๋นผ์„œ ์ƒ‰์ƒ์„ ๋ฐ˜์ „์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.
void grayInverse(Mat &image, Mat &output)
{
    if (image.channels() != 1)
        return;

    output = image.clone();

    for (int y = 0; y < image.size().height; y++)
        for (int x = 0; x < image.size().width; x++)
            output.at<uchar>(y, x) = 255 - output.at<uchar>(y, x);
}


void main()
{
Mat image = imread(โ€œtest.pngโ€, 0);
Mat out;

inverse(image, out);

imshow(โ€œOriginalโ€, image);
imshow(โ€œInverseโ€, out);
waitKey(0);
}



์ปฌ๋Ÿฌ ๋ฐ˜์ „
void colorInverse(Mat &image, Mat &output)
{
    if (image.channels() == 1)
        return;

    output = image.clone();

    for (int x = 0; x < image.cols; x++)
    {
        for (int y = 0; y < image.rows; y++)
        {
            output.at<Vec3b>(y, x)[0] = 255 - output.at<Vec3b>(y, x)[0];
            output.at<Vec3b>(y, x)[1] = 255 - output.at<Vec3b>(y, x)[1];
            output.at<Vec3b>(y, x)[2] = 255 - output.at<Vec3b>(y, x)[2];
        }
    }
}


๋ช…๋„ ์กฐ์ ˆ

  • ๋ช…๋„๋ฅผ ์กฐ์ ˆํ•˜๊ธฐ ์œ„ํ•ด ๋ณ€์ˆ˜ num ์˜ ๊ฐ’ ๋งŒํผ ๋”ํ•œ๋‹ค.
  • 255๋ฅผ ๋„˜๊ฑฐ๋‚˜ 0์•„๋ž˜ ๊ฐ’ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ํ•ด์•ผํ•จ.


void brightness(Mat &image, Mat &output, int num)
{
    if (image.channels() != 1)
        return;

    output = image.clone();

    for (int y = 0; y < image.size().height; y++)
    {
        for (int x = 0; x < image.size().width; x++)
        {
           if (output.at <uchar>(y, x) + num > 255)
               output.at<uchar>(y, x) = 255;
           else
               output.at<uchar>(y, x) += num;
         }
    }
}


void main()
{
Mat image = imread(โ€œtest.pngโ€, 0);
Mat out;
brightness(image, out, 50);
imshow(โ€œOriginalโ€, image);
imshow(โ€œ๋ฐ๊ธฐ ์กฐ์ ˆโ€, out);
waitKey(0);
}



๋Œ€๋น„ ์กฐ์ ˆ (๋ฐ์€ ๊ณณ ๋ฐ๊ฒŒ, ์–ด๋‘์šด ๊ณณ ์–ด๋‘ก๊ฒŒ)

  • 255๋ฅผ ๋„˜๊ฑฐ๋‚˜ 0์•„๋ž˜ ๊ฐ’ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ํ•ด์•ผํ•จ.

void contrast(Mat &image, Mat &output)
{
    if (image.channels() != 1)
        return;

    output = image.clone();

    for (int y = 0; y < image.size().height; y++)
    {
        for (int x = 0; x < image.size().width; x++)
        {
            int pixel = output.at<uchar>(y, x);
            int contrast = (pixel - 128) / 2;
            if (pixel > 128)
            {
                if (pixel + contrast > 255)
                    output.at<uchar>(y, x) = 255;
                else
                    output.at<uchar>(y, x) += contrast;
            }
            else if (pixel < 128)
            {
                if (pixel + contrast < 0)
                    output.at<uchar>(y, x) = 0;
                else
                    output.at<uchar>(y, x) += contrast;
            }
        }
    }
}



์ƒ‰์ƒ ๋ชจ๋ธ


Gray : ๋ฐ๊ธฐ ์ •๋ณด๋งŒ์œผ๋กœ ์˜์ƒ์„ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ. ๊ฒ€์ •์ƒ‰ 0 ~ ํฐ์ƒ‰ 255๊นŒ์ง€ ๋ฐ๊ธฐ ๊ฐ’(intensity)์œผ๋กœ ํ”ฝ์…€ ๊ฐ’์„ ํ‘œํ˜„ํ•จ.


RGB : ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์ƒ‰์ƒ๋ชจ๋ธ. Color๋ฅผ Red, Green, Blue 3๊ฐ€์ง€ ์„ฑ๋ถ„์˜ ์กฐํ•ฉ์œผ๋กœ ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ.


HSV : Hue(์ƒ‰์กฐ), Saturation(์ฑ„๋„), Value(๋ช…๋„) 3๊ฐ€์ง€ ์„ฑ๋ถ„์œผ๋กœ ์ƒ‰์„ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ. 

         ์›๋ฟ”(conic), ์›๊ธฐ๋‘ฅ(cylindric) ๋‘๊ฐ€์ง€ ํ˜•ํƒœ ์กด์žฌ. ์ƒ‰์˜ ์ฐจ์ด๋ฅผ ์ •๋Ÿ‰์ ์œผ๋กœ ์ˆ˜์น˜ํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์—์„œ ์ฐจ์ด๊ฐ€ ๋‚จ.


YCbCr : RGB ๋ชจ๋ธ์—์„œ ๋ฐ๊ธฐ์„ฑ๋ถ„(Y), ์ƒ‰์ฐจ์ •๋ณด(Cb, Cr)๋ฅผ ๋ถ„๋ฆฌํ•˜์—ฌ ํ‘œํ˜„ํ•˜๋Š” ์ƒ‰์ƒ ๋ชจ๋ธ.





. ๋™์•„๋ฆฌ ์„ธ๋ฏธ๋‚˜๋ฅผ ๋“ฃ๊ณ  ์ •๋ฆฌํ•œ ๊ฒƒ ์ž…๋‹ˆ๋‹ค. ํ‹€๋ฆฐ ์ ์€ ์ง€์ ํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

728x90
728x90