Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void CallBackL4(int event, int x, int y, int flags, void* param)
- {
- Mat* H = (Mat*)param;
- if (event == CV_EVENT_LBUTTONDOWN)
- {
- Mat labels = Mat::zeros((*H).size(), CV_8U);
- queue <Point> que;
- int k = 1;
- int N = 1;
- //float hue_avg = 1;
- que.push(Point(x, y));
- labels.at<uchar>(y, x) = k;
- int width = (*H).cols;
- int height = (*H).rows;
- int T = 15;
- double hue_avg = (*H).at<uchar>(y, x);
- while (!que.empty()){
- Point oldest = que.front();
- que.pop();
- int xx = oldest.x;
- int yy = oldest.y;
- for (int i = xx - 1; i <= xx + 1; i++){
- for (int j = yy - 1; j <= yy + 1; j++){
- if (i >= 1 && i <= (*H).rows - 1 && j >= 1 && j <= (*H).cols - 1){
- if (labels.at<uchar>(i, j) == 0 && abs((*H).at<uchar>(i, j) - hue_avg) < T){
- que.push(Point(i, j));
- labels.at<uchar>(i, j) = k;
- hue_avg = (N*hue_avg + (*H).at<uchar>(i, j)) / (N + 1);
- N++;
- }
- }
- }
- }
- }
- Mat dst = labels.clone();
- for (int i = 0; i < labels.rows; i++){
- for (int j = 0; j < labels.cols; j++){
- if (labels.at<uchar>(i, j) == 1){
- dst.at<uchar>(i, j) = 255;
- }
- }
- }
- Mat el1 = getStructuringElement(MORPH_CROSS, Size(5, 5));
- erode(dst, dst, el1, Point(-1, -1), 2);
- dilate(dst, dst, el1, Point(-1, -1), 4);
- erode(dst, dst, el1, Point(-1, -1), 2);
- imshow("dst", dst);
- //printf("%d", hue_avg);
- }
- }
- void L4_R6(){
- Mat src;
- Mat hsv;
- // Read image from file
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- src = imread(fname);
- GaussianBlur(src, src, Size(5, 5), 0, 0);
- cvtColor(src, hsv, CV_BGR2HSV);
- Mat channels[3];
- split(hsv, channels);
- Mat Hue;
- Hue = channels[0] * (255 / 180);
- Mat dst(channels[0].rows, channels[0].cols, CV_8UC1);
- namedWindow("src", 1);
- setMouseCallback("src", CallBackL4, &Hue);
- imshow("src", src);
- waitKey();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement