대학교/영상처리

동적할당된 Buffer를 이용한 3,1 Channel Mat 구조 검증

무말랭이 2022. 3. 19. 11:22
#include<iostream>
#include"opencv2/core.hpp"
#include"opencv2/highgui.hpp"
#include"opencv2/imgproc.hpp"

using namespace std;
using namespace cv;

int main()
{
	Mat img = imread("C:\\dev\\c\\week2\\sample.jpg");
	
	Mat resize_img;
	int width = 240;
	int height = 240;
	int channels = 3;
	resize(img, resize_img, Size(width, height));

	int* buffer = new int[width * height * channels]();
	Mat buffer_img(width, height, CV_8UC3, Scalar(0, 0, 0));

	for (int c = 0; c < channels; c++) {
		for (int h = 0; h < height; h++) {
			for (int w = 0; w < width; w++) {
				buffer[(h * height + w) * channels + c] = resize_img.at<Vec3b>(h, w)[c];
			}
		}
	}

	for (int c = 0; c < channels; c++) {
		for (int h = 0; h < height; h++) {
			for (int w = 0; w < width; w++) {
				buffer_img.at<Vec3b>(h, w)[c] = buffer[(h * height + w) * channels + c];
			}
		}
	}

	imshow("img", img);
	imshow("resize_img", resize_img);
	imshow("buffer_img", buffer_img);
	waitKey(0);

	delete[] buffer;

	return 0;
}

#include<iostream>
#include"opencv2/core.hpp"
#include"opencv2/highgui.hpp"
#include"opencv2/imgproc.hpp"

using namespace std;
using namespace cv;

int main()
{
	Mat img = imread("C:\\dev\\c\\week2\\sample.jpg");
	
	Mat resize_img;
	int width = 240;
	int height = 240;
	int channels = 3;
	resize(img, resize_img, Size(width, height));

	int* buffer = new int[width * height * channels]();
	Mat buffer_img(width, height, CV_8UC3, Scalar(0, 0, 0));

	for (int c = 0; c < channels; c++) {
		for (int h = 0; h < height; h++) {
			for (int w = 0; w < width; w++) {
				buffer[(h * height + w) * channels + c] = resize_img.at<Vec3b>(h, w)[c];
			}
		}
	}

	for (int c = 0; c < channels; c++) {
		for (int h = 0; h < height; h++) {
			for (int w = 0; w < width; w++) {
				buffer_img.at<Vec3b>(h, w)[c] = buffer[(h * height + w) * channels + c]+1;
			}
		}
	}

	imshow("img", img);
	imshow("resize_img", resize_img);
	imshow("buffer_img", buffer_img);
	waitKey(0);

	delete[] buffer;

	return 0;
}

#include<iostream>
#include"opencv2/core.hpp"
#include"opencv2/highgui.hpp"
#include"opencv2/imgproc.hpp"

using namespace std;
using namespace cv;

int main()
{
	Mat img = imread("C:\\dev\\c\\week2\\sample.jpg");
	
	Mat resize_img;
	int width = 240;
	int height = 240;
	int channels = 3;
	resize(img, resize_img, Size(width, height));

	int* buffer = new int[width * height * channels]();
	Mat buffer_img(width, height, CV_8UC3, Scalar(0, 0, 0));

	int* gray_buffer = new int[width * height]();
	Mat gray_img(width, height, CV_8UC1, Scalar(0));

	for (int c = 0; c < channels; c++) {
		for (int h = 0; h < height; h++) {
			for (int w = 0; w < width; w++) {
				buffer[(h * height + w) * channels + c] = resize_img.at<Vec3b>(h, w)[c];
			}
		}
	}

	for (int c = 0; c < channels; c++) {
		for (int h = 0; h < height; h++) {
			for (int w = 0; w < width; w++) {
				buffer_img.at<Vec3b>(h, w)[c] = buffer[(h * height + w) * channels + c];
			}
		}
	}

	for (int h = 0; h < height; h++) {
		for (int w = 0; w < width; w++) {
			gray_buffer[h * height + w] = (buffer[(h * height + w) * channels + 0] + buffer[(h * height + w) * channels + 1] + buffer[(h * height + w) * channels + 3]) / 3;
		}
	}

	for (int h = 0; h < height; h++) {
		for (int w = 0; w < width; w++) {
			gray_img.at<uchar>(h, w) = gray_buffer[h * height + w];
		}
	}

	imshow("img", img);
	imshow("resize_img", resize_img);
	imshow("buffer_img", buffer_img);
	imshow("gray_img", gray_img);
	waitKey(0);

	delete[] buffer;
	delete[] gray_buffer;

	return 0;
}

'대학교 > 영상처리' 카테고리의 다른 글

CCD, CMOS 센서의 세부적인 블록 메커니즘  (0) 2022.04.24
Continuos, Discrete Time Signal 의 특징나열  (0) 2022.04.24
CCD 센서와 CMOS 센서  (0) 2022.04.24
bmp 구조  (0) 2022.03.28
opencv 디렉토리 환경설정  (0) 2022.03.14