#include <opencv2/opencv.hpp>
#include <cmath>

using namespace cv;
using namespace std;



int main(void)
{
	Mat m = (Mat_<uchar>(3, 4) <<
		11, 13, 17, 26,
		39, 40, 42, 43,
		56, 72, 99, 88);
	cout << m << endl;

	//calcHist(
	// const Mat * images, 
	// int nimages,
	//	const int* channels, 
	// InputArray mask,
	//	SparseMat & hist, 
	// int dims,
	//	const int* histSize, 
	// const float** ranges,
	//	bool uniform = true, 
	//  bool accumulate = false);


	int channels[] = { 0 };
	Mat hist;
	int histSize[] = { 4 };
	float range[] = { 0,101 };
	const float* ranges[] = { range };
	calcHist(
		&m,//원본 이미지 설정
		1,
		channels,      //0번 채널만 계산(gray)
		Mat(),         //Matrix Masking을 하지 않겠다.
		hist,
		1,            //결과 매트리스를 몇차원으로 받을 것인가에 대한 설정
		histSize,
		ranges         //유효 범위를 설정한다(0,256)
	);

	cout << hist.t() << endl;

	waitKey(0);
	return 0;

}

 

int main(void)
{
	Mat m = (Mat_<Vec3b>(3, 4) <<
		Vec3b(10, 10, 40), Vec3b(20, 10, 40), Vec3b(30, 10, 40), Vec3b(40, 10, 40),
		Vec3b(50, 40, 40), Vec3b(60, 40, 40), Vec3b(70, 40, 40), Vec3b(80, 40, 40),
		Vec3b(90, 80, 90), Vec3b(80, 80, 90), Vec3b(70, 80, 90), Vec3b(60, 80, 90)
		);

	cout << m << endl;
	cout << m.channels() << endl;

	// 0 : [2, 3, 4, 3]
	int channels[] = { 0 };
	Mat hist;

	// 0 부터 100사이를 4 간격으로
	int histSize[] = { 4 };
	float range[] = { 0,101 };
	const float* ranges[] = { range };
	calcHist(
		&m,//원본 이미지 설정
		1,
		channels,      //0번 채널만 계산(gray)
		Mat(),         //Matrix Masking을 하지 않겠다.
		hist,
		1,            //결과 매트리스를 몇차원으로 받을 것인가에 대한 설정
		histSize,
		ranges         //유효 범위를 설정한다(0,256)
	);

	cout << hist.t() << endl;

	waitKey(0);
	return 0;

}

 

normalize()

int main(void)
{
	// 0 ~256 >> 10단계로 나누어서 누적값을 카운팅한 결과값이다.

	Mat hist = (Mat_<float>(1, 10) <<
		5000, 3000, 190, 6500, 1200,
		3500, 6000, 12000, 35000, 4000);

	cout << hist << endl << endl;

	//void normalize(
	// InputArray src, 
	// InputOutputArray dst, 
	// double alpha = 1, 
	// double beta = 0,
	//int norm_type = NORM_L2 );

	normalize(hist, hist, 0, 200, NORM_MINMAX ); //최소 190 을 0, 최대 35000을 200으로
	cout << hist << endl;

}

int main(void)
{
	// 0 ~256 >> 10단계로 나누어서 누적값을 카운팅한 결과값이다.

	Mat hist = (Mat_<float>(1, 10) <<
		5000, 3000, 190, 6500, 1200,
		3500, 6000, 12000, 35000, 4000);

	cout << hist << endl << endl;

	normalize(hist, hist, 1, 0, NORM_L1 ); // 총 합의 비율만큼 계산하기
	cout << hist << endl;
	cout << sum(hist) << endl;

}

int main(void)
{

	// calcHist로 데이터를 뽑았다고 친다.
	Mat hist = (Mat_<float>(1, 10) <<
		5000, 3000, 190, 6500, 1200,
		3500, 6000, 12000, 35000, 4000);

	cout << hist << endl << endl;

	normalize(hist, hist, 0, 200, NORM_MINMAX ); // 총 합의 비율만큼 계산하기
	cout << hist << endl << endl;
 


	Mat img(200, 256, CV_8U, 255);


	// 원래는 구해야 한다.
	float bin = 256 / 10; // img.Width / hist.Height

	// for문을 사용해야 되지만, 규칙을 찾기 위해 직접 기록.
	// [27, 16, 0, 36, 5, 19, 33, 67, 200, 21]
	rectangle(img, Point(0, 0), Point(25, 27), 128, -1);
	rectangle(img, Point(25, 0), Point(50, 16), 128, -1);
	rectangle(img, Point(50, 0), Point(75, 0), 128, -1);
	rectangle(img, Point(75, 0), Point(100, 36), 128, -1);
	rectangle(img, Point(100, 0), Point(125, 5), 128, -1);
	rectangle(img, Point(125, 0), Point(150, 19), 128, -1);
	rectangle(img, Point(150, 0), Point(175, 33), 128, -1);
	rectangle(img, Point(175, 0), Point(200, 67), 128, -1);
	rectangle(img, Point(200, 0), Point(225, 200), 128, -1);
	rectangle(img, Point(225, 0), Point(250, 21), 128, -1);


	flip(img, img, 0);
	imshow("1", img);
	waitKey();

}

 

이미지를 그레이스케일로 바꾸고 histgram을 통해 그래프 살펴보기

int main(void)
{
	Mat img = imread("image/tiger.bmp", IMREAD_GRAYSCALE);

 
	int channels[] = { 0 };
	Mat hist;

	// 0 부터 100사이를 4 간격으로
	int histSize[] = { 255 };
	float range[] = { 0,255 };
	const float* ranges[] = { range };

	calcHist(
		&img,//원본 이미지 설정
		1,
		channels,      //0번 채널만 계산(gray)
		Mat(),         //Matrix Masking을 하지 않겠다.
		hist,
		1,            //결과 매트리스를 몇차원으로 받을 것인가에 대한 설정
		histSize,
		ranges         //유효 범위를 설정한다(0,256)
	);


	normalize(hist, hist, 0, 200, NORM_MINMAX);
	Mat histImage(200, 256, CV_8U, 255);
	int bin = histSize[0] / (range[1] - range[0]);

	for (int i = 0; i < histSize[0]; i++)
	{
		rectangle(histImage, Point(i * bin, 0), Point((i + 1) * bin, (int)(hist.at<float>(i, 0))), 128, -1);
	}

	flip(histImage, histImage, 0);
	imshow("원본", img);
	imshow("gray", histImage);
	waitKey();

}

 

int main(void)
{
	Mat img = imread("image/tiger.bmp", IMREAD_COLOR);


	const int channel_B[] = { 0 };
	const int channel_G[] = { 1 };
	const int channel_R[] = { 2 };
	Mat histB, histG, histR;

	// 0 부터 100사이를 4 간격으로
	int histSize[] = { 255 };
	float range[] = { 0,255 };
	const float* ranges[] = { range };

	calcHist(&img, 1, channel_B, Mat(), histB, 1, histSize, ranges);
	calcHist(&img, 1, channel_G, Mat(), histG, 1, histSize, ranges);
	calcHist(&img, 1, channel_R, Mat(), histR, 1, histSize, ranges);

	Mat histImageB(200, 256, CV_8UC3, Scalar(0, 0, 0));
	normalize(histImageB, histImageB, 0, 200, NORM_MINMAX);
	Mat histImageG(200, 256, CV_8UC3, Scalar(0, 0, 0));
	normalize(histImageG, histImageG, 0, 200, NORM_MINMAX);
	Mat histImageR(200, 256, CV_8UC3, Scalar(0, 0, 0));
	normalize(histImageR, histImageR, 0, 200, NORM_MINMAX);
	int bin = img.cols / histSize[0];


	for (int i = 0; i < histSize[0]; i++)
	{
		rectangle(histImageB, Point(i * bin, 0), Point((i + 1) * bin, (int)(histB.at<float>(i, 0))), Scalar(255,0,0), -1);
		rectangle(histImageG, Point(i * bin, 0), Point((i + 1) * bin, (int)(histG.at<float>(i, 0))), Scalar(0, 255, 0), -1);
		rectangle(histImageR, Point(i * bin, 0), Point((i + 1) * bin, (int)(histR.at<float>(i, 0))), Scalar(0, 0, 255), -1);
	}

	flip(histImageB, histImageB, 0);
	flip(histImageG, histImageG, 0);
	flip(histImageR, histImageR, 0);
	imshow("원본", img);
	imshow("Blue", histImageB);
	imshow("Green", histImageG);
	imshow("Red", histImageR);
	waitKey();
}

 

위에꺼 함수화 처리??

void hist(Mat img, const int channel)
{
	int histSize[] = { 255 };
	float range[] = { 0,255 };
	const float* ranges[] = { range };
	Mat histcolor;
	int r, g, b;
	r = g = b = 0;
	string title;

	calcHist(&img, 1, &channel, Mat(), histcolor, 1, histSize, ranges);

	Mat histImage(200, 256, CV_8UC3, Scalar(0, 0, 0));
	normalize(histImage, histImage, 0, 200, NORM_MINMAX);

	int bin = img.cols / histSize[0];
	
	switch (channel)
	{
	case 0: title = "Blue"; b = 255; break;
	case 1: title = "Green";g = 255; break;
	case 2: title = "Red"; r = 255; break;
	default:
		break;
	}

	for (int i = 0; i < histSize[0]; i++)
		rectangle(histImage, Point(i * bin, 0), Point((i + 1) * bin, (int)(histcolor.at<float>(i, 0))), Scalar(b, g, r), -1);

	flip(histImage, histImage, 0);
	imshow(title, histImage);
}

int main(void)
{
	Mat img = imread("image/tiger.bmp", IMREAD_COLOR);

	const int channel_B =  0 ;
	const int channel_G =  1 ;
	const int channel_R =  2 ;
	Mat histB, histG, histR;

	hist(img, channel_B);
	hist(img, channel_G);
	hist(img, channel_R);

	imshow("원본", img);
	waitKey();
}

 

채널 합성 & 분리

 

int main(void)
{
	Mat img = imread("Image/tiger.bmp", IMREAD_COLOR);
	cout << img.channels() << endl;
	cout << img.rows << endl;
	cout << img.cols << endl;


	Mat ch0(3, 4, CV_8U, Scalar(10));
	cout << ch0 << endl << endl;
	Mat ch1(3, 4, CV_8U, Scalar(20));
	cout << ch1 << endl << endl;
	Mat ch2(3, 4, CV_8U, Scalar(30));
	cout << ch2 << endl << endl;

	Mat bgr_arr[] = { ch0, ch1, ch2 };
	cout << bgr_arr[2] << endl << endl;

	// 채널 합성
	Mat bgr;
	merge(bgr_arr, 3, bgr);
	cout << bgr << endl << endl;

	// 3채널을 >> 1채널 3개로 분리
	vector<Mat> bgr3;
	split(bgr, bgr3);

	cout << bgr3[0] << endl << endl;
	cout << bgr3[1] << endl << endl;
	cout << bgr3[2] << endl << endl;

}

 

 

 

int main(void)
{
	Mat img = imread("Image/tiger.bmp", IMREAD_COLOR);
	cout << img.channels() << endl;
	cout << img.rows << endl;
	cout << img.cols << endl;

	Mat bgr[3];
	split(img, bgr);

	//imshow("1" , img);
	//imshow("b" , bgr[0]);
	//imshow("g" , bgr[1]);
	//imshow("r" , bgr[2]);

	cout << bgr[0].channels() << endl;

	Mat temp(img.rows, img.cols, CV_8U, Scalar(0));

	Mat arr[3] = { temp, temp, bgr[2] };
	Mat result;
	merge(arr, 3, result);
	imshow("result", result);
	waitKey();

}

int main(void)
{
	uchar data[] = {
		0, 1, 2, 3,
		4, 5, 6, 7,
		8, 9, 0, 1
	};
	Mat ch0(3, 4, CV_8U, data);
	Mat ch1(3, 4, CV_8U, data);
	Mat ch2(3, 4, CV_8U, data);
	vector<Mat> vec;
	vec.push_back(ch0);
	vec.push_back(ch1);
	vec.push_back(ch2);

	Mat result;
	merge(vec, result);
	cout << result << endl;

}

오류나오는 코드

int main(void)
{
	uchar data[] = {
		0, 1, 2, 3,
		4, 5, 6, 7,
		8, 9, 0, 1
	};
	Mat ch0(3, 4, CV_8U, data);
	Mat ch1(3, 4, CV_8U, data);
	Mat ch2(3, 4, CV_8U, data);
	vector<Mat> vec;
	vec.push_back(ch0);
	vec.push_back(ch1);
	vec.push_back(ch2);

	// 3채널
	Mat result;
	merge(vec, result);
	cout << result << endl << endl;

	//2채널
	Mat m1(result.size(), CV_8UC2, Scalar(0));
	cout << m1 << endl << endl;

	//1채널
	Mat m2(result.size(), CV_8UC1, Scalar(0));
	cout << m2 << endl << endl;

	Mat out[] = { m1, m2 };

	int ar[] = {
		0, 0,
		2, 1,
		1, 2
	};

	mixChannels(&result, 1, out, 2, ar, 3);

		cout << m1 << endl << endl;
		cout << m2 << endl << endl;
}

 

void showMatInfo(Mat mat, string str)
{
	cout << "채널 : " << mat.channels() << "\t\t";
	cout << "행열 : " << mat.size << endl;
	cout << str + " = " << endl << mat << endl;
	cout << "--------------------------------------" << endl << endl;
}

int main(void)
{
	Mat m1(3, 6, CV_8UC1, Scalar(10));
	showMatInfo(m1, "m1");

	Mat m2(3, 6, CV_8UC1, Scalar(50));
	showMatInfo(m2, "m2");

	Mat mask(m1.size(), CV_8UC1, Scalar(0));
	showMatInfo(mask, "Mask");

	//
	Rect rect(Point(3, 0), Size(3, 3));
	cout << "rect.x = " << rect.x << endl;
	cout << "rect.y = " << rect.y << endl;
	cout << "rect.width = " << rect.width << endl;
	cout << "rect.height = " << rect.height << endl;

	//특정영역만 값을 설정
	mask(rect).setTo(1);
	showMatInfo(mask, "mask");

	//덧셈
	Mat m3;
	m3 = m1 + m2;
	showMatInfo(m3, "m3");
	add(m1, m2, m3);
	showMatInfo(m3, "m3");

	//덧셈 원하는 마스크 영역
	Mat m4;
	add(m1, m2, m4, mask);
	showMatInfo(m4, "m4");
	showMatInfo(mask, "mask");

	// 나눗셈
	Mat m5;
	divide(m2, m1, m5);
	showMatInfo(m5, "m5");

	//---------------
	Matx<float, 1, 5> m10(1, 2, 3, 5, 10);
	Mat m60;
	exp(m10, m60);
	showMatInfo(m60, "m60");
	log(m10, m60);
	showMatInfo(m60, "m60");
	sqrt(m10, m60);
	showMatInfo(m60, "m60");
	pow(m10,2, m60);
	showMatInfo(m60, "m60");


}

 

논리연산

int main(void)
{
	Mat image1(300, 300, CV_8U, Scalar(0));
	Mat image2(300, 300, CV_8U, Scalar(0));

	Point center = image1.size() / 2;
	circle(image1, center, 100, Scalar(255), -1);
	imshow("image1", image1);

	rectangle(image2,Point(0,0), Point(150,300), Scalar(255), -1);
	imshow("image2", image2);

	Mat image3;
	bitwise_or(image1, image2, image3);
	imshow("image3", image3);

	Mat image4;
	bitwise_and(image1, image2, image4);
	imshow("image4", image4);

	Mat image5;
	bitwise_xor(image1, image2, image5);
	imshow("image5", image5);

	Mat image6;
	bitwise_not(image1, image6);
	imshow("image6", image6);



	waitKey();
}

 

 

void showMatInfo(Mat mat, string str)
{
	cout << "채널 : " << mat.channels() << "\t\t";
	cout << "행열 : " << mat.size << endl;
	cout << str + " = " << endl << mat << endl;
	cout << "--------------------------------------" << endl << endl;
}

int main(void)
{
	char data[] = {
			10, 30, 40, 50,
			60, 70, 75, 90,
			100, 150, 200, 250
	};

	Mat image(3, 4, CV_8U, data);
	showMatInfo(image, "image");

	Mat result;
	threshold(image,  result, 70, 255, THRESH_BINARY);
	showMatInfo(result, "result");

}

 

 

 

int main(void)
{
	Mat image = imread("Image/bit_test.jpg", IMREAD_COLOR);
	Mat logo = imread("Image/logo.jpg", IMREAD_COLOR);

	//imshow("1", image);
	imshow("2", logo);


	Mat result1;
	// 이진화
	threshold(logo, result1, 70, 255, THRESH_BINARY);
	//imshow("3", result1);

	Mat masks[5];
	// 0, 1, 2은 값을 받는다. 2개는 놀고 있다.
	split(result1, masks);
	imshow("blue", masks[0]);
	imshow("green", masks[1]);
	imshow("red", masks[2]);

	bitwise_or(masks[0], masks[1],  masks[3]);
	bitwise_or(masks[2], masks[3], masks[3]);
	imshow("or", masks[3]);

	bitwise_not(masks[3], masks[4]);
	imshow("not", masks[4]);

	waitKey();
}

 

 

'.Net 교육 > OpenCV(C++)' 카테고리의 다른 글

OpenCV(C++) - 화소처리  (0) 2021.05.21
OpenCV(C++) - 행렬 연산 02  (0) 2021.05.21
OpenCV(C++) - User Interface 02  (0) 2021.05.18
OpenCV(C++) - User Interface 01  (0) 2021.05.17
OpenCV(C++) - 기본데이터 타입  (0) 2021.05.17
Posted by pi92

블로그 이미지
pi92

공지사항

Yesterday
Today
Total

달력

 « |  » 2025.5
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

최근에 올라온 글

최근에 달린 댓글

글 보관함