[Intel AI SW 아카데미] 영상처리 - 영상 특징 검출과 표현

Jimeaning·2023년 11월 16일
0

Intel AIoT

목록 보기
16/38

23.11.14 (화) 39일차

영상 특징 검출과 표현

영상 이진화

// ISP_ShapeInfo.cpp

#pragma once

#include "ISP.h"

int main()
{
	std::string fileName = "../KCCImageNet/shapes.jpg";
	cv::Mat src_gray = cv::imread(fileName, cv::ImreadModes::IMREAD_GRAYSCALE);

	uchar* pData = src_gray.data;
	size_t width = src_gray.cols;
	size_t height = src_gray.rows;

	Mat src_bin = Mat::zeros(Size(width, height), CV_8UC1);
	Mat src_obj = Mat::zeros(Size(width, height), CV_8UC1);
	uchar* pDataBin = src_bin.data;

	int threshold_min = 60;	// 0 ~ 255
	int threshold_max = 200;
	// 이진화, Binary
	for (size_t i = 0; i < width * height; i++)
	{
		int value = pData[i];

		/*if (value > threshold_min && value < threshold_max)
			pDataBin[i] = 0;
		else
			pDataBin[i] = 255;*/
		(value > threshold_max) ? pDataBin[i] = 0 : pDataBin[i] = 255;
	}
	src_obj = src_bin & src_gray;

	return 1;
}

윤곽선 검출

#pragma once

#include "ISP.h"

int main()
{
	std::string fileName = "../KCCImageNet/shapes.jpg";
	cv::Mat src_gray = cv::imread(fileName, cv::ImreadModes::IMREAD_GRAYSCALE);

	uchar* pData = src_gray.data;
	size_t width = src_gray.cols;
	size_t height = src_gray.rows;

	Mat src_bin = Mat::zeros(Size(width, height), CV_8UC1);
	Mat src_obj = Mat::zeros(Size(width, height), CV_8UC1);
	uchar* pDataBin = src_bin.data;

	int threshold_min = 60;	// 0 ~ 255
	int threshold_max = 200;
	// 이진화, Binary
	for (size_t i = 0; i < width * height; i++)
	{
		int value = pData[i];

		/*if (value > threshold_min && value < threshold_max)
			pDataBin[i] = 0;
		else
			pDataBin[i] = 255;*/
		(value > threshold_max) ? pDataBin[i] = 0 : pDataBin[i] = 255;
	}
	src_obj = src_bin & src_gray;

	RNG rng(12345);
	vector<vector<Point> > contours;
	vector<Vec4i> hierarchy;
	findContours(src_bin, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
	Mat drawing = Mat::zeros(src_bin.size(), CV_8UC3);
	for (size_t i = 0; i < contours.size(); i++)
	{
		Scalar color = Scalar(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256));
		drawContours(drawing, contours, (int)i, color, 2, LINE_8, hierarchy, 0);
	}
    
    return 1;
}

CoG (무게중심) 찾기

#pragma once

#include "ISP.h"

int main()
{
	std::string fileName = "../KCCImageNet/shapes.jpg";
	cv::Mat src_gray = cv::imread(fileName, cv::ImreadModes::IMREAD_GRAYSCALE);

	uchar* pData = src_gray.data;
	size_t width = src_gray.cols;
	size_t height = src_gray.rows;

	Mat src_bin = Mat::zeros(Size(width, height), CV_8UC1);
	Mat src_obj = Mat::zeros(Size(width, height), CV_8UC1);
	uchar* pDataBin = src_bin.data;

	int threshold_min = 60;	// 0 ~ 255
	int threshold_max = 200;
	// 이진화, Binary
	for (size_t i = 0; i < width * height; i++)
	{
		int value = pData[i];

		/*if (value > threshold_min && value < threshold_max)
			pDataBin[i] = 0;
		else
			pDataBin[i] = 255;*/
		(value > threshold_max) ? pDataBin[i] = 0 : pDataBin[i] = 255;
	}
	src_obj = src_bin & src_gray;

	RNG rng(12345);
	vector<vector<Point> > contours;
	vector<Vec4i> hierarchy;
	findContours(src_bin, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
	Mat drawing = Mat::zeros(src_bin.size(), CV_8UC3);
	for (size_t i = 0; i < contours.size(); i++)
	{
		Scalar color = Scalar(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256));
		drawContours(drawing, contours, (int)i, color, 2, LINE_8, hierarchy, 0);
	}

	for (size_t i = 0; i < contours.size(); i++)
	{
		cout << "객체 " << i+1 << "의 Point 개수 : " << contours[i].size() << endl;
		int CoGx, CoGy;
		CoGx, CoGy = 0, 0;
		int accX = 0, accY = 0;
		int length = contours[i].size();
		for (size_t n = 0; n < length; n++)
		{
			accX += contours[i].at(n).x;
			accY += contours[i].at(n).y;
		}
		CoGx = accX / length;
		CoGy = accY / length;
		cout << "Object[" << i + 1 << "] CoG.x = " << CoGx << " CoG.y = " << CoGy << endl;

		line(drawing, Point(CoGx - 10, CoGy - 10), Point(CoGx + 10, CoGy + 10), CV_RGB(255, 0, 0));
		line(drawing, Point(CoGx + 10, CoGy - 10), Point(CoGx - 10, CoGy + 10), CV_RGB(255, 0, 0));
	}

	return 1;
}

두껍게 라인을 그리고 싶다면 ..

line(drawing, Point(CoGx - 10, CoGy - 10), Point(CoGx + 10, CoGy + 10), CV_RGB(255, 0, 0), 10);
line(drawing, Point(CoGx + 10, CoGy - 10), Point(CoGx - 10, CoGy + 10), CV_RGB(255, 0, 0), 10);

외곽 사각형 그리기

#pragma once

#include "ISP.h"

int main()
{
	std::string fileName = "../KCCImageNet/shapes.jpg";
	cv::Mat src_gray = cv::imread(fileName, cv::ImreadModes::IMREAD_GRAYSCALE);

	uchar* pData = src_gray.data;
	size_t width = src_gray.cols;
	size_t height = src_gray.rows;

	Mat src_bin = Mat::zeros(Size(width, height), CV_8UC1);
	Mat src_obj = Mat::zeros(Size(width, height), CV_8UC1);
	uchar* pDataBin = src_bin.data;

	int threshold_min = 60;	// 0 ~ 255
	int threshold_max = 200;
	// 이진화, Binary
	for (size_t i = 0; i < width * height; i++)
	{
		int value = pData[i];

		/*if (value > threshold_min && value < threshold_max)
			pDataBin[i] = 0;
		else
			pDataBin[i] = 255;*/
		(value > threshold_max) ? pDataBin[i] = 0 : pDataBin[i] = 255;
	}
	src_obj = src_bin & src_gray;

	RNG rng(12345);
	vector<vector<Point> > contours;
	vector<Vec4i> hierarchy;
	findContours(src_bin, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
	Mat drawing = Mat::zeros(src_bin.size(), CV_8UC3);
	for (size_t i = 0; i < contours.size(); i++)
	{
		Scalar color = Scalar(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256));
		drawContours(drawing, contours, (int)i, color, 2, LINE_8, hierarchy, 0);
	}

	Mat src_color;
	cv::cvtColor(src_gray, src_color, ColorConversionCodes::COLOR_GRAY2BGR);
	for (size_t i = 0; i < contours.size(); i++)
	{
		cout << "객체 " << i+1 << "의 Point 개수 : " << contours[i].size() << endl;
		int CoGx, CoGy;
		CoGx, CoGy = 0, 0;
		int accX = 0, accY = 0;
		int length = contours[i].size();
		int x_min = width, x_max = 0, y_min = height, y_max = 0;
		for (size_t n = 0; n < length; n++)
		{
			accX += contours[i].at(n).x;
			accY += contours[i].at(n).y;

			if (x_min > contours[i].at(n).x)
				x_min = contours[i].at(n).x;
			if (x_max < contours[i].at(n).x)
				x_max = contours[i].at(n).x;
			if (y_min > contours[i].at(n).y)
				y_min = contours[i].at(n).y;
			if (y_max < contours[i].at(n).y)
				y_max = contours[i].at(n).y;
		}
		CoGx = accX / length;
		CoGy = accY / length;
		//cout << "Object[" << i + 1 << "] CoG.x = " << CoGx << " CoG.y = " << CoGy << endl;

		line(drawing, Point(CoGx - 10, CoGy - 10), Point(CoGx + 10, CoGy + 10), CV_RGB(255, 0, 0), 10);
		line(drawing, Point(CoGx + 10, CoGy - 10), Point(CoGx - 10, CoGy + 10), CV_RGB(255, 0, 0), 10);

		line(src_color, Point(CoGx - 10, CoGy - 10), Point(CoGx + 10, CoGy + 10), CV_RGB(255, 0, 0), 10);
		line(src_color, Point(CoGx + 10, CoGy - 10), Point(CoGx - 10, CoGy + 10), CV_RGB(255, 0, 0), 10);

		const int ptSz = 4;
		Point pt[ptSz];
		pt[0].x = x_min; pt[0].y = y_min;
		pt[1].x = x_max; pt[1].y = y_min;
		pt[2].x = x_max; pt[2].y = y_max;
		pt[3].x = x_min; pt[3].y = y_max;
		for (size_t i = 0; i < ptSz; i++)
		{
			line(src_color, pt[i % ptSz], pt[(i + 1) % ptSz], CV_RGB(255, 0, 0));
		}
	}

	return 1;
}

OpenCV 라이브러리 활용 코드

double area = contourArea(contours[i]);
RotatedRect rrt = minAreaRect(contours[i]);
double length = arcLength(contours[i], true);
profile
I mean

0개의 댓글