์ก์ ์์ฑ
#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)๋ฅผ ๋ถ๋ฆฌํ์ฌ ํํํ๋ ์์ ๋ชจ๋ธ.
. ๋์๋ฆฌ ์ธ๋ฏธ๋๋ฅผ ๋ฃ๊ณ ์ ๋ฆฌํ ๊ฒ ์
๋๋ค. ํ๋ฆฐ ์ ์ ์ง์ ํด์ฃผ์๋ฉด ๊ฐ์ฌํ๊ฒ ์ต๋๋ค.