OpenCV 色抽出,マスク作成,膨張収縮 | i-mk2!

i-mk2!

自分のための備忘録
OpenCVの使い方とか.

//-- color extruction ------------------------------------------------------//
if(1)
{
cv::Mat channels[3], hue_temp, hue, saturation, value, binary_temp;

cv::cvtColor(img_color, hsv, CV_BGR2HSV);
cv::split(hsv, channels);
cv::threshold(channels[0], hue_temp , 160 , 255, CV_THRESH_TOZERO);
cv::threshold(channels[0], hue , 20 , 255, CV_THRESH_TOZERO_INV);
cv::threshold(channels[1], saturation , 50 , 255, CV_THRESH_BINARY);
cv::threshold(channels[2], value , 130 , 255, CV_THRESH_BINARY);

cv::bitwise_or(hue, hue_temp, hue); // "or" of over red and under red
cv::bitwise_and(hue, saturation, binary_temp); // "and" of red and saturation
cv::bitwise_and(binary_temp, value, binary); // "and" of above and value
cv::threshold(binary, binary, 0, 255, CV_THRESH_BINARY); // thresholding to make mask

// dilate and erode for stability
cv::dilate(binary, binary, cv::Mat(), cv::Point(-1,-1), 3);
cv::erode (binary, binary, cv::Mat(), cv::Point(-1,-1), 4)

//cv::imshow("obj_binary",binary);
//mask_ce = binary;
}
//--------------------------------------------------------------------------//