검색결과 리스트
글
Image Segmentation with Distance Transform and Watershed Algorithm
컴퓨터비전/영상처리/OpenCV
2015. 7. 13. 02:36
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | #include <cv.hpp> #include <iostream> using namespace std; using namespace cv; int DELAY_CAPTION = 1500; char window_name[] = "Watershed Demo"; Mat src; int display_caption(char *caption, Mat input); int main() { src = imread("img/card2.png"); if (!src.data) return -1; display_caption("source Image", src); for (int x = 0; x < src.rows; x++) { for (int y = 0; y < src.cols; y++) { if (src.at<Vec3b>(x, y) == Vec3b(255, 255, 255)) { src.at<Vec3b>(x, y)[0] = 0; src.at<Vec3b>(x, y)[1] = 0; src.at<Vec3b>(x, y)[2] = 0; } } } Mat kernel = (Mat_<float>(3,3) << 1, 1, 1, 1, -8, 1, 1, 1, 1); Mat imgLaplacian; Mat sharp = src; filter2D(sharp, imgLaplacian, CV_32F, kernel); src.convertTo(sharp, CV_32F); Mat imgResult = sharp - imgLaplacian; imgResult.convertTo(imgResult, CV_8UC3); imgLaplacian.convertTo(imgLaplacian, CV_8UC3); display_caption("New Sharped Image", imgResult); display_caption("New Sharped Image Laplacian", imgLaplacian); src = imgResult; Mat bw; cvtColor(src, bw, CV_BGR2GRAY); threshold(bw, bw, 40, 255, CV_THRESH_BINARY | CV_THRESH_OTSU); Mat dist; // CV_DIST_L2 = Euclidean distance distanceTransform(bw, dist, CV_DIST_L2, 3); normalize(dist, dist, 0, 1., NORM_MINMAX); display_caption("Distance Transform Image", dist); threshold(dist, dist, .4, 1., CV_THRESH_BINARY); Mat kernel1 = Mat::ones(3, 3, CV_8UC1); dilate(dist, dist, kernel1); display_caption("peaks point", dist); Mat dist_8U; dist.convertTo(dist_8U, CV_8U); vector<vector<Point>> contours; findContours(dist_8U, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); Mat markers = Mat::zeros(dist.size(), CV_32SC1); for (size_t i = 0; i < contours.size(); i++) drawContours(markers, contours, static_cast<int>(i), Scalar::all(static_cast<int>(i)+1)); circle(markers, Point(5, 5), 3, CV_RGB(255, 255, 255), -1); display_caption("Marker", markers * 10000); watershed(src, markers); Mat mark = Mat::zeros(markers.size(), CV_8UC1); markers.convertTo(mark, CV_8UC1); bitwise_not(mark, mark); display_caption("Markers_v2", mark); vector<Vec3b>colors; for (size_t i = 0; i < contours.size(); i++) { int b = theRNG().uniform(0, 255); int g = theRNG().uniform(0, 255); int r = theRNG().uniform(0, 255); colors.push_back(Vec3b((uchar)b, (uchar)g, (uchar)r)); } Mat dst = Mat::zeros(markers.size(), CV_8UC3); for (int i = 0; i < markers.rows; i++) { for (int j = 0; j < markers.cols; j++) { int index = markers.at<int>(i, j); if (index > 0 && index <= static_cast<int>(contours.size())) dst.at<Vec3b>(i, j) = colors[index - 1]; else dst.at<Vec3b>(i, j) = Vec3b(0, 0, 0); } } display_caption("Final", dst); waitKey(0); return 0; } int display_caption(char *caption, Mat input) { Mat dst; dst = Mat::zeros(input.size(), input.type()); putText(dst, caption, Point(src.cols / 4, src.rows / 2), FONT_HERSHEY_COMPLEX, 1, Scalar(255, 255, 255)); imshow(window_name, dst); int c = waitKey(DELAY_CAPTION); if (c >= 0) return -1; imshow(window_name, input); c = waitKey(DELAY_CAPTION); if (c >= 0) return -1; return 0; } | cs |
'컴퓨터비전/영상처리 > OpenCV' 카테고리의 다른 글
OpenCV Principal Component Analysis(PCA) (0) | 2015.07.19 |
---|---|
OpenCV Scene Change Detection(장면 전환 검출) (0) | 2015.07.19 |
OpenCV Image Filtering (0) | 2015.07.13 |
OpenCV 엠보싱, 수채화, 컬러 스케치 효과 (0) | 2014.11.04 |
사람얼굴을 검출해보자 (OpenCV) (6) | 2014.10.28 |