Mat 초기화

OpenCV/Mat의 기본처리 2013. 5. 20. 22:37


#include <iostream>

#include <opencv2/core/core.hpp>

using namespace cv;

using namespace std;

void main() {

// 3x3 단위행렬 (= Mat::eye(3,3, CV_64F) )

Mat m = (Mat_<double>(3,3) << 1, 0, 0, 0, 1, 0, 0, 0, 1);

// 2x2 회전행렬

double angle = 30, a = cos(angle*CV_PI/180), b = sin(angle*CV_PI/180);

Mat r = (Mat_<double>(2,2) << a, -b, b, a);


cout << "m=" << m << endl << endl;

cout << "r=" << r << endl;

}




#include <iostream>

#include <opencv2/core/core.hpp>

using namespace cv;

using namespace std;

void main() {

Mat mat1 = Mat::ones(5, 5, CV_8U)*3;


Mat mat2 = Mat::zeros(5, 5, CV_8U);


Mat mat3 = Mat::eye(5, 5, CV_8U);


cout << "mat1=" << mat1 << endl << endl;

cout << "mat2=" << mat2 << endl << endl;

cout << "mat3=" << mat3 << endl;

}




#include <iostream>

#include <opencv2/core/core.hpp>

using namespace cv;

using namespace std;

void main() {

float data[] = {1,2,3,4,5,6,7,8,9};

Mat m1(3, 3, CV_32F, data);


cout << "m1=" << m1 << endl;

}



#include <iostream>

#include <opencv2/core/core.hpp>

using namespace cv;

using namespace std;

void main() {

Mat m1(3, 3, CV_32F, Scalar(5));

cout << "m1=" << m1 << endl;


Mat m2(3, 3, CV_32F);

m2 = Scalar(5);

cout << "m2=" << m2 << endl;


Mat m3 = Mat::ones(3, 3, CV_32F)*5;

cout << "m3=" << m3 << endl;


Mat m4 = Mat::zeros(3, 3, CV_32F)+5;

cout << "m4=" << m4 << endl;


}


#include <iostream>

#include <opencv2/core/core.hpp>

using namespace cv;

using namespace std;

void main() {

Mat mat(3, 2, CV_8UC1);


// 랜덤균일분포,[0,256)

randu(mat, Scalar(0), Scalar(256));

cout << mat << endl << endl;


// 랜덤정규분포,mean=128, stddev=10

randn(mat, Scalar(128), Scalar(10));

cout << mat << endl << endl;  


// RNG 초기화

RNG gen(getTickCount());


// 랜덤균일분포,[0,256)

gen.fill(mat, RNG::UNIFORM, Scalar(0), Scalar(256));

cout << mat << endl << endl;


// 랜덤정규분포,mean=128, stddev=10

gen.fill(mat, RNG::NORMAL, Scalar(128), Scalar(10));

cout << mat << endl << endl;

}


#include <iostream>

#include <opencv2/core/core.hpp>

using namespace cv;

using namespace std;

void main() {

// CV32SC2, 3x3 열

// 초기화+reshape을 이용한 변환

Mat m0 = (Mat_<int>(3,6) << 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18);

Mat m1 = m0.reshape(2);


// 이런 식으로도 할 수 있다.

Vec2i data[] = {Vec2i(1,2), Vec2i(3,4), Vec2i(5,6), Vec2i(7,8), Vec2i(9,10),

Vec2i(11,12), Vec2i(13,14), Vec2i(15,16), Vec2i(17,18)};

Mat m2 =  Mat(3, 3, CV_32SC2, data).clone();

//

Mat m3 = (Mat_<Vec2i>(3,3) <<Vec2i(1,2), Vec2i(3,4), Vec2i(5,6), Vec2i(7,8),

Vec2i(9,10), Vec2i(11,12), Vec2i(13,14), Vec2i(15,16), Vec2i(17,18));


cout << "m1=" << m1 << endl << endl;

cout << "m2=" << m2 << endl << endl;

cout << "m3=" << m3 << endl << endl;

}




#include <iostream>

#include <opencv2/core/core.hpp>

using namespace cv;

using namespace std;

void main() {

Mat m1(3, 4, CV_64FC1);


// 행의수

cout << "rows:" << m1.rows <<endl;

// 열의수

cout << "cols:" << m1.cols << endl;

// 차수

cout << "dims:" << m1.dims << endl;

// 크기(2차원의 경우)

cout << "size[]:" << m1.size().width << "," << m1.size().height << endl;

// 비트 깊이

cout << "depth (ID):" << m1.depth() << "(=" << CV_64F << ")" << endl;

// 채널수

cout << "channels:" << m1.channels() << endl;

// 1요소의 크기[바이트]

cout << "elemSize:" << m1.elemSize() << "[byte]" << endl;

// 1요소의 1채널분의 크기[바이트]

cout << "elemSize1 (elemSize/channels):" << m1.elemSize1() << "[byte]" << endl;

// total

cout << "total:" << m1.total() << endl;

// 스텝수[바이트]

cout << "step:" << m1.step << "[byte]" << endl;

// 1단계의 채널 개수

cout << "step1 (step/elemSize1):" << m1.step1()  << endl;

// 데이터는 연속이가?

cout << "isContinuous:" << (m1.isContinuous()?"true":"false") << endl;

// 데이터가 잘렸는가?

cout << "isSubmatrix:" << (m1.isSubmatrix()?"true":"false") << endl;

// 데이터는 비어있나?

cout << "empty:" << (m1.empty()?"true":"false") << endl;

}



#include <iostream>

#include <opencv2/core/core.hpp>

using namespace cv;

using namespace std;

void main() {

Mat m1(4, 5, CV_32FC(5));  //5x4

Rect roi_rect(0, 0, 3, 4); //4x3

Mat r1(m1, roi_rect);


// 행의수

cout << "rows:" << r1.rows <<endl;

// 열의수

cout << "cols:" << r1.cols << endl;

// 차수

cout << "dims:" << r1.dims << endl;

// 크기(2차원의 경우)

cout << "size[]:" << r1.size().width << "," << r1.size().height << endl;

// 비트 깊이

cout << "depth (ID):" << r1.depth() << "(=" << CV_32F << ")" << endl;

// 채널수

cout << "channels:" << r1.channels() << endl;

//  1요소의 크기[바이트]

cout << "elemSize:" << r1.elemSize() << "[byte]" << endl;

// 1요소의 1채널분의 크기[바이트]

cout << "elemSize1 (elemSize/channels):" << r1.elemSize1() << "[byte]" << endl;

// total

cout << "total:" << r1.total() << endl;

// 스텝수[바이트]

cout << "step:" << r1.step << "[byte]" << endl;

// 1단계의 채널 개수

cout << "step1 (step/elemSize1):" << r1.step1()  << endl;

//  데이터는 연속이가?

cout << "isContinuous:" << (r1.isContinuous()?"true":"false") << endl;

// 데이터가 잘렸는가?

cout << "isSubmatrix:" << (r1.isSubmatrix()?"true":"false") << endl;

// 데이터는 비어있나?

cout << "empty:" << (r1.empty()?"true":"false") << endl;

}




참조:

http://book.mycom.co.jp/support/pc/opencv2/c3/opencv_mat.html

반응형

프로그램 실행시간 측정

OpenCV/example 2013. 5. 20. 22:25

OpenCV


int64 startTime = getTickCount(); double frequency = getTickFrequency();

//처리할 프로그램 함수()

int64 endTime = getTickCount(); cout << (endTime - startTime) / frequency;


int64 t,tinit;

tinit = getTickCount();

//처리 함수()

t = getTickCount() - tinit;

반응형

[OpenCV]카메라 영상에서 특정값 검출(HSV컬러모델)

OpenCV/example 2013. 5. 10. 10:13

 

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

using namespace std;
using namespace cv;

 

int main()
{

VideoCapture camera(CV_CAP_ANY);
Mat input;
Mat output(Size(input.size().height,input.size().width),input.type());
Mat img_thresh(Size(640,480),input.type());

 

namedWindow("input",0);
namedWindow("output",0);

namedWindow("threshold",0);

 

Scalar hsv_min = cvScalar(70 , 100, 100, 0);

Scalar hsv_max = cvScalar(130, 255, 255, 0);

for(;;)
{

camera >> input;

cvtColor(input,output,CV_BGR2HSV,1);
inRange(output,hsv_min,hsv_max,img_thresh); 

imshow("input",input);
imshow("output",output);
imshow("threshold",img_thresh);

waitKey(30);    

}

 return 0;
}

반응형