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

[์˜์ƒ์ฒ˜๋ฆฌ] openCV2 ์‹œ์ž‘ํ•˜๊ธฐ - ํ”ฝ์…€์ด๋ž€?, Mat ์ด๋ฏธ์ง€ ๊ฐ์ฒด

by Danna 2017. 2. 18.
728x90
728x90

Pixel, ํ”ฝ์…€ 

ํ•˜๋‚˜ ๋‹น 0~255 ์ˆซ์ž๋กœ ๋ช…๋„๋ฅผ ๋‚˜ํƒ€๋ƒ„
ํ•ด์ƒ๋„ 1920x1080 ๋ผ๋Š” ๊ฑด ๊ฐ€๋กœ 1920 ํ”ฝ์…€ x ์„ธ๋กœ 1080 ํ”ฝ์…€์„ ๋œปํ•จ

0~255๋กœ๋งŒ ์ด๋ฏธ์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉด Grayscale(ํ‘๋ฐฑ) ๋ฐ–์— ์•ˆ๋จ -> ํ•œ ํ”ฝ์…€๋‹น 1 byte
์ƒ‰์ƒ์„ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด ๊ฐ๊ฐ R, G, B ๋งˆ๋‹ค 0~255 ๊ฐ’์„ ๊ฐ€์ง   -> ํ•œ ํ”ฝ์…€๋‹น 3 byte
(R + G + B) / 3 = ํ‘๋ฐฑ ๋ช…๋„ ๊ฐ’

์ด๋ฏธ์ง€ ํŒŒ์ผ ํฌ๋งท

.raw ํŒŒ์ผ - ์šฉ๋Ÿ‰์ด ํฌ์ง€๋งŒ ํ”ฝ์…€ ๊ฐ’์ด ํ•˜๋‚˜๋„ ๊นจ์ง€์ง€ ์•Š์œผ๋ฏ€๋กœ C/C++ ์—์„œ ํŒŒ์ผ์„ ๋ฐ”๋กœ ์ฝ์–ด์„œ ์ƒ‰์ƒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Œ

.bmp ํŒŒ์ผ - rawํŒŒ์ผ ๋ณด๋‹ค ์šฉ๋Ÿ‰์ด ์ž‘์•„ ๋” ๋น ๋ฅธ ์ „์†ก ๊ฐ€๋Šฅ. Windows์—์„œ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋ฉฐ C++ ๊ธฐ๋ณธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์žˆ์Œ

.jpg .gif .png ํŒŒ์ผ - ์šฉ๋Ÿ‰์ด ์ž‘์ง€๋งŒ ํŒŒ์ผ์„ ๋ฐ”๋กœ ์ฝ์„ ์ˆ˜ ์—†๊ณ  ๋ณ„๋„์˜ Library๋ฅผ ์ด์šฉํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๋‹ค๋ฃจ๊ธฐ ์–ด๋ ค์›€

BMP ํŒŒ์ผ ํฌ๋งท ํ™•์ธํ•ด๋ณด๊ธฐ


ํ”ฝ์…€์— ์ ‘๊ทผํ•˜๋Š”๋ฒ•?




  • MAT Class : openCV2 ์—์„œ ์‚ฌ์šฉ, ์ด๋ฏธ์ง€๋ฅผ ๋‹ด๋Š” Class, C++์—์„œ ์‚ฌ์šฉ -> Mat์„ ์‚ฌ์šฉํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 
  • struct IpImage : openCV ์—์„œ ์‚ฌ์šฉ, C์—์„œ ์‚ฌ์šฉ

Mat ์ด๋ฏธ์ง€ ๊ฐ์ฒด ์ƒ์„ฑ

Mat image;

Mat image(H, W, CV_8UC1);

Mat image(H, W, CV_8UC1, Scalar(255));

Mat image = imread("test.png");


*์ฐธ๊ณ * Mat ๊ฐ์ฒด ์žฌ์ •์˜ ๋ถˆ๊ฐ€๋Šฅ, 

Mat image 
  • size 0, 0
Mat image(H, W, CV_8UC1);
  • H, W : define num
  • CV_8UC1 : 8bit Unsinged Char  , Channel 1๊ฐœ -> ๊ทธ๋ ˆ์ด์Šคํ‚จ
Mat image(H, W, CV_8UC1, Scalar(255));
  • 255 - ๊ธฐ๋ณธ๊ฐ’ ์›๋ž˜ 0์ธ๋ฐ ํฐ์ƒ‰๋ฐฐ๊ฒฝ์œผ๋กœ ๋งž์ถฐ์ง
Mat image = imread("test.png");

  • cpp ํŒŒ์ผ ์žˆ๋Š” ํด๋”์˜ ์ƒ๋Œ€์œ„์น˜\ํŒŒ์ผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

์ด๋ฏธ์ง€ ์ฐธ์กฐ & ๋ณต์‚ฌ

Mat image = imread("test.png");

// ๋ ˆํผ๋Ÿฐ์Šค ์ฐธ์กฐ
Mat A = image;

// ์ด๋ฏธ์ง€ ๋ณต์‚ฌ
Mat B(image);

Mat C;
image.copyTo(C);

Mat D(image.rows, image.cols, CV_8UC3);
D = image.clone();


๊ทธ๋ ˆ์ด or ์ปฌ๋Ÿฌ ์ฑ„๋„ ์„ค์ •

Mat image = imread("test.png", CV_LOAD_IMAGE_COLOR);

Mat image2 = imread("test.png", CV_LOAD_IMAGE_GRAYSCALE);

Mat image = imread("test.png");
  • ์ฑ„๋„ 3๊ฐœ, rgb๋กœ ์ƒ‰์ƒ ํ‘œํ˜„ 
  • CV_LOAD_IMAGE_COLOR = 1
  • imread ํ•จ์ˆ˜์˜ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์ƒ๋žต ๊ฐ€๋Šฅ
Mat image2 = imread("test.png", CV_LOAD_IMAGE_GRAYSCALE);
  • ์ฑ„๋„ 1๊ฐœ, ํ‘๋ฐฑ ํ‘œํ˜„
  • CV_LOAD_IMAGE_GRAYSCALE = 0


  • 2020.12.07 ํ”ฝ์…€์— ๋Œ€ํ•œ ๋‚ด์šฉ ์ถ”๊ฐ€ 
  • ํ‹€๋ฆฐ ์ ์€ ์ง€์ ํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค ~


728x90
728x90