컬러 추출후 해당 컬러 보여주기2

OpenCV/my_source 2013. 9. 3. 16:51


//-------------------------------------------------------------

// 메인

//-------------------------------------------------------------

void main() {

char * wnSrc = "src";

//Mat src = imread("resizesample.tif");

Mat src = imread("조명.jpg");

//Mat src = imread("ColorCheckerChart.png");

if(!src.data) exit(1);

//fun2(src);

imshow(wnSrc,src);

cvSetMouseCallback(wnSrc, onMouse, &src);

waitKey(0);

}



//-------------------------------------------------------------

// 마우스 이벤트

//-------------------------------------------------------------

void onMouse(int event, int x, int y, int flags, void* param) {


//Point3i rgb;

Mat *src = (Mat *)param;

static vector<Point3i> pt;


switch(event) {

case CV_EVENT_RBUTTONDOWN: //오른쪽버튼 DOWN

pt.push_back(Point3i(src->at<Vec3b>(y,x)[0], src->at<Vec3b>(y,x)[1], src->at<Vec3b>(y,x)[2]));


break;

case CV_EVENT_LBUTTONDOWN: //오른쪽버튼 DOWN

draw(*src, pt);

pt.clear();

break;

}


imshow("src",*src);

}


//-------------------------------------------------------------

// 컬러 그리기

//-------------------------------------------------------------

void draw(Mat &src, vector<Point3i> range) {

int gap = 5;


Mat temp = Mat(src.size(), src.type(),Scalar(0,255,0));

for(int i = 0; i< src.rows; i++) {

for(int j = 0; j< src.cols; j++){

Vec3b col = src.at<Vec3b>(i,j);

for(int k = 0; k<range.size(); k++) {

if( ( col[0] >= range[k].x - gap && col[0] <= range[k].x + gap) && 

( col[1] >= range[k].y - gap && col[1] <= range[k].y + gap) && 

( col[2] >= range[k].z - gap && col[2] <= range[k].z + gap) )

circle(temp,Point(j,i),1,Scalar(range[k].x,range[k].y,range[k].z),2);

}

}

}

imshow("temp",temp);

}


반응형

'OpenCV > my_source' 카테고리의 다른 글

컬러 추출후 해당 컬러 보여주기  (0) 2013.09.03
[콘솔]마우스 이벤트  (0) 2013.08.20
컬러공간 변환  (0) 2013.05.21