Skip to content

Instantly share code, notes, and snippets.

@codeOfRobin
Created May 22, 2015 13:40
Show Gist options
  • Save codeOfRobin/6936da0161d90df34d28 to your computer and use it in GitHub Desktop.
Save codeOfRobin/6936da0161d90df34d28 to your computer and use it in GitHub Desktop.
int const max_BINARY_value = 2147483647;
cv::Mat src_gray=[self cvMatFromUIImage:img.image];
cv::Mat dst;
dst=src_gray;
cv::cvtColor(src_gray, dst, cv::COLOR_RGB2GRAY);
cv::Mat canny_output;
std::vector<std::vector<cv::Point> > contours;
std::vector<cv::Vec4i> hierarchy;
cv::RNG rng(12345);
cv::threshold( dst, dst, threshold_value, max_BINARY_value,cv::THRESH_OTSU );
cv::Mat contourOutput = dst.clone();
cv::findContours( contourOutput, contours, cv::RETR_LIST, cv::CHAIN_APPROX_NONE );
//Draw the contours
cv::Mat contourImage(dst.size(), CV_8UC3, cv::Scalar(0,0,0));
cv::Scalar colors[3];
colors[0] = cv::Scalar(255, 0, 0);
colors[1] = cv::Scalar(0, 255, 0);
colors[2] = cv::Scalar(0, 0, 255);
for (size_t idx = 0; idx < contours.size(); idx++) {
cv::drawContours(contourImage, contours, idx, colors[idx % 3]);
}
[img setImage:[self UIImageFromCVMat:dst]];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment