반응형


두번째 예제에 있던 오타 및 오류를 잡았습니다.


테스트에 사용한 이미지입니다.






openCV 3.0부터 라벨링 알고리즘이 추가되었습니다.. 이미지를 라벨링하고 원하는 라벨을 색으로 표현한다든가.. 각각의 영역들을 박스치는 것등이 쉽게되네요.. 해당 영역의 크기도 각각 계산되서 나옵니다.. 자세한건 아래 소스코드를 읽어 보세요...




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
#include <iostream>  
#include <opencv2/core/mat.hpp>  
#include <opencv2/imgcodecs.hpp>  
#include <opencv2/imgproc.hpp>  
#include <opencv2/highgui.hpp>  
  
using namespace cv;  
using namespace std;  
  
int main() {  
    Mat img_gray, img_color, img_binary;  
  
    img_gray = imread("art8.jpg", IMREAD_GRAYSCALE );  
  
    threshold(img_gray, img_binary, 127255, THRESH_BINARY);  
    cvtColor( img_gray, img_color, COLOR_GRAY2BGR );  
  
  
    Mat img_labels,stats, centroids;  
    int numOfLables = connectedComponentsWithStats(img_binary, img_labels,   
                                                   stats, centroids, 8,CV_32S);  
  
  
    //라벨링된 이미지중 특정 라벨을 컬러로 표현해주기  
    for ( int y=0; y<img_labels.rows; ++y ) {  
  
        int *label = img_labels.ptr<int>(y);  
        Vec3b* pixel = img_color.ptr<Vec3b>(y);  
  
  
        for (int x = 0; x < img_labels.cols; ++x) {  
  
  
            if (label[x] == 3 ) {  
                pixel[x][2= 0;  
                pixel[x][1= 255;  
                pixel[x][0= 0;  
            }  
        }  
    }  
  
  
    //라벨링 된 이미지에 각각 직사각형으로 둘러싸기 
    for (int j = 1; j < numOfLables; j++) {  
        int area = stats.at<int>(j, CC_STAT_AREA);  
        int left = stats.at<int>(j, CC_STAT_LEFT);  
        int top  = stats.at<int>(j, CC_STAT_TOP);  
        int width = stats.at<int>(j, CC_STAT_WIDTH);  
        int height  = stats.at<int>(j, CC_STAT_HEIGHT);  
  
  
        rectangle( img_color, Point(left,top), Point(left+width,top+height),  
                   Scalar(0,0,255),1 );  
          
        putText(img_color, to_string(j), Point(left+20,top+20), 
                          FONT_HERSHEY_SIMPLEX, 1, Scalar(255,0,0), 2);  
    }  
  
  
    imshow( "result", img_color );  
    waitKey(0);  
}  
cs




centroids를 이용하여 라벨링 된 영역의 중심을 출력해보았습니다..




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
#include <iostream>  
#include <opencv2/core/mat.hpp>  
#include <opencv2/imgcodecs.hpp>  
#include <opencv2/imgproc.hpp>  
#include <opencv2/highgui.hpp>  
 
using namespace cv;
using namespace std;
 
int main() {
    Mat img_gray, img_color, img_binary;
 
    img_gray = imread("1.png", IMREAD_GRAYSCALE);
 
    threshold(img_gray, img_binary, 127255, THRESH_BINARY);
    cvtColor(img_gray, img_color, COLOR_GRAY2BGR);
 
 
    Mat img_labels, stats, centroids;
    int numOfLables = connectedComponentsWithStats(img_binary, img_labels,
        stats, centroids, 8, CV_32S);
 
 
    //라벨링된 이미지중 특정 라벨을 컬러로 표현해주기  
    for (int y = 0; y<img_labels.rows; ++y) {
 
        int *label = img_labels.ptr<int>(y);
        Vec3b* pixel = img_color.ptr<Vec3b>(y);
 
 
        for (int x = 0; x < img_labels.cols; ++x) {
 
 
            if (label[x] == 3) {
                pixel[x][2= 0;
                pixel[x][1= 255;
                pixel[x][0= 0;
            }
        }
    }
 
 
    //라벨링 된 이미지에 각각 직사각형으로 둘러싸기  
    for (int j = 1; j < numOfLables; j++) {
        int area = stats.at<int>(j, CC_STAT_AREA);
        int left = stats.at<int>(j, CC_STAT_LEFT);
        int top = stats.at<int>(j, CC_STAT_TOP);
        int width = stats.at<int>(j, CC_STAT_WIDTH);
        int height = stats.at<int>(j, CC_STAT_HEIGHT);
 
        int x = centroids.at<double>(j, 0); //중심좌표
        int y = centroids.at<double>(j, 1); 
 
        circle(img_color, Point(x, y), 5, Scalar(25500), 1);
 
        rectangle(img_color, Point(left, top), Point(left + width, top + height),
            Scalar(00255), 1);
 
        putText(img_color, to_string(j), Point(left + 20, top + 20), FONT_HERSHEY_SIMPLEX,
            1, Scalar(25500), 2);
    }
 
 
    imshow("result", img_color);
    waitKey(0);
}
 
cs


문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts