Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void lab3(){
- char fname[MAX_PATH];
- int hue_mean = 16;
- int hue_std = 5;
- while (openFileDlg(fname))
- {
- Mat rgb, hsv;
- rgb = imread(fname, CV_LOAD_IMAGE_COLOR);
- GaussianBlur(rgb, rgb, Size(5, 5), 0, 0);
- int r = rgb.rows;
- int c = rgb.cols;
- cvtColor(rgb, hsv, CV_BGR2HSV);
- Mat channels[3];
- split(hsv, channels);
- Mat dst(r, c, CV_8UC1);
- Mat H = channels[0].clone() * 255 / 180;
- for (int i = 0; i < r; i++)
- {
- for (int j = 0; j < c; j++)
- {
- if ((H.at<uchar>(i, j) > (hue_mean - 2.5 * hue_std)) && ((H.at<uchar>(i, j) < (hue_mean + 2.5 * hue_std))))
- {
- dst.at<uchar>(i, j) = 255;
- }
- else
- dst.at<uchar>(i, j) = 0;
- }
- }
- // x=0 marginea stanga a imaginii - o numim y0
- Mat element1 = getStructuringElement(MORPH_RECT, Size(3, 3));
- imshow("src", rgb);
- erode(dst, dst, element1, Point(-1, -1), 2);
- dilate(dst, dst, element1, Point(-1, -1), 4);
- erode(dst, dst, element1, Point(-1, -1), 2);
- imshow("dst", dst);
- //Labeling("contur", dst, false);
- //line(dst, p1, p2, Scalar(255,255,255), 8, 8);
- MyLabeling("contur cu linie", dst, false);
- imshow("linie", dst);
- waitKey();
- }
- }
- //lab4------------------------------------------------------
- /*
- void regionCallback(int event, int x, int y, int flags, void* userdata)
- {
- Mat* H = (Mat*)userdata;
- if (event == CV_EVENT_LBUTTONDOWN)
- {
- int height = (*H).rows;
- int width = (*H).cols;
- Mat labels = Mat::zeros((*H).size(), CV_16UC1);
- Mat dst = Mat::zeros((*H).size(), CV_16UC1);
- queue <Point> que;
- int k = 1;
- int N = 1;
- double hue_avg = (*H).at<uchar>(y,x);
- que.push(Point(y, x));
- labels.at<uchar>(y, x) = k;
- for (int i = x - 1; i <= x + 1; i++){
- for (int j = y - 1; j <= y + 1; j++){
- if (i >= 0 && i <= (*H).rows && j >= 0 && j <= (*H).cols){
- hue_avg += (*H).at<uchar>(i, j);
- }
- }
- }
- hue_avg = hue_avg / 9;
- int T = 10;
- while (!que.empty()){
- Point oldest = que.front();
- que.pop();
- int xx = oldest.y;
- int yy = oldest.x;
- 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>(yy + i, xx + j) == 0 && abs((*H).at<uchar>(yy + i, xx + j) - hue_avg) < T){
- que.push(Point(yy + i, xx + j));
- labels.at<uchar>(yy + i, xx + j) = k;
- hue_avg = (N*hue_avg + ((*H).at<uchar>(yy + i, xx + j)) / (N + 1));
- N++;
- }
- }
- }
- }
- }
- 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;
- }
- }
- }
- imshow("dst", dst);
- waitKey();
- //imshow(dst)
- }
- }
- void regionGrowing(){
- Mat src;
- // Read image from file
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- src = imread(fname);
- GaussianBlur(src, src, Size(5, 5), 0, 0);
- Mat hsvImg = src.clone();
- cvtColor(src, hsvImg, CV_BGR2Luv);
- Mat channels[3];
- split(hsvImg, channels);
- int height = src.rows;
- int width = src.cols;
- for (int i = 0; i < height; i++){
- for (int j = 0; j < width; j++){
- channels[0].at<uchar>(i, j) = channels[0].at<uchar>(i, j) * 255 / 180;
- }
- }
- int hH[256] = { 0 };
- for (int i = 0; i < height; i++){
- for (int j = 0; j < width; j++){
- hH[channels[0].at<uchar>(i, j)]++;
- }
- }
- Mat src2 = channels[1];
- namedWindow("src2", 1);
- setMouseCallback("regionGrowing", CallBackL4, &src2);
- imshow("src2", src2);
- waitKey(0);
- }
- }
- */
- void CallBackL4(int event, int x, int y, int flags, void *userdata)
- {
- Mat* H = (Mat*)userdata;
- if (event == EVENT_RBUTTONDOWN)
- {
- imshow("H", *H);
- int width = (*H).cols;
- int height = (*H).rows;
- Mat labels = Mat::zeros((*H).size(), CV_8U);
- queue <Point> que;
- double hue_avg = (*H).at<uchar>(y, x);
- int T = 15;
- int k = 1;
- int N = 1;
- que.push(Point(x,y));
- labels.at<uchar>(y, x) = k;
- while (!que.empty())
- {
- Point oldest = que.front();
- que.pop();
- int xx = oldest.x;
- int yy = oldest.y;
- for (int i = yy - 1; i <= yy + 1; i++)
- for (int j = xx - 1; j <= xx + 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(j, i));
- 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 < height; i++)
- for (int j = 0; j < width; j++)
- {
- if (labels.at<uchar>(i, j) != 0) {
- dst.at<uchar>(i, j) = 255;
- }
- }
- imshow("dst", dst);
- }
- }
- void regionGrowing()
- {
- Mat src;
- Mat hsv, channels[3], H;
- char fname[MAX_PATH];
- while (openFileDlg(fname))
- {
- src = imread(fname);
- GaussianBlur(src, src, Size(5, 5), 0, 0);
- cvtColor(src, hsv, CV_BGR2HSV);
- split(hsv, channels);
- H = channels[0] * 510 / 360;
- namedWindow("src", 1);
- setMouseCallback("src", CallBackL4, &H);
- imshow("src", src);
- waitKey(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement