#python
import cv2
import numpy as np
def labeling_basic():
data = np.array([
[0, 0, 1, 1, 0, 0, 0, 0],
[1, 1, 1, 1, 0, 0, 1, 0],
[1, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 0],
[0, 0, 0, 1, 1, 1, 1, 0],
[0, 0, 0, 1, 0, 0, 1, 0],
[0, 0, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0]
], dtype=np.uint8) * 255
src = data.astype(np.uint8)
cnt, labels = cv2.connectedComponents(src)
print("src:\n", src)
print("labels:\n", labels)
print("number of labels:", cnt)
def labeling_stats():
src = cv2.imread("D:\\projects\\SampleCode\\006939-master\\ch12\\labeling\\keyboard.bmp", cv2.IMREAD_GRAYSCALE)
if src is None:
print("Image load failed!")
return
_, bin = cv2.threshold(src, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
cnt, labels, stats, centroids = cv2.connectedComponentsWithStats(bin)
dst = cv2.cvtColor(src, cv2.COLOR_GRAY2BGR)
for i in range(1, cnt):
if stats[i, cv2.CC_STAT_AREA] < 20:
continue
x, y, w, h = stats[i, cv2.CC_STAT_LEFT], stats[i, cv2.CC_STAT_TOP], stats[i, cv2.CC_STAT_WIDTH], stats[i, cv2.CC_STAT_HEIGHT]
cv2.rectangle(dst, (x, y), (x + w, y + h), (0, 255, 255), 2)
cv2.imshow("src", src)
cv2.imshow("dst", dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
def main():
labeling_basic()
labeling_stats()
if __name__ == "__main__":
main()
src:
[[ 0 0 255 255 0 0 0 0]
[255 255 255 255 0 0 255 0]
[255 255 255 255 0 0 0 0]
[ 0 0 0 0 0 255 255 0]
[ 0 0 0 255 255 255 255 0]
[ 0 0 0 255 0 0 255 0]
[ 0 0 255 255 255 255 255 0]
[ 0 0 0 0 0 0 0 0]]
labels:
[[0 0 1 1 0 0 0 0]
[1 1 1 1 0 0 2 0]
[1 1 1 1 0 0 0 0]
[0 0 0 0 0 3 3 0]
[0 0 0 3 3 3 3 0]
[0 0 0 3 0 0 3 0]
[0 0 3 3 3 3 3 0]
[0 0 0 0 0 0 0 0]]
number of labels: 4
// C++
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
using namespace std;
void labeling_basic();
void labeling_stats();
int main()
{
labeling_basic();
labeling_stats();
return 0;
}
void labeling_basic()
{
uchar data[] = {
0, 0, 1, 1, 0, 0, 0, 0,
1, 1, 1, 1, 0, 0, 1, 0,
1, 1, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 1, 0,
0, 0, 0, 1, 1, 1, 1, 0,
0, 0, 0, 1, 0, 0, 1, 0,
0, 0, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0,
};
Mat src = Mat(8, 8, CV_8UC1, data) * 255;
Mat labels;
int cnt = connectedComponents(src, labels);
cout << "src:\n" << src << endl;
cout << "labels:\n" << labels << endl;
cout << "number of labels: " << cnt << endl;
}
void labeling_stats()
{
Mat src = imread("keyboard.bmp", IMREAD_GRAYSCALE);
if (src.empty()) {
cerr << "Image load failed!" << endl;
return;
}
Mat bin;
threshold(src, bin, 0, 255, THRESH_BINARY | THRESH_OTSU);
Mat labels, stats, centroids;
int cnt = connectedComponentsWithStats(bin, labels, stats, centroids);
Mat dst;
cvtColor(src, dst, COLOR_GRAY2BGR);
for (int i = 1; i < cnt; i++) {
int* p = stats.ptr<int>(i);
if (p[4] < 20) continue;
rectangle(dst, Rect(p[0], p[1], p[2], p[3]), Scalar(0, 255, 255));
}
imshow("src", src);
imshow("dst", dst);
waitKey();
destroyAllWindows();
}
C++ Code 출처 : OpenCV 4로 배우는 컴퓨터 비전과 머신 러닝 - 황선규 저
'OpenCV' 카테고리의 다른 글
ch13 cascade (0) | 2024.05.21 |
---|---|
ch12 polygon (0) | 2024.05.21 |
ch12 findcts (0) | 2024.05.21 |
ch11 threshold (0) | 2024.05.21 |
ch11 morphology (0) | 2024.05.21 |