본문 바로가기
Open CV

[OPENCV-C++ ] 형태학 기반 영상처리 Erosion 침식

by TYB 2023. 11. 26.
반응형

 

 

 

 

#pragma once

#include "ISP.h"

int main()
{
	//const int data_w = 5;
	//const int data_h = 5;
	//uchar Datas[data_w * data_h] = {
	//255,255,255,255,255,
	//255,255,255,255,255,
	//255,255,255,255,255,
	//255,255,255,255,255,
	//255,255,255,255,255
	//};

	const int data_w = 7;
	const int data_h = 7;
	uchar Datas[data_w * data_h] = {
	000,000,000,000,000,000,000,
	000,255,255,255,000,000,000,
	000,255,255,255,255,255,000,
	000,255,000,255,255,255,000,
	000,255,255,255,255,000,000,
	000,255,255,255,000,000,000,
	000,000,000,000,000,000,000
	};

	//uchar Datas[data_w * data_h] = {
	//	1,1,0,1,1,
	//	1,1,0,1,1,
	//	0,0,0,0,0,
	//	1,1,0,1,1,
	//	1,1,0,1,1
	//};
	//uchar Datas[data_w * data_h] = {
	//0,0,1,0,0,
	//0,0,1,0,0,
	//1,1,1,1,1,
	//0,0,1,0,0,
	//0,0,1,0,0
	//};
	Mat img_Datas = Mat(data_h, data_w, CV_8UC1, Datas);
	Mat img_Datas_erosion = Mat::zeros(data_h, data_w, CV_8UC1);

	//침식(erosion)
	const int SE_sz = 3; int SE_half_sz = SE_sz / 2;
	int SE_Cross[SE_sz * SE_sz] = {
		0,255,0,
		255,255,255,
		0,255,0 };
	int SE_Rect[SE_sz * SE_sz] = {
		255,255,255,
		255,255,255,
		255,255,255 };
	//full image searching
	uchar* pDst = img_Datas_erosion.data;
	for (size_t row = SE_half_sz; row < data_h - SE_half_sz; row++) {
		for (size_t col = SE_half_sz; col < data_w - SE_half_sz; col++) {
			//struct element
			bool allone = true;
			for (int h = -SE_half_sz; h <= SE_half_sz; h++) {
				for (int w = -SE_half_sz; w <= SE_half_sz; w++) {
					int index = (row + h) * data_w + (col + w);
					int index_SE = (h + SE_half_sz) * SE_sz + (w + SE_half_sz);
					uchar input = Datas[index];
					//input == 0 : background
					bool condition = (input == 0 && SE_Cross[index_SE] == 255);
					if (condition == true)
					{
						allone = false;
						break;
					}
				}
				if (!allone) break;
			}
			if (allone)
				pDst[row * data_w + col] = 255;
			else
				pDst[row * data_w + col] = 0;
		}
	}
	return 1;
}

 

 

 

 

 

 

 

 

 

cross filter 가 들어갈 수 있는 위치만 남기고 다 없애주는거

 

위 코드는 사진을 직접 만든 거고 사진을 받아오는 걸로 만들어주면

 

 

#pragma once

#include "ISP.h"

int main()
{

	std::string fileName = "../KCCImageNet/images/circle_plate.png";
	cv::Mat src_gray = cv::imread(fileName, cv::ImreadModes::IMREAD_GRAYSCALE);

	cv::Mat src_bin;
	cv::threshold(src_gray, src_bin, 150, 255, ThresholdTypes::THRESH_BINARY);

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


	Mat src_erosion = Mat::zeros(height, width, CV_8UC1);

	//침식(erosion)
	const int SE_sz = 5; int SE_half_sz = SE_sz / 2;
	int SE_Cross[SE_sz * SE_sz] = {
		0,0,255,0,0,
		0,255,255,255,0,
		255,255,255,255,255,
		0,255,255,255,0,
		0,0,255,0,0,
	};
	int SE_Rect[SE_sz * SE_sz] = {
		255,255,255,
		255,255,255,
		255,255,255 };
	//full image searching
	uchar* pDst = src_erosion.data;
	for (size_t row = SE_half_sz; row < height - SE_half_sz; row++) {
		for (size_t col = SE_half_sz; col < width - SE_half_sz; col++) {
			//struct element
			bool allone = true;
			for (int h = -SE_half_sz; h <= SE_half_sz; h++) {
				for (int w = -SE_half_sz; w <= SE_half_sz; w++) {
					int index = (row + h) * width + (col + w);
					int index_SE = (h + SE_half_sz) * SE_sz + (w + SE_half_sz);
					uchar input = pData[index];
					//input == 0 : background
					bool condition = (input == 0 && SE_Cross[index_SE] == 255);
					if (condition == true)
					{
						allone = false;
						break;
					}
				}
				if (!allone) break;
			}
			if (allone)
				pDst[row * width + col] = 255;
			else
				pDst[row * width + col] = 0;
		}
	}
	return 1;
}

 

 

 

 

 

 

반응형