OpenCV(Cpp) KeyboardLabeling - minho0315/OpenCV GitHub Wiki

  1. 이미지 자르기
  2. gray
  3. 이진화
  4. 라벨링
  5. 크기 제한 설정

실습코드

#include <opencv2/opencv.hpp>

#include <windows.h>

using namespace cv;
using namespace std;

int main(int ac, char** av)
{
	Mat img = imread("keyboard.png");

	Mat img_resize = img(Range(300, 1200), Range(300, 1200)); //이미지 자르기

	Mat img_gray;
 	cvtColor(img_resize, img_gray, COLOR_BGR2GRAY); //gray

	Mat img_threshold;
	threshold(img_gray, img_threshold, 100, 255, THRESH_BINARY_INV); //이진화

	Mat img_labels, stats, centroids;
	int numOfLables = connectedComponentsWithStats(img_threshold, img_labels, stats, centroids, 8, CV_32S); //labeling

	int num = 1; // labeling 숫자
	// 레이블링 결과에 사각형 그리고, 넘버 표시하기
	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);

		if (area > 40000) { //라벨링 면적 확인

			rectangle(img_resize, Point(left, top), Point(left + width, top + height),
				Scalar(0, 0, 255), 1);

			putText(img_resize, to_string(num++), Point(left + 20, top + 20), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 0, 0), 1);
		}
	}

	imshow("img_gray", img_gray);
	imshow("img_threshold", img_threshold);
	imshow("img_resize", img_resize);

	waitKey(0);

	return 0;
}

결과화면

gray

1


이진화

2


라벨링

3



참고

https://diyver.tistory.com/139?category=911404

⚠️ **GitHub.com Fallback** ⚠️